Linux下时间的概念及处理
(UTC):称为世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。比如,中国内地的时间与UTC的时差为+8,也就是UTC+8。美国是UTC-5。
Calendar Time:日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。
在unix/linux系统中,时间的表示方法是以1970年1月1日00:00:00所经过的秒数,使用基本系统数据类型time_t表示,在/usr/include下查找time_t类型的定义.
32位的time_t最迟能表示到2038年1月19日 11:14:07(Asia/Shanghai时间) 或2038年1月19日 03:14:07(GMT时间),再过1秒,time_t数据将变为负数,变为1901年12月14日 04:51:44(本地时间),或1901年12月13日 20:45:52(GMT时间).
struct tm
#include<time.h> struct tm { int tm_sec; /*代表目前秒数,正常范围为0-59,但允许至61秒 */ int tm_min; /*代表目前分数,范围0-59*/ int tm_hour; /*从午夜算起的时数,范围为0-23*/ int tm_mday; /*目前月份的日数,范围1-31*/ int tm_mon; /*代表目前月份,从一月算起,范围从0-11*/ int tm_year; /*从1900 年算起至今的年数,其值等于实际年份减去1900*/ int tm_wday; /*一星期的日数,从星期一算起,范围为0-6 ,其中0代表星期天,1代表星期一,以此类推*/ int tm_yday; /*从今年1月1日算起至今的天数,范围为0-365*/ int tm_isdst; /*日光节约时间的旗标,实行夏令时的时候,tm_isdst为正,不实行夏令时的时候, tm_isdst为0,不了解情况时,tm_isdst()为负。*/ };
time()
#ifndef _TIME_T_DEFINED typedef long time_t; /* time value */ #define _TIME_T_DEFINED /* avoid multiple defines of time_t */ #endif
#include <stdio.h> #include <stddef.h> #include <time.h> int main(void) { time_t timer;//time_t就是long int 类型 struct tm *tblock; timer = time(NULL);//这一句也可以改成time(&timer); tblock = localtime(&timer); printf("Local time is: %s\n",asctime(tblock)); return 0; }
#include<time.h> #include<stdio.h> int main() { struct tm *t; time_t tt; time(&tt); t=localtime(&tt); printf("%4d年%02d月%02d日 %02d:%02d:%02d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec); return 0; }
gmtime()
#include "stdio.h" #include "time.h" #include "stdlib.h" int main(void) { time_t t; struct tm *gmt, *area; tzset(); /* tzset()*/ t = time(NULL); area = localtime(&t); printf("Local time is: %s", asctime(area)); gmt = gmtime(&t); printf("GMT is: %s", asctime(gmt)); return 0; }
matime()
<span style="color:#000000;"></span><ol class="dp-cpp" start="1"><ol class="dp-cpp" start="1"><li class="alt"><span><span class="preprocessor">#include <stdio.h></span><span> </span></span></li><li><span><span class="preprocessor">#include <time.h></span><span> </span></span></li><li class="alt"><span><span class="comment">/**</span> </span></li><li><span><span class="comment"> * str为日期字符串</span> </span></li><li class="alt"><span><span class="comment"> * formatStr 为时间对应的格式,</span> </span></li><li><span><span class="comment"> * 如2012-07-04 15:33:52对应的格式为%d-%d-%d %d:%d:%d</span> </span></li><li class="alt"><span><span class="comment"> */</span><span> </span></span></li><li><span><span class="datatypes">time_t</span><span> string2time(</span><span class="keyword">const</span><span> </span><span class="datatypes">char</span><span> * str,</span><span class="keyword">const</span><span> </span><span class="datatypes">char</span><span> * formatStr) </span></span></li><li class="alt"><span>{ </span></li><li><span> <span class="keyword">struct</span><span> </span><span class="datatypes">tm</span><span> tm1; </span></span></li><li class="alt"><span> <span class="datatypes">int</span><span> year,mon,mday,hour,min,sec; </span></span></li><li><span> <span class="keyword">if</span><span>( -1 == sscanf(str,formatStr,&year,&mon,&mday,&hour,&min,&sec)) </span><span class="keyword">return</span><span> -1; </span></span></li><li class="alt"><span> tm1.tm_year=year-1900; </span></li><li><span> tm1.tm_mon=mon-1; </span></li><li class="alt"><span> tm1.tm_mday=mday; </span></li><li><span> tm1.tm_hour=hour; </span></li><li class="alt"><span> tm1.tm_min=min; </span></li><li><span> tm1.tm_sec=sec; </span></li><li class="alt"><span> <span class="keyword">return</span><span> mktime(&tm1); </span></span></li><li><span>} </span></li><li class="alt"><span><span class="datatypes">int</span><span> main(</span><span class="keyword">void</span><span>) { </span></span></li><li><span> <span class="datatypes">time_t</span><span> t1=string2time(</span><span class="string">"2012-07-04 15:33:52"</span><span>,</span><span class="string">"%d-%d-%d %d:%d:%d"</span><span>); </span></span></li><li class="alt"><span> <span class="keyword">if</span><span>(t1 == -1) </span></span></li><li><span> { </span></li><li class="alt"><span> fputs(<span class="string">"convert error!\n"</span><span>,stderr); </span></span></li><li><span> exit(1); </span></li><li class="alt"><span> } </span></li><li><span> <span class="datatypes">char</span><span> buf[20]={0}; </span></span></li><li class="alt"><span> strftime(buf,<span class="keyword">sizeof</span><span>(buf),</span><span class="string">"%F %T"</span><span>,localtime(&t1)); </span></span></li><li><span> printf(<span class="string">"%s\n"</span><span>,buf); </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> 0; </span></span></li><li><span>} </span></li></ol><li><span> </span></li></ol>时间格式输出
matime() ctime()
#include <time.h> #include <stdio.h> int main(void) { struct tm *ptr; time_t lt; lt =time(NULL); ptr=gmtime(&It); printf(asctime(ptr)); printf(ctime(&It)); return 0; } 运行结果: Sat Jul 30 08:43:03 2005 Sat Jul 30 16:43:03 2005
我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。
函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。
%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
struct tm tm1;
tm1 = localtime(&now);
strftime(buf, sizeof(buf), "%A %b %d %T %Z %Y", tm1);
difftime()
有时候在实际应用中要计算一个事件持续的时间长度,比如计算打字速度。在第1节计时部分中,我已经用clock函数举了一个例子。Clock()函数可以精确到毫秒级。同时,我们也可以使用difftime()函数,但它只能精确到秒。该函数的定义如下:
double difftime(time_t time1, time_t time0);
虽然该函数返回的以秒计算的时间间隔是double类型的,但这并不说明该时间具有同double一样的精确度,这是由它的参数决定的(time_t是以秒为单位计算的)。比如下面一段程序:
<span style="font-size:14px;">#include "time.h" #include "stdio.h" #include "stdlib.h" int main(void) { time_t start,end; start = time(NULL); system("pause"); end = time(NULL); printf("The pause used %f seconds.\n",difftime(end,start));//<- system("pause"); return 0; } 运行结果为: 请按任意键继续. . . The pause used 2.000000 seconds. 请按任意键继续. . . 可以想像,暂停的时间并不那么巧是整整2秒钟。其实,你将上面程序的带有“//<-”注释的一行用下面的一行代码替换: printf("The pause used %f seconds.\n",end-start); ? 其运行结果是一样的。</span>
时区函数:setenv("TZ", "GMT+8", 1)
刷时区:tzset()
整形转字符串:sprintf(hour, "%d", tm1->tm_hour);