Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
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;

Please correct me .....
Any clues ?
thanks
 
Last edited by a moderator:

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,303
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
Joined
Jul 4, 2009
Messages
15,216
Helped
4,958
Reputation
9,938
Reaction score
4,783
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
129,201
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

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,303
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..
TimeStamp_test.jpg
 

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
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

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,303
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

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
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

Advanced Member level 5
Joined
Apr 18, 2012
Messages
1,981
Helped
632
Reputation
1,266
Reaction score
624
Trophy points
1,393
Activity points
12,772
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.
 

BradtheRad

Super Moderator
Staff member
Joined
Apr 1, 2011
Messages
14,240
Helped
2,817
Reputation
5,642
Reaction score
2,757
Trophy points
1,393
Location
Minneapolis, Minnesota, USA
Activity points
106,225
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

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,303
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.

Time_calculs_compare.jpg

View attachment TimeCompare.zip
 

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top