Header File
time.h
Category
Time and Date Routines
Prototype
time_t mktime(struct tm *t);
Description
Converts time to calendar format.
Converts the time in the structure pointed to by t into a calendar time with the same format used by the time function. The original values of the fields tm_sec, tm_min, tm_hour, tm_mday, and tm_mon are not restricted to the ranges described in the tm structure. If the fields are not in their proper ranges, they are adjusted. Values for fields tm_wday and tm_yday are computed after the other fields have been adjusted.
The allowable range of calendar times is Jan 1 1970 00:00:00 to Jan 19 2038 03:14:07.
Return Value
On success, mktime returns calendar time as described above.
On error (if the calendar time cannot be represented), mktime returns -1.
Example
#include <stdio.h> #include <time.h> char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Unknown"}; int main(void) { struct tm time_check; int year, month, day; /* Input a year, month and day to find the weekday for */ printf("Year: "); scanf("%d", &year); printf("Month: "); scanf("%d", &month); printf("Day: "); scanf("%d", &day); /* load the time_check structure with the data */ time_check.tm_year = year - 1900; time_check.tm_mon = month - 1; time_check.tm_mday = day; time_check.tm_hour = 0; time_check.tm_min = 0; time_check.tm_sec = 1; time_check.tm_isdst = -1; /* call mktime to fill in the weekday field of the structure */ if (mktime(&time_check) == -1) time_check.tm_wday = 7; /* print out the day of the week */ printf("That day is a %s\n", wday[time_check.tm_wday]); return 0; }
Portability
POSIX |
Win32 |
ANSI C |
ANSI C++ |
+ |
+ |
+ |
+ |
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|