Skip to main content
antonius
Associate III
January 8, 2014
Question

How to calculate date,month and year ?

  • January 8, 2014
  • 8 replies
  • 1976 views
Posted on January 08, 2014 at 08:35

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 ?

thanks
    This topic has been closed for replies.

    8 replies

    trevor23
    Associate III
    January 8, 2014
    Posted on January 08, 2014 at 10:38

    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.
    antonius
    antoniusAuthor
    Associate III
    January 9, 2014
    Posted on January 09, 2014 at 05:20

    Where can I download time.h ?

    antonius
    antoniusAuthor
    Associate III
    January 9, 2014
    Posted on January 09, 2014 at 05:32

    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 ?

    Tesla DeLorean
    Guru
    January 9, 2014
    Posted on January 09, 2014 at 05:57

     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, etc
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    January 9, 2014
    Posted on January 09, 2014 at 06:02

    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.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    January 9, 2014
    Posted on January 09, 2014 at 06:31

    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(''-'');

    leventeyigel52
    Associate III
    January 9, 2014
    Posted on January 09, 2014 at 09:46

    // 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 ; 

      

    }

    Tesla DeLorean
    Guru
    January 9, 2014
    Posted on January 09, 2014 at 12:29

    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.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..