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:
Please log in to post comments.
Hello,
coud you please fix all code posted here? The syntax is
<<code>> <</code>>
I believe both must be on a separate line.