C标准时间和日期函数
2013-12-13 10:45
127 查看
标准c时间与日期函数
1.数据结构
time_t----长整型__int64clock_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); |
difftime
语法:#include<time.h> doubledifftime(time_ttime2,time_ttime1); |
clock
语法:#include<time.h> clock_tclock(void); |
ctime
语法:#include<time.h> char*ctime(consttime_t*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); |
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); |
strftime
语法:#include<time.h> size_tstrftime(char*str,size_tmaxsize,constchar*fmt,structtm*time); |
代码 | 含义 |
%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 | 时区名 |
%% | 百分号 |
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.
相关文章推荐
- Web 通信 之 长连接、长轮询(long polling)
- 实用ExtJS教程100例-009:ExtJS Form无刷新文件上传
- NSPredicate,谓词
- RBAC 基于角色的访问控制
- 获取指定列中的连续数字
- Direct9学习之--------------------------实时阴影的另一种实现ShadowMap
- xp删除用户导致加密文件打不开的解决方法
- 哈希表
- linux进程间通信(一)--------管道
- 微信前端访问模拟器(flask)
- 文本每行的某列作为参数传递给别的程序
- 大数据量处理经验
- R语言在windows下报错问题
- 在dos下进入mysql
- java 环境搭建
- 实用ExtJS教程100例-008:使用iframe填充ExtJS Window组件
- 关于NSMutableArray removeObject
- shell和perl对文件单词的频数统计
- ASP.NET 使用My97DatePicker日期控件
- 【转】MFC CListCtrl 使用技巧