10 years, 9 months ago.

Time - weird problem

It seems like mbed thinks it is February when correct month is January, and increments to the next month at midnight of Jan 28, 2014.

Here is a little piece of code along with the screen outputs:

        printf("Year is %s\r\n",yr);
        printf("Month is %s\r\n",mo);
        printf("Day is %s\r\n",dy);
        printf("Hour is %s\r\n",hr);
        printf("Min is %s\r\n",mn);
        printf("Sec is %s\r\n",sc);

        sci = atoi(sc);
        mni = atoi(mn);
        hri = atoi(hr);
        dyi = atoi(dy);
        moi = atoi(mo);
        yri = atoi(yr);

        printf("Yr %d Mo %d Day %d Hr %d Min %d Sec %d\r\n",yri,moi,dyi,hri,mni,sci);

        struct tm t;
        t.tm_sec = sci;    // 0-59
        t.tm_min = mni;    // 0-59
        t.tm_hour = hri;   // 0-23
        t.tm_mday = dyi;   // 1-31
        t.tm_mon = moi;     // 0-11
        t.tm_year = 100+yri;  // year since 1900

        // convert to timestamp and set (1256729737)
        time_t seconds = mktime(&t);
        pc.printf("Seconds is %d\r\n",seconds);

        if(yri!=0) set_time(seconds);

        time_t seconds2 = time(NULL);

        printf("Time as seconds since January 1, 1970 = %d\r\n", seconds2);

        printf("Time as a basic string = %s\r\n", ctime(&seconds2));

Output:

Year is 14

Month is 01

Day is 28

Hour is 14

Min is 46

Sec is 35

Yr 14 Mo 1 Day 28 Hr 14 Min 46 Sec 35

Seconds is 1393598795

Time as seconds since January 1, 1970 = 1393598795

Time as a basic string = Fri Feb 28 14:46:35 2014

printf("Tnow : %d,\r\n", Tnow);
printf("%04d/%02d/%02d %02d:%02d:%02d\r\n",t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);

Output:

Tnow : 1393631999,

2014/01/28 23:59:59

Tnow : 1393632000,

2014/02/01 00:00:00

Instead of 2014/02/01 00:00:00, time jumps to 2014/01/28 00:00:00

Is this a known problem ? Where should I look to correct this ?

Orhan

1 Answer

10 years, 9 months ago.

I think months count from 0-11, so 1 is february. Anyhow, mbed has no clue about time until you initialise the RTC. The RTC stops running when power is removed, unless you have a battery connected to the mbed (LPC1768 only).

Accepted Answer