C/C++ Time functions & Structures

C/C++ Time functions

The standard C structure time_t

Although not defined, this is almost always a integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time.

The standard C structure tm

Member objects

 int tm_sec	//seconds after the minute – [0, 60][@1]    (public member object) 
int tm_min	//minutes after the hour – [0, 59]    (public member object) 
int tm_hour	//hours since midnight – [0, 23]    (public member object) 
int tm_mday	//day of the month – [1, 31]    (public member object) 
int tm_mon	//months since January – [0, 11]    (public member object) 
int tm_year	//years since 1900    (public member object) 
int tm_wday	//days since Sunday – [0, 6]    (public member object) 
int tm_yday	//days since January 1 – [0, 365]  
int tm_isdst	//Daylight Saving Time flag. The value is positive if DST is in effect, zero if
                       //not and negative if no information is available    (public member object)

Notes

@1 Range allows for a positive leap second.

mktime

time_t mktime (tm * timeptr)

Takes a tm structure pointer and returns a time_t ie. turns hms dmy into an integer seconds

The standard C function localtime

 struct tm * localtime (const time_t * timer)

Takes a time_t structure and returns a tm structure pointer. ie turns a number of seconds into the hms dmy tm format in local time

The standard C function gmtime

 int struct tm * gmtime (const time_t * timer)

Takes a time_t structure and returns a tm structure pointer. ie turns a number of seconds into the hms dmy tm format in gmt

This one will do me. My little projects are unlikely to fly around the world

The standard C function strftime

 size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr )

ptr a pointer to a string to hold the formatted output
maxsize maximum size of string including the null terminator
format a pointer to a format sring
timeptr a pointer to a tm structure

return the number of character in the string not counting the null terminator

The standard C function asctime

 char* asctime (const struct tm * timeptr)

timeptr pointer to a tm structure containing the data to be converted.

return pointer to a string


7 comments on C/C++ Time functions & Structures:

29 Sep 2013

Hello,

coud you please fix all code posted here? The syntax is <<code>> <</code>> I believe both must be on a separate line.

29 Sep 2013

Martin Kojtal wrote:

Hello,

coud you please fix all code posted here? The syntax is <<code>> <</code>> I believe both must be on a separate line.

Thank you for the tip Martin. I must write out 100 times
"I must RTFM before I jump in boots and all""
Made it much better
thanks

30 Sep 2013

Click in the comment box on editing tips, there it shows the correct syntax, now it still isn't displaying properly ;)

01 Oct 2013

Erik - wrote:

Click in the comment box on editing tips, there it shows the correct syntax, now it still isn't displaying properly ;)

At least part of my trouble seems to be not knowing how it should look, The other part of my problem is that I seem to have done it according to the editing tips.

But thanks for the tip. cheers

01 Oct 2013

struct tm * localtime (const time_t * timer)

That is how it should look.


<<code>> struct tm * localtime (const time_t * timer) <</code>>

01 Oct 2013

Erik - wrote:

struct tm * localtime (const time_t * timer)

That is how it should look.


<<code>> struct tm * localtime (const time_t * timer) <</code>>

AHA ! Now I seem to have it. Much thanks for the assistance. And also to those above who tired to educate me but ultimately failed. though it must be said, though not through any fault of their own. I'm getting slower as I age. Now to review it all and making it comprehensible. Off to beddy byes now though.

Much thanks to all.

29 May 2016

I've encountered strange behaviour (at least for me). For development I am setting RTC timestamp to zero:

int main()
    set_time(0);

Then, I am storing timestamp in struct defined:

struct __attribute__((packed)) Entry {
    uint32_t timestamp;
    float args[5];
    int16_t flags;
    int16_t mask;
    char pad[4];
};

void gatherData(Entry &e)
{
    for (int i=0; i <5; i++)
        e.args[i] = getArgs([i]);
    e.timestamp = time(NULL);
    e.flags = getFlags();
    e.mask = getMask();
    e.pad[0] = 0xAA;
    e.pad[1] = 0x14;
    e.pad[2] = 0xF8;
    e.pad[3] = 0xBA;
}

Then I send this structure over the network to a new, better home, and... What do we have in timestamp field? 12 F4 92 B4

Of course it is incremented every second, but I'd expect something near zero, not in mid-2000's

Please log in to post comments.