This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

msp430 and mktime

Other Parts Discussed in Thread: MSP430F6736, MSP430F5438A

MSP430F6736

My application needs to create a unix time stamp from available RTC data.  This code yields wild answers (see values in comments).

I should think a long long would be more than sufficient to hold the result data.  Perhaps this is simply not supported?

long long UnixTimeStamp(void)
{
    struct tm t;
    //time_t t_of_day;
    long long  t_of_day;
    BCD2BIN = RTCYEAR;
    unsigned long nYear=BCD2BIN;
    BCD2BIN = RTCDAY;
    unsigned char nDay=BCD2BIN;
    BCD2BIN = RTCMON;
    unsigned char nMonth=BCD2BIN;
    BCD2BIN = RTCHOUR;
    unsigned char nHour=BCD2BIN;
    BCD2BIN = RTCMIN;
    unsigned char nMin=BCD2BIN;
    BCD2BIN = RTCSEC;
    unsigned char nSec=BCD2BIN;

    t.tm_year = nYear-1900;
    t.tm_mon = nMonth; // Month, 0 - jan
    t.tm_mday = nDay; // Day of the month
    t.tm_hour = nHour;
    t.tm_min = nMin;
    t.tm_sec = nSec;
    t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
    t_of_day = mktime(&t);  // timestamp from RTC data.  Result=3565814343

    t.tm_year = 2011-1900;
    t.tm_mon = 7; // Month, 0 - jan
    t.tm_mday = 8; // Day of the month
    t.tm_hour = 16;
    t.tm_min = 11;
    t.tm_sec = 42;
    t.tm_isdst = -1;
    t_of_day = mktime(&t);  // timestamp from test data.  Result=3521808702

    return( t_of_day);
}

  • Note my epoch above: nYear-1900.

    Note this (processors.wiki.ti.com/index.php/Time_and_clock_RTS_Functions#epoch) which states, "However, the C standard does not require any particular epoch, and the TI version of time() uses a different epoch: midnight UTC-6 Jan 1, 1900."

    The epoch in common use, nYear-1970, now gives me a proper unix timestamp.

    Dear TI: where the documentation is conflicting or broken it would be helpful to fix or remove it.

  • Mark Richards said:
    Dear TI: where the documentation is conflicting or broken it would be helpful to fix or remove it.

    Well, which compiler do you use? mktime is not processor specific, it is from a compiler-provided library.

  • Jens,

    The compiler that CCS 5 uses.  ti/ccsv5/tools/compiler/msp430_4.1.1/bin/cl430

  • Mark Richards said:
    The compiler that CCS 5 uses.

    Okay,. Well, maybe this has been "fixed" for the new 5x IDE (4x compiler).  The WIKI page is at least two years old (last change january 2011). Adn it is a Wiki, not the official compiler documentation.

    Sure, it should be fixed if things changed, but a wiki usually is an external resource and not directly connected to any product development.

    BTW, passing '0' as time value to the time functions immediately gives you the epoch. :)

  • Jens-Michael,
    epoch = time(0); // will return the epoch on the MSP430F5438a?
    Does that mean that time() will return epoch of whatever time is currently in the RTC?
    So does that mean I need to have the RTC initialized and running or will time() work without the RTC initialized?
    I noticed that localtime was returning the correct time and date, but, I had not even initialized the RTC... I then ran the source without CCS Debug and saw that it returned a weird time or whatever was currently in the RTC registers so I am assuming that the gmtime, time, mktime, localtime functions will access the RTC registers...
    Hardy
  • It's been some time since you asked, ans maybe you figured out yourself, but anyway...
    It's not time() that returns the epoch (it will return the current time if you pass NULL).
    But gmtime will fill a tm struct with the time_t value you pass. and if you pass 0, this will be the base epoch:

    struct tm * ptm;
    time_t zero = 0;
    ptm = gmtime(&zero)

    then find the epoch base date in ptm.year etc.
  • Jens,

    Thank you for checking in after all this time.  Yes, I got things working, and quite nicely, but would have to read the code again to tell you what I did. 


    At my age, the ram is quite volatile!


    All the best,

    /mark

  • Welcome to the club. The trick is to remember that you did and where you did, so you can look-up what you did, if needed. Same for almost any information. There's more to know than would fit in any brain, even younger ones. As long as you remember that there was something to remember, you can always try to dig it out and refresh memory. Even better if you still remember where to dig.

**Attention** This is a public forum