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 ...
随机推荐
- winform timespan 两个时间的间隔(差) 分类: WinForm 2014-04-15 10:14 419人阅读 评论(0) 收藏
TimeSpan 结构 表示一个时间间隔. 先举一个小例子:(计算两个日期相差的天数) 代码如下: DateTime dt = DateTime.Now.ToShortDateString(yyyy ...
- Demo_塔防(自动生成怪物,导航,炮塔攻击,怪物掉血死忙)
using UnityEngine; using System.Collections; public struct WaveMsg { //该波次生成的怪物 public GameObject mo ...
- android编程中setLayoutParams方法设置
第一篇 private LinearLayout generateHeadOfControl() { LinearLayout LayoutHead = createLayout(LinearLayo ...
- Bernese单点定位数据准备及处理
原创作者 blog :http://yifeiyao.blog.163.com/blog/static/2058932752012669731170/1.准备所需用的数据文件,如下: 原始观测.O文件 ...
- COGS 渡轮问题 (LIS规定字典序输出方案数)
/* 下标字典序最小 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1 ...
- jquery/js当前URL对当前栏目高亮突出显示
html: 1 <div class="nav"> 2 <ul> 3 <li><a href="index.html" ...
- jquery获取元素到屏幕底的可视距离
jquery获取元素到屏幕底的可视距离 要打对号的图里的height(我自称为可视高度:滚动条未滑到最底端) 不是打叉图里的到页面底部(滚动条到最底部时的height)(offset().top方法 ...
- Android中Application全局方法(变量)的调用
Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象,用来存储系统的一些信息.通常我们是 ...
- memcache的安装及管理
一.Memcache概述 Memcache(内存,缓存):是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个巨大的hash表.(key=value)(是用C语言开发的,并且需要libeven ...
- solr建立索引的过程
HttpSolrServer HttpSolrServer继承SolrServer 参考文档:http://my.oschina.net/qige/blog/173008