本文转自 http://blog.csdn.net/chenyiming_1990/article/details/8682552
time.h
有人总结成这么几句,的确是经典,自己好好编程试试效果吧,
两个类型:
time_t:表示距离 UTC 时间 1970-01-01 00:00:00 的秒数。也叫做日历时,类型是 long
clock_t: 只用于程序计时,貌似其他的没它什么事。
struct tm:通常用于存储本地时。
常用函数
:
clock: 获取程序开始执行后占用的处理器时间,返回值clock_t。
time:获取当前系统时间(UTC时间)的time_t值。
ctime:将time_t值转换为表示本地时间的字符串。
gmttime:将time_t值转换为表示GMT时间的字符串。
localtime:将time_t转换为表示本地时间的strunct tm结构。
mktime:将表示本地时间的struct tm转换为time_t。
asctime:将struct tm转换为字符串形式。
difftime:得到两个日历时之间的差值。
strftime:自定义把结构体tm的日期与时间信息转换为制定的格式。
========================================================================
@函数名称: clock
函数原型:
clock_t clcok()
函数功能: 获取程序开始执行后占用的处理器时间
函数返回: 一般
返回值/CLOCK_PER_SEC
来已秒来表示时间
@函数名称: localtime
函数原型:
struct tm *localtime(const time_t *timer)
函数功能: 返回一个以tm结构表达的机器时间信息
函数返回: 以tm结构表达的时间,结构tm定义如下:
-
struct
tm
-
{
-
int
tm_sec;
/* Seconds: 0-59 (K&R says 0-61?) */
-
int
tm_min;
/* Minutes: 0-59 */
-
int
tm_hour;
/* Hours since midnight: 0-23 */
-
int
tm_mday;
/* Day of the month: 1-31 */
-
int
tm_mon;
/* Months *since* january: 0-11 */
-
int
tm_year;
/* Years since 1900 */
-
int
tm_wday;
/* Days since Sunday (0-6) */
-
int
tm_yday;
/* Days since Jan. 1: 0-365 */
-
int
tm_isdst;
/* +1 Daylight Savings Time, 0 No DST,* -1 don’t know */
-
};
参数说明: timer-使用time()函数获得的机器时间
-
#include <time.h>
-
#include <stdio.h>
-
#include <dos.h>
-
int
main()
-
{
-
time_t
timer;
-
struct
tm
*tblock;
-
timer=time(NULL);
-
tblock=localtime(&timer);
-
printf(
“Local time is: %s”
,asctime(tblock));
-
return
0;
-
}
@函数名称: asctime
函数原型:
char* asctime(struct tm * ptr)
函数功能: 得到机器时间(日期时间转换为ASCII码)
函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到
-
#include <stdio.h>
-
#include <string.h>
-
#include <time.h>
-
int
main()
-
{
-
struct
tm
t;
//通过自己为每个tm成员赋值,没有什么实际意思,大多数情况下都是通过系统函数计算time_t来得到tm中的各个数值
-
char
str[80];
-
t.tm_sec=1;
-
t.tm_min=3;
-
t.tm_hour=7;
-
t.tm_mday=22;
-
t.tm_mon=11;
-
t.tm_year=56;
-
t.tm_wday=4;
-
t.tm_yday=0;
-
t.tm_isdst=0;
-
strcpy(str,asctime(&t));
-
printf(
“%s”
,str);
-
return
0;
-
}
@函数名称: ctime
函数原型:
char *ctime(const time_t *tp)
函数功能: 得到日历时间
函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函数time获得 等同于 astime( localtime(tp) )
-
#include <stdio.h>
-
#include <time.h>
-
int
main()
-
{
-
time_t
t;
-
time(&t);
-
printf(
“Today’s date and time: %s”
,ctime(&t));
-
return
0;
-
}
@函数名称: difftime
函数原型:
double difftime(time_t time2, time_t time1)
函数功能: 得到两次机器时间差,单位为秒
函数返回: 时间差,单位为秒
参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得
-
#include <time.h>
-
#include <stdio.h>
-
#include <dos.h>
-
#include <conio.h>
-
int
main()
-
{
-
time_t
first, second;
-
System(
“cls”
);
-
first=time(NULL);
-
delay(2000);
-
second=time(NULL);
-
printf(
“The difference is: %f seconds”
,difftime(second,first));
-
getch();
-
return
0;
-
}
@函数名称: gmtime
函数原型:
struct tm *gmtime(time_t *time)
函数功能: 得到以结构tm表示的时间信息
函数返回: 以结构tm表示的时间信息指针
参数说明: time-用函数time()得到的时间信息
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <time.h>
-
#include <dos.h>
-
-
char
*tzstr=
“TZ=PST8PDT”
;
-
int
main()
-
{
-
time_t
t;
-
struct
tm
*gmt, *area;
-
putenv(tzstr);
-
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;
-
}
@函数名称: time
函数原型:
time_t time(time_t *timer)
函数功能: 得到机器的日历时间或者设置日历时间
函数返回: 机器日历时间
参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
-
#include <time.h>
-
#include <stdio.h>
-
#include <dos.h>
-
int
main()
-
{
-
time_t
t;
-
t=time(NULL);
-
printf(
“The number of seconds since January 1,1970 is %ld\n”
,t);
-
return
0;
-
}
@函数名称: tzset
函数原型:
void tzset(void)
函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
函数返回:
参数说明:
-
#include <time.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
int
main()
-
{
-
time_t
td;
-
putenv(
“TZ=PST8PDT”
);
-
tzset();
-
time(&td);
-
printf(
“Current time=%s”
,asctime(localtime(&td)));
-
return
0;
-
}
@函数名称: strftime
函数原型:
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
函数功能: 根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向 strDest中存放maxsize个字符。
该函数返回向strDest指向的字 符串中放置的字符数。
参数说明: 转化结果存在s中,最多maxsize个字符写到s中
函数返回: 写到s中的字符数(不包括’\0’),如果字符数多于ssizemax,函数返回0.
-
/*
-
类似于sprintf(),识别以百分号(%)开始 的格式命令集合,格式化输出结果放在一个字符串中。格式命令列在下面,它们是区分大小写的。
-
%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 时区名称,如果不能得到时区名称则返回空字符。
-
%% 百分号
-
*/
-
-
#include <stdio.h>
-
#include <time.h>
-
void
main()
-
{
-
struct
tm
*newtime;
-
char
tmpbuf[128];
-
time_t
lt1;
-
time( <1 );
-
newtime=localtime(<1);
-
strftime( tmpbuf, 128,
“Today is %A, day %d of %B in the year %Y.\n”
, newtime);
-
printf(tmpbuf);
-
-
return
0;
-
}
-
-
-
/*
-
运行结果:
-
Today is Saturday, day 30 of July in the year 2005.
-
*/
=================================================================================
知识点:
1.Coordinated Universal Time(UTC):
协调世界时,又称世界标准时间,也即格林威治标准时间(Greenwich Mean Time,GMT),中国内地的时间与UTC得时差为+8,也即UTC+8,美国为UTC-5。
2.Calendar Time:
日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。标准时间点对不同编译器可能会不同,但对一个编译系统来说,标准时间是不变的。
3.epoch:时间点。
在标准c/c++中是一个整数,用此时的时间和标准时间点相差的秒数(即日历时间)来表示
4.clock tick:
时钟计时单元(而不叫做时钟滴答次数),一个时钟计时单元的时间长短是由cpu控制的,一个clock tick不是cpu的一个时钟周期,而是c/c++的一个基本计时单位。
5.计时函数:
clock_t clock(void);//返回值:从“开启这个程序进程”到“程序中调用clock()函数”时之间的cpu时钟计时单元(clock tick)数,MSDN中称为挂钟时间(wal-clock)
-
//time.h
-
#ifndef _CLOCK_T_DEFINED
-
typedef
long
clock_t
;
-
#define _CLOCK_T_DEFINED
-
#endif
-
//time.h
-
#define CLOCKS_PER_SEC ((clock_t)1000) //CLOCK_PER_SEC表示一秒会有多少个时钟计时单元,标准c/c++中,最小的计时单位是一毫秒,
-
//可通过clock()/CLOCK_T_SEC得到进程自身运行时间
6.存储时间和日期的数据结构tm:
-
struct
tm
-
{
-
int
tm_sec;
//秒,[0-59]
-
int
tm_min;
//分,[0-59]
-
int
tm_hour;
//时,[0-23]
-
int
tm_mday;
//一个月中的日期,[1-31]
-
int
tm_mon;
//月份,[0-11]
-
int
tm_year;
//年份,其值为实际年份-1900
-
int
tm_wday;
//星期,[0-6]
-
int
tm_yday;
//从每年的1月1日开始计数,[0-365]
-
int
tm_isdst;
//夏令标识符,实行夏令时,tm_isdst为正;不实行夏令时,tm_isdst为0;不了解情况时,tm_isdst为负
-
};
-
#define _TM_DEFINED
-
#endif
7.日历时间用time_t存储:
-
//time.h
-
#ifndef _TM_DEFINED
-
//time.h
-
#ifndef _TIME_T_DEFINED
-
typedef
long
time_t
;
-
#define _TIME_T_DEFINED
-
#endif
使用time_t表示的日历时间只能存储到2038年1月18日19时14分07秒,为了能表示更久远的时间,一些编译器引入了64位甚至更长的整形数保 存日历时间,如VC中采用_time64_t数据类型,通过_time64()函数获得日历时间(不是32位的time())这样可以计算3001年1月 1日0时0分0秒(不包括该时间点)之前的时间。