The meaning of each is:
MemberMeaningRange
tm_sec
seconds after
the minute
0-61*
tm_min
minutes after the hour
0-59
tm_hour
hours
since midnight
0-23
tm_mday
day of the month
1-31
tm_mon
months
since January
0-11
tm_year
years since 1900
tm_wday
days
since Sunday
0-6
tm_yday
days since January 1
0-365
tm_isdst
Daylight
Saving Time flag
下面程序计算美国凤凰城 (-7),中国北京(+8),和冰岛(0)时间:
#include
#include
struct {
int tm_min;
int
tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int
tm_yday;
int tm_isdst;
}tm;
void show(struct tm * timeinfo,int
j)
{
int day,hour;
if(j>0)
{
if((j+timeinfo->tm_hour)/24==1)
day=timeinfo->tm_mday+1;
else
day=timeinfo->tm_mday;
hour=(j+timeinfo->tm_hour)%24;
}
if(j==0)
{
day=timeinfo->tm_mday;
hour=(j+timeinfo->tm_hour)%24;
}
if(j<0)
{
if((j+timeinfo->tm_hour)/24<0)
day=timeinfo->tm_mday-1;
else
day=timeinfo->tm_mday;
hour=(j+timeinfo->tm_hour)%24;
}
printf
( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeinfo->tm_year,
1+timeinfo->tm_mon,
day,hour,timeinfo->tm_min,timeinfo->tm_sec);
}
void
main ()
{
time_t timer;
struct tm * timeinfo,*ptm;
time (
&timer );//Pointer to an object of type time_t, where the time value
is stored.
timeinfo = localtime ( &timer);//返回当地时区时间
ptm =
gmtime ( &timer );//返回格林制时区时间
printf("按格式显示冰岛时间\n");
show(ptm,0);
printf("按格式显示美国凤凰城时间\n");
show(ptm,-7);
printf("按格式显示中国北京时
间\n");
show(ptm,8);
}
按格式显示冰岛时间
2009-09-24 19:26:30
按格式显
示美国凤凰城时间
2009-09-24 12:26:30
按格式显示中国北京时间
2009-09-25 03:26:30
......
阅读(6712) | 评论(0) | 转发(0) |