# [SOLVED]How to convert month,year and date from unix time ?

Status
Not open for further replies.

#### bianchi77

Guys,

How to calculate date,month and year ? from RTC_GetCounter function,

I saw :

Code:
uint32_t RTC_GetCounter(void)
{
uint16_t tmp = 0;
tmp = RTC->CNTL;
return (((uint32_t)RTC->CNTH << 16 ) | tmp) ;
}
I've done :
/* Load the Counter value */
Tmp = RTC_GetCounter();

/* Load the Counter value */
Tmp = RTC_GetCounter();

/* Compute day */
WEEKDAY = (Tmp / 86400)%7;

/* Compute date */
DATE = (Tmp / 86400)%360;

/* Compute month */
MONTH = (Tmp / 2629743)%12;

/* Compute year */
YEAR = (Tmp / 31556926)+1970;

Any clues ?
thanks

Last edited by a moderator:

#### paulfjujo

Re: Does anyone know on how to convert month,year and date from unix time ?

hello,

Are you refering to time stamp value to convert in ascii date and time
"http://www.timestamp.fr/?"

#### betwixt

##### Super Moderator
Staff member
Re: Does anyone know on how to convert month,year and date from unix time ?

bianchi77, none of those will work, the calculation is far more complex. I suggest you seach for "dostounix" and "unixtodos" time functions is a standard C compiler, they will have the formula there that you can copy or adapt. These functions accept a structure containing the time and date and convert it to a number or take a unix time value and convert it to a structure containing the time and date.

Brian.

#### paulfjujo

Re: Does anyone know on how to convert month,year and date from unix time ?

hello,

I think you can not use , inbuilt fonction of unix to get back date and time, is it ?

and your calculations are not egnough accurate...
i did some test with excel and don't get right date and time , even using more accurate values
for Nb of secondes per year or average nb of days in a year..
it's seems very complicated with some change of the rules between 1970 and Today concerning time stamp...

I am also interested on this subject , but to do the reverse , built time stamp
in RFO-basic Android, to load a picture based on TimeStamp.png file name
via URL link. My probleme is synchro at One second to get correct name!

if you are interested i can post the xls file..

#### bianchi77

Re: Does anyone know on how to convert month,year and date from unix time ?

sure you can post the xls file, I can display year and month correctly but, for day, I haven't got the right formula....any ideas ?
I'm using internal counter of STM32 (RTC)

#### paulfjujo

Re: Does anyone know on how to convert month,year and date from unix time ?

I have same probleme for day and Hour,Mn..

maybe the soluce is to dissambly the code of an Unix function
to see in details what is the algorithm and reference values
and correctors factors.

View attachment Time_stamp_test.xls.txt

#### bianchi77

Re: Does anyone know on how to convert month,year and date from unix time ?

how do you calculate for date ?

I got :
DATE = ((Tmp / 31556926)%365)-40; ...almost ...
Tmp = unix time....

any ideas ?

#### godfreyl

Re: Does anyone know on how to convert month,year and date from unix time ?

Not every year has the same number of days. However, every four year period between 1901 and 2099 has (365*4 +1 = 1461) days in it. If you start by calculating the number of four year periods, you will know how many leap days to account for.

##### Super Moderator
Staff member
Re: Does anyone know on how to convert month,year and date from unix time ?

Astronomers use the Julian day to keep track of time. An internet search might find the needed conversion routines.

#### paulfjujo

Re: Does anyone know on how to convert month,year and date from unix time ?

hello,

i did some comparaison with old program written with Turbo C 2.00
about Julian date and Time stamp.
1) program is based on julian day ... is ok to do calculus between some dates, but not with time stamp
2) uses DOS function to get time stamp
3) this is web appli Time Stamp
on second program Usetime.exe , if i force the time stamp same as on time stamp value given by the third web application
i don't get the same time and day ...
i get 6Hours of difference ???
In France we are in winter shift time , so +1 hour only.
Thoses old program were usefull in periode 1970-1999
maybe with the 2000 year bug rules as changed ?

in the zip, executable and source C ,
in particular Calendri.h has a lot of data and time functions.

View attachment TimeCompare.zip

#### bianchi77

Re: Does anyone know on how to convert month,year and date from unix time ?

I fix it with localtime

Code:
/* Load the Counter value */
Tmp = RTC_GetCounter();
THH = utcTimeSnapshot->tm_hour;

Status
Not open for further replies.