1. the difference between UTC and Local Time;
UTC: the abbreviation for Universal Time Coordinated, as the same as GMT, is the London local time.
GMT: the abbreviation for Greewich Mean Time. is the London local time.
UTC + Time Zone = Local Time;
eg 1: Bei Jing Time Zone is East 8 , so BeiJing Time = UTC + ( + 0800 ); New York Time Zone is West 5, so New York Time = UTM + ( - 0500 );
so BeiJing Time is 13 hour first than New York Time;
2. two struct to time data
time_t : calendar time, when interpreted as an absoluted time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970.
tm: broken-down time, is a representation separated into year, month, day, etc;
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
3. functions take an argument of data type time_t;
char *ctime(const time_t *timep);
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
this function change the UTC to local time, interpresent as break-down format. but, it should notice thant the tm_mon should add 1, because the month
is begin with 0; and tm_year should add 1900, because the year is begin with 1900;
also you can converse struct *tm to time_t by function mktime( );
if you reference lacaltime() frequently, you can manually call tzset( ) to read the time zone from file to memory.
time(const time_t *timep);
4. functions take an argument of data type struct tm;
char *asctime(const struct tm *tm);
time_t mktime(struct tm *tm);
int main()
{
time_t cur_time;
struct tm *t, *p;
time(&cur_time);
t = gmtime(&cur_time);
p = localtime(&cur_time);
printf("time 1: %d/%d/%d,%d, %d:%d:%d\n",(1900+ t->tm_year),(1+ t->tm_mon), t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec);
printf("time 2: %s\n", ctime(&cur_time));
printf("time 3: %d/%d/%d,%d, %d:%d:%d\n",(1900+ p->tm_year),(1+ p->tm_mon), p->tm_mday, p->tm_wday, p->tm_hour, p->tm_min, p->tm_sec);
printf("time 4: %s\n", asctime(t));
printf("time 5: %d, %d\n", cur_time, mktime(t));
return 0;
}
result:
time 1: 2011/9/23,5, 12:28:8
time 2: Fri Sep 23 12:28:08 2011
time 3: 2011/9/23,5, 12:28:8
time 4: Fri Sep 23 12:28:08 2011
time 5: 1316795288, 1316795288
5. function of time format
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
format the broken-down time "tm" according to the format specification "format", and place the result in the character array "s" of size max;
char *strptime(const char *s, const char *format, struct tm *tm);
converts the character string pointed to by "s" to value which are stored in the tm struct pointed to by tm, using the format specified by "format".