2014-01-07 11:35 PM
Guys,
How to calculate date,month and year ? from RTC_GetCounter function, I saw : 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 ? thanks2014-01-08 01:38 AM
No ,I'd advise that you don't do this as you will get into lots of issues with leap years etc.. Use gmtime function (ideally thread safe version gmtime_r) from time library supplied with your tools (time.h). Read this: http://www.cplusplus.com/reference/ctime/
Google gmtime and mktime (and time.h) as this is a well discussed topic on the web. E.g.#include <
time.h
>
struct tm tm_time;
time_t t_time;
t_time = RTC_GetCounter();
gmtime_r( &t_time, &tm_time );
Ask better questions like what your overall aim is not specifics that are going down a dead end anyway. And please stop creating new threads with no reference to old threads that relate to the same issue. Let's have one thread with better questions.
2014-01-08 08:20 PM
Where can I download time.h ?
2014-01-08 08:32 PM
I made :
struct tm utcTimeSnapshot; time_t Tmp; /* Load the Counter value */ Tmp = RTC_GetCounter(); utcTimeSnapshot = *gmtime(&Tmp); How can I extract, date,month,year, hour,minute,second ?2014-01-08 08:57 PM
struct tm *utcTimeSnapshot;
time_t Tmp; /* Load the Counter value */ Tmp = RTC_GetCounter(); utcTimeSnapshot = gmtime(&Tmp); // it returns a pointer Then utcTimeSnapshot->tm_hour, tm_year, etc2014-01-08 09:02 PM
Also, be aware the reason you read the value twice is so you can actually COMPARE the value, to make sure it doesn't change, ie at a tick boundary
If it changes you should read it again until it doesn't, nominally one additional read.2014-01-08 09:31 PM
I still read the value on second, not in the exact value, for example Wednesday is still on 134218199, how can I change it to a readable value for human ?
It's stable and easier with Tmp = RTC_GetCounter();
utcTimeSnapshot = localtime(&Tmp); uint32_t /*Tmp,*/THH,TMM,TSS,WEEKDAY,DATE,MONTH,YEAR; DATE = utcTimeSnapshot->tm_mday; sprintf(date,''%02d'',DATE); lcd_string(date);lcd_string(''-'');
2014-01-09 12:46 AM
// Define g_DateStructVar that contain Day,Month,Year,Hour,Minute
Ans use this function . void CalculateDateTime(uint32_t t){ const unsigned int daysInMonth [] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //has to be const or compiler compaints int d,m; unsigned int ss,mm,hh,yOff; ss = t % 60; t /= 60; mm = t % 60; t /= 60; hh = t % 24; unsigned int days = t / 24; unsigned int leap; for (yOff = 0; ; ++yOff) { leap = yOff % 4 == 0; if (days < 365 + leap) break; days -= 365 + leap; } unsigned int daysPerMonth =0; for ( m = 1; ; ++m) { daysPerMonth = daysInMonth [m - 1]; if (leap && m == 2) ++daysPerMonth; if (days < daysPerMonth) break; days -= daysPerMonth; } d = days + 1; g_DateStructVar.Day = d ; g_DateStructVar.Month = m ; g_DateStructVar.Year = yOff + 1970 ; g_DateStructVar.Minutes = mm ; g_DateStructVar.Seconds = ss ; g_DateStructVar.Hours = hh ; }2014-01-09 03:29 AM
I still read the value on second, not in the exact value, for example Wednesday is still on 134218199, how can I change it to a readable value for human ?
Does it return a NULL pointer? Does the library code you're using actually support gmtime() properly? Step into it, look at a disassembly.