最近有朋友问如下问题:

#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.

gmtimemktime, 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

http://blog.csdn.net/yiruirui0507/article/details/5841837

C获取本地时间的localtime函数的更多相关文章

  1. Linux C 中获取local日期和时间 time()&localtime()函数

    1.  time() 函数 /* time - 获取计算机系统当前的日历时间(Calender Time) * 处理日期时间的函数都是以本函数的返回值为基础进行运算 * * 函数原型: * #incl ...

  2. linux c获取本地时间

    在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值区间 ...

  3. C/C++获取本地时间常见方法

    跨平台方法 方法一:手动暴力法 #include <iostream> using namespace std; #include <time.h> time_t t = ti ...

  4. PHP获取本地时间

    使用date ( "Y-m-d H:i:s" )函数获取当前时间,总是不对,原来默认是格林威治时间,解决的办法有两个: 1.获取之前先加   date_default_timezo ...

  5. python获取本地时间

    python本地时间 import time # 格式化成2016-03-20 11:45:39形式 now = time.strftime("%Y-%m-%d %H:%M:%S" ...

  6. python获取本地时间,时间戳与日期格式相互转换

    附上代码与运行结果截图: import time # 获取当前时间 now = time.localtime() # 格式化日期 now_ = time.strftime('%Y-%m-%d %H:% ...

  7. iOS获取本地时间

    NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...

  8. 利用JS获取本地时间和服务器时间

    <p id="labTime"> <script type="text/javascript"> //取客户端时间 setInterva ...

  9. JavaScript—获取本地时间以12小时制显示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. 爱加密Android APk 原理解析

    转载请标明出处:http://blog.csdn.net/u011546655/article/details/45921025 爱加密Android APK加壳原理解析 一.什么是加壳? 加壳是在二 ...

  2. android:Adb connection Error:远程主机强迫关闭了一个现有的连接

    用真机调试程序的时候,eclipse的console总是出现如下的错误“Adb connection Error:远程主机强迫关闭了一个现有的连接” 问题出现的原因:这是ddms调用adb引发的. 经 ...

  3. [转] git reset简介

    http://blog.csdn.net/hudashi/article/details/7664464 http://guibin.iteye.com/blog/1014369 http://hi. ...

  4. [Android]如何创建一个View的分割线

    如何创建一个View的分割线,如上图 我们见介绍三种可以创建看起来很不错的view的分割线,如在button之间添加分割线. 这个例子是将为LinearLayout内的三个Button间添加分割线. ...

  5. GridView的初级使用

    使用GridView自带的分页功能,需要激发PageIndexChanging protected void gvNewsList_PageIndexChanging(object sender, G ...

  6. (转)dedecms [field:array runphp='yes']标签使用技巧

    field支持用array获取任意字段的值:(支持标记:文章内容模板的 {dede:field name=’array’ /}.arclist.arclistsg.loop.sql 标签) 我们平时常 ...

  7. git针对Android Studio的使用

    1.将文件放到项目根目录下 .gitignore 文件内容: *.iml.gradle/local.properties/.idea/workspace.xml/.idea/libraries.DS_ ...

  8. 【转载】逃离adapter的地狱-针对多个View type的组合实现方案

    英文原文:JOE'S GREAT ADAPTER HELL ESCAPE 转载地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015 ...

  9. MySQL 连接

    MySQL 连接 使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@ ...

  10. 【USACO 2.2.4】派对灯

    [描述] 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄 ...