C获取本地时间的localtime函数
最近有朋友问如下问题:
#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
http://blog.csdn.net/yiruirui0507/article/details/5841837
C获取本地时间的localtime函数的更多相关文章
- Linux C 中获取local日期和时间 time()&localtime()函数
1. time() 函数 /* time - 获取计算机系统当前的日历时间(Calender Time) * 处理日期时间的函数都是以本函数的返回值为基础进行运算 * * 函数原型: * #incl ...
- linux c获取本地时间
在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值区间 ...
- C/C++获取本地时间常见方法
跨平台方法 方法一:手动暴力法 #include <iostream> using namespace std; #include <time.h> time_t t = ti ...
- PHP获取本地时间
使用date ( "Y-m-d H:i:s" )函数获取当前时间,总是不对,原来默认是格林威治时间,解决的办法有两个: 1.获取之前先加 date_default_timezo ...
- python获取本地时间
python本地时间 import time # 格式化成2016-03-20 11:45:39形式 now = time.strftime("%Y-%m-%d %H:%M:%S" ...
- python获取本地时间,时间戳与日期格式相互转换
附上代码与运行结果截图: import time # 获取当前时间 now = time.localtime() # 格式化日期 now_ = time.strftime('%Y-%m-%d %H:% ...
- iOS获取本地时间
NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...
- 利用JS获取本地时间和服务器时间
<p id="labTime"> <script type="text/javascript"> //取客户端时间 setInterva ...
- JavaScript—获取本地时间以12小时制显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- java 服务端解决ajax跨域问题
//过滤器方式 可以更改为拦截器方式public class SimpleCORSFilter implements Filter { public void doFilter(ServletRequ ...
- QT的信号与槽机制介绍
信号与槽作为QT的核心机制在QT编程中有着广泛的应用,本文介绍了信号与槽的一些基本概念.元对象工具以及在实际使用过程中应注意的一些问题. QT是一个跨平台的C++ GUI应用构架,它提供了丰富的窗 ...
- Java 7之多线程- Semaphore--转载
Semaphore用于保存当前可用许可的数量.是通过共享锁实现的.根据共享锁的获取原则,Semaphore分为"公平信号量"和"非公平信号量". "公 ...
- 基于库zkclient 的leader选举代码实现
利用了zookeeper临时节点,在当连接或session断掉时被删除这一特性来做选举.(简单简单互斥锁) 查了下网上的做法. 大致流程: <1>判定是否存在/wzgtest路径 < ...
- Android开发技巧——去掉TextView中autolink的下划线
我们知道,在布局文件中设置textview的autolink及其类型,这时textivew上会显示link的颜色,并且文字下面会有一条下划线,表示可以点击.而在我们在点击textview时,应用将根据 ...
- java中判断两个字符串是否相等的问题
我最近刚学java,今天编程的时候就遇到一个棘手的问题,就是关于判断两个字符串是否相等的问题.在编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的是eq ...
- MVC部署出现HTTP 404 错误
asp.net mvc部署出现问题,http错误404.0,报错如下图: 在网上找了好多方法都不行.最后我的解决方案是: 打好这个补丁就行了http://support.microsoft.com/k ...
- ASPNET5 诊断
1. 配置一个错误的处理页 在ASP.NET5, 可以在Startup的Configure里配置一个错误处理页,对于开发来说,非常简单,只要增加Microsoft.AspNet.Diagnostics ...
- 读写Excel
有读Excel,也有生成相同格式的Excel.需要引用Microsoft.Office.Interop.Excel.dll public string ShiPin() { //获取项目下的目录 st ...
- 016_openxml_forxml
016_openxml_forxml --openxml*********************************************************************** ...