Convert local time to calendar time
#include <time.h> time_t mktime( struct tm* timeptr );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The mktime() function converts the local time information in the struct tm specified by timeptr into a calendar time (Coordinated Universal Time) with the same encoding used by the time() function.
The original values of the tm_sec, tm_min, tm_hour, tm_mday and tm_mon fields aren't restricted to the ranges described for struct tm. If these fields aren't in their proper ranges, they're adjusted so that they are. Values for the fields tm_wday and tm_yday are computed after all the other fields have been adjusted.
The original value of tm_isdst is interpreted as follows:
Whenever mktime() is called, the tzset() function is also called.
The converted calendar time, or -1 if mktime() can't convert it.
#include <stdio.h> #include <stdlib.h> #include <time.h> static const char *week_day[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; int main( void ) { struct tm new_year; time_t t; new_year.tm_year = 2001 - 1900; new_year.tm_mon = 0; new_year.tm_mday = 1; new_year.tm_hour = 0; new_year.tm_min = 0; new_year.tm_sec = 0; new_year.tm_isdst = 0; t = mktime( &new_year ); if ( t == (time_t)-1) printf("No conversion possible.\n"); else printf( "The 21st century began on a %s.\n", week_day[ new_year.tm_wday ] ); return EXIT_SUCCESS; }
produces the output:
The 21st century began on a Monday.
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
asctime(), asctime_r(), clock(), ctime(), ctime_r(), difftime(), gmtime(), gmtime_r(), localtime(), localtime_r(), strftime(), time(), tm, tzset()