#include <time.h>struct tm *localtime(const time_t *time);

The localtime( ) function returns a pointer to the broken-down form of time in the form of a tm structure. The time is represented in local time. The time pointer is usually obtained through a call to time( ).

Programming Tip 

The mktime( ) function is especially useful when you want to know on which day of the week a given date falls. For example, what day of the week is Jan 12, 2012? To find out, call the mktime( ) function with that date and then examine the tm_wday member of the tm structure after the function returns. It will contain the day of the week. The following program demonstrates this method:

/* Find day of week for January 12, 2012. */ #include <stdio.h> #include <time.h> char day[][20] = {   "Sunday",    "Monday",   "Tuesday",   "Wednesday",   "Thursday",   "Friday",   "Saturday" }; int main(void) {   struct tm t;   t.tm_mday = 12;   t.tm_mon = 0;   t.tm_year = 112;   t.tm_hour = 0;   t.tm_min = 0;   t.tm_sec = 0;   t.tm_isdst = 0;   mktime(&t); /* fill in day of week */   printf("Day of week is %s.\n", day[t.tm_wday]);   return 0; }

When this program is run, mktime( ) automatically computes the day of the week, which is Thursday in this case. Since the return value of mktime( ) is not needed, it is simply ignored.

The structure used by localtime( ) to hold the broken-down time is statically allocated and is overwritten each time the function is called. If you want to save the contents of the structure, you must copy it elsewhere.

Related functions are gmtime( ), time( ), and asctime( ).

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539 © 2008-2017.
If you may any questions please contact us: