您的位置:首页 > 其它

C标准时间和日期函数

2013-12-13 10:45 127 查看

标准c时间与日期函数

1.数据结构

time_t----长整型__int64
clock_t----long
structtm{
inttm_sec;
/*secondsaftertheminute-[0,59]*/
inttm_min;
/*minutesafterthehour-[0,59]*/
inttm_hour;
/*hourssincemidnight-[0,23]*/
inttm_mday;
/*dayofthemonth-[1,31]*/
inttm_mon;
/*monthssinceJanuary-[0,11]*/
inttm_year;
/*yearssince1900*/
inttm_wday;
/*dayssinceSunday-[0,6]*/
inttm_yday;
/*dayssinceJanuary1-[0,365]*/
inttm_isdst;
/*daylightsavingstimeflag*/
};

2.函数

time

语法:


#include<time.h>

time_ttime(time_t*time);

功能:函数返回当前时间,如果发生错误返回零。如果给定参数time,那么当前时间存储到参数time中。

time函数返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

difftime

语法:


#include<time.h>

doubledifftime(time_ttime2,time_ttime1);

功能:函数返回时间参数time2和time1之差的秒数表示。

clock

语法:


#include<time.h>

clock_tclock(void);

功能:函数返回自程序开始运行的处理器时间,如果无可用信息,返回-1。转换返回值以秒记,返回值除以CLOCKS_PER_SECOND.(注:如果编译器是POSIX兼容的,CLOCKS_PER_SECOND定义为1000000.)

ctime

语法:


#include<time.h>

char*ctime(consttime_t*time);

功能:函数转换参数time为本地时间格式:

daymonthdatehours:minutes:secondsyear\n\0

ctime()等同

asctime(localtime(tp));

localtime

语法:


#include<time.h>

structtm*localtime(consttime_t*time);

功能:函数返回本地日历时间。

Watchout.

Thisfunctionreturnsavariablethatisstaticallylocated,andthereforeoverwritteneachtimethisfunctioniscalled.Ifyouwanttosavethereturnvalueofthisfunction,youshouldmanuallysaveitelsewhere.

Ofcourse,whenyousaveitelsewhere,youshouldmakesuretoactuallycopythevalue(s)ofthisvariabletoanotherlocation.Ifthereturnvalueisastruct,youshouldmakeanewstruct,thencopyoverthemembersofthestruct.


asctime

语法:


#include<time.h>

char*asctime(conststructtm*ptr);

功能:函数将ptr所指向的时间结构转换成下列字符串:

daymonthdatehours:minutes:secondsyear\n\0

例如:

MonJun2612:03:532000

gmtime

语法:


#include<time.h>

structtm*gmtime(consttime_t*time);

mktime

语法:


#include<time.h>

time_tmktime(structtm*time);

功能:函数转换参数time类型的本地时间至日历时间,并返回结果。如果发生错误,返回-1。

strftime

语法:


#include<time.h>

size_tstrftime(char*str,size_tmaxsize,constchar*fmt,structtm*time);

功能:函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,然后存储在字符串str中(至多maxsize个字符)。用于设定时间不同类型的代码为:

代码

含义

%a

星期的缩略形式

%A

星期的完整形式

%b

月份的缩略形式

%B

月份的完整形式

%c

月份的缩略形式

%d

月中的第几天(1-31)

%H

小时,24小时格式(0-23)

%I

小时,12小时格式(1-12)

%j

年中的第几天(1-366)

%m

月份(1-12).Note:某些版本的MicrosoftVisualC++可能使用取值范围0-11.

%M

分钟(0-59)

%p

本地时间的上午或下午(AMorPM)

%S

秒钟(0-59)

%U

年中的第几周,星期天是一周的第一天

%w

星期几的数字表示(0-6,星期天=0)

%W

一年中的第几周,星期天是一周的第一天

%x

标准日期字符串

%X

标准时间字符串

%y

年(0-99)

%Y

用CCYY表示的年(如:2004)

%Z

时区名

%%

百分号

函数strftime()返回值为处理结果字符串str中字符的个数,如果发生错误返回零。

3.一个简单示例

4.//ctime_test.cpp:定¡§义°?控?制?台¬¡§应®|用®?程¨¬序¨°的Ì?入¨?口¨²点Ì?。¡ê
5.//
6.
7.#include
"stdafx.h"
8.
9.#include
<time.h>
10.#include
<stdlib.h>
11.
12.int_tmain(intargc,_TCHAR*argv[])
13.{
14.time_tstart,end;
15.start=time(NULL);
16.system("pause");
17.end=time(NULL);
18.printf("Thepauseused%fseconds.\n",difftime(end,start));
19.
20.clock_t_clk;
21._clk=clock();
22.printf("clock:%d\n",_clk);
23.
24.system("pause");
25.//ctime
26.printf("%s\n",ctime(&start));
27.system("pause");
28.
29.//localtime
30.printf("%s\n",asctime(localtime(&start)));
31.//gmtime
32.printf("%s\n",asctime(gmtime(&start)));
33.//mktime
34.printf("%I64d\n",mktime(localtime(&start)));
35.
36.//strftime
37.charbuff[1024];
38.strftime(buff,1023,
"%Y-%m-%d%H:%M:%S\n",localtime(&start));
39.printf("%s\n",buff);
40.
41.return0;
42.}
43.
44.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: