Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Minor TimeLib bug... dayStr() returns "Jan"?

  1. #1
    Junior Member
    Join Date
    Jan 2017
    Posts
    5

    Minor TimeLib bug... dayStr() returns "Jan"?

    Hi Everyone,

    I'm a new Teensy 3.6 user. I'm using Arduino 1.8.0 and Teensyduino 1.35.

    Here is the source code:

    Code:
    #include <TimeLib.h>
    
    void setup() 
    {
        Serial.begin(250000);
        setTime(RTC_TSR);
    }
    
    void loop() 
    {
        time_t timeNow = now();
        Serial.printf("\n%s, %02u%s%u %02u:%02u:%02u", 
                      dayShortStr( weekday(timeNow) ),
                      day(timeNow), monthShortStr(month(timeNow)), 
                      year(timeNow), hour(timeNow), minute(timeNow), 
                      second(timeNow) 
                     );
    }
    The output:
    Code:
    Jan, 23Jan2017 16:30:57
    Shouldn't it be?:
    Code:
    Mon, 23Jan2017 16:30:57
    I tried dayStr( weekday(timeNow) ). It does the same. It also displays 'Jan' instead of 'Monday'.

    Am I using this function incorrectly?

    Not a big deal. Just reporting what appears to be a bug. I don't actually need the function. I was just doing some testing and noticed.

    BTW - I was pleasantly disappointed that the RTC is auto-magically programmed and the time is set. I was preparing to write a driver. Super kudos!

  2. #2
    Senior Member KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    2,330
    Working as designed

    That is all of the string functions in this library use one character buffer and returns a pointer to that buffer, so last call wins.

    That is the call: monthShortStr(month(timeNow))
    Will return the same pointer and it overwrote the output of the dayShortStr.

    This program returns more what you are expecting...
    Code:
    #include <TimeLib.h>
    
    void setup() 
    {
        Serial.begin(250000);
        setTime(RTC_TSR);
    }
    
    void loop() 
    {
        time_t timeNow = now();
        Serial.printf("\n%s,", dayShortStr( weekday(timeNow) ));
        Serial.printf(" %02u%s%u %02u:%02u:%02u", 
                      day(timeNow), monthShortStr(month(timeNow)), 
                      year(timeNow), hour(timeNow), minute(timeNow), 
                      second(timeNow) 
                     );
    }

  3. #3
    Junior Member
    Join Date
    Jan 2017
    Posts
    5
    Yep. That would do it. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •