130 likes | 378 Views
Unix Time Functions. CNS 3210. The basic Linux (and Unix) time service counts the number of seconds that have passed since January 1, 1970 (UTC). These seconds are normally stored in a variable whose type is time_t. This is called calendar time. We can get calendar time by using the call.
E N D
Unix Time Functions CNS 3210
The basic Linux (and Unix) time service counts the number of seconds that have passed since January 1, 1970 (UTC). These seconds are normally stored in a variable whose type is time_t. This is called calendar time.
We can get calendar time by using the call time_t time (time_t *tptr); returns the time, and stores the value here if the argument is non-null.
string formatted string asctime strftime struct tm broken down time ctime localtime gmtime mktime time_t calendar time time kernel takes time zone into account
struct tm struct tm { int tm_sec; // seconds after the minute [0-61] int tm_min; // minutes after the hour [0-59] int tm_hour; // hours after midnight [0-23] int tm_mday; // day of the month [1-31] int tm_mon; // month of the year [0-11] int tm_year; // years since 1900 int tm_wday; // day of the week [0-6] int tm_yday; // days since Jan 1 [0-365] int tm_isdst // daylight savings flag [pos, 0, neg] }; allows leap seconds dst stt not avail
time_t to tm #include <time.h> struct tm *gmtime (const time_t *tptr); struct tm *localtime (const time_t *tptr); broken down time is in UTC broken down time is in local time, given time zone and daylight savings
tm to time_t time_t mktime (struct tm *tptr);
generating time strings char *asctime (const struct tm *tmptr); char (ctime (const time_t *tptr); both generate a 26 character string of the form Tue Sep 26 16:49:05 2002\n\0
Generating a formatted string size_t strftime (char *tbuf, size_t maxsize, const char *format, const struct tm *tptr); the buffer to store the string in, and its max size a broken down time the format string – works like printf returns the size of the formatted string if successful 0 if fails
Format Description Example %a abbreviated weekday name Tue %A full weekday name Tuesday %b abbreviated month name Jan %B full weekday name January %c date and time Tue Jan 14 19:40:05 2002 %d day of the month [00-31] 14 %H hour of 24 hour day [00-23] 19 %I hour of 24 hour day [00-12] 07 %j day of the year [001-366] 014 %m month [01-12] 01 %M minute [00-59] 40 %p am or pm PM %S second [00-61] 05 %U Sunday week number [00-53] 02 %w weekday [0=Sunday – 6] 5 %W Monday week number [00-53] 07 %x date 01/14/02 %X time 19:40:04 %y year without century [00-99] 02 %Y year with century 2002 %Z time zone name MST