打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C获取本地时间的小问题
分类: c/c++ c/c++ 2010-08-26 21:00 512人阅读 评论(0) 收藏 举报

最近有朋友问如下问题:

#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <time.h>

int main()
{
struct tm *tms;
time_t now = time(0);
tms = localtime(&now);

printf("%d/n", tms->tm_year);
printf("%d/n", tms->tm_mon);
printf("%d/n", tms->tm_mday);
printf("%d/n", tms->tm_hour);
printf("%d/n", tms->tm_min);
printf("%d/n", tms->tm_sec);
}为什么输出的年是110,不是2010?输出的月不是8月而是7月?

这个问题在MSDN上解释的很清楚了,我不用罗嗦了,直接拿出证据吧。

Return Value

localtime returns a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, localtime returns NULL. The fields of the structure type tm store the following values, each of which is an int:

tm_sec

Seconds after minute (0 – 59)

tm_min

Minutes after hour (0 – 59)

tm_hour

Hours after midnight (0 – 23)

tm_mday

Day of month (1 – 31)

tm_mon

Month (0 – 11; January = 0)     //请注意这里

tm_year

Year (current year minus 1900) //还要注意这里

tm_wday

Day of week (0 – 6; Sunday = 0)

tm_yday

Day of year (0 – 365; January 1 = 0)

tm_isdst

Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST).

Parameter

timer

Pointer to stored time

Remarks

The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). This value is usually obtained from the time function.

gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call.

localtime corrects for the local time zone if the user first sets the global environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime.

Note   The target environment should try to determine whether daylight saving time is in effect.

Example

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(一)linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒)_鲍...
c语言中时间的获取
Linux C函数之时间函数
Linux常用C函数-日期时间篇 -
linux时间函数总结,特别有无时区转换
Linux中gmtime和localtime的区别 通过time()函数获取时间
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服