C/C++时间函数的使用

一、获取日历时间
time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从1970年1月1日0时0分0秒到此时的秒数,原型是:
typedef long time_t; /* time value */
可以看出time_t其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是2038年1月18日19时14分07秒。

函数time可以获取当前日历时间时间,time的定义:
time_t time(time_t *)

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t nowtime;
  7. nowtime = time(NULL); //获取当前时间
  8. cout << nowtime << endl;
  9. return 0;
  10. }

输出结果:1268575163

二、获取本地时间
time_t只是一个长整型,不符合我们的使用习惯,需要转换成本地时间,就要用到tm结构,time.h中结构tm的原型是:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [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 January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};


可以看出,这个机构定义了年、月、日、时、分、秒、星期、当年中的某一天、夏令时。可以用这个结构很方便的显示时间。

用localtime获取当前系统时间,该函数将一个time_t时间转换成tm结构表示的时间,函数原型:
struct tm * localtime(const time_t *)
使用gmtime函数获取格林尼治时间,函数原型:
struct tm * gmtime(const time_t *)

为了方便显示时间,定义了一个函数void dsptime(const struct tm *);

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. void dsptime(const struct tm *); //输出时间。
  5. int main(void)
  6. {
  7. time_t nowtime;
  8. nowtime = time(NULL); //获取日历时间
  9. cout << nowtime << endl; //输出nowtime
  10. struct tm *local,*gm;
  11. local=localtime(&nowtime); //获取当前系统时间
  12. dsptime(local);
  13. gm=gmtime(&nowtime); //获取格林尼治时间
  14. dsptime(gm);
  15. return 0;
  16. }
  17. void dsptime(const struct tm * ptm)
  18. {
  19. char *pxq[]={"日","一","二","三","四","五","六"};
  20. cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;
  21. cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;
  22. cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl;
  23. }

输出结果:
1268575163
2010年3月14日 21:59:23 星期日 当年的第72天
2010年3月14日 13:59:23 星期日 当年的第72天

三、输出时间
C/C++语言提供了用字符串格式表示时间的函数。
char * asctime(const struct tm *)
char * ctime(const time_t *)
这两个函数返回值都是一个表示时间的字符串,区别在于传入的参数不同。

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t nowtime;
  7. nowtime = time(NULL); //获取日历时间
  8. cout << nowtime << endl; //输出nowtime
  9. struct tm *local;
  10. local=localtime(&nowtime); //获取当前系统时间
  11. cout << asctime(local) ;
  12. cout << ctime(&nowtime) ;
  13. return 0;
  14. }

输出结果:
1268575163
Sun Mar 14 13:59:23 2010
Sun Mar 14 21:59:23 2010

四、计算时间间隔
可以通过difftime来计算两个时间的间隔,可以精确到秒,函数原型:
double difftime(time_t, time_t)
要想精确到毫秒,就要使用clock函数了,函数原型:
clock_t clock(void)
从定义可以看出clock返回一个clock_t类型,这个类型也定义在time.h中,原型是:
typedef long clock_t
clock_t也是一个长整型,表示的是从程序开始运行到执行clock函数时所经过的cpu时钟计时单元数。

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t start, ends;
  7. clock_t cstart,cends;
  8. start=time(NULL);
  9. cstart=clock();
  10. system("pause");
  11. ends=time(NULL);
  12. cends=clock();
  13. cout << "时间差:" << difftime(ends,start) << endl;
  14. cout << "Clock时间差:" << cends-cstart << endl;
  15. return 0;
  16. }

输出结果:
请按任意键继续. . .
时间差:3
Clock时间差:3094

在time.h中定义了一个CLOCKS_PER_SEC
/* Clock ticks macro - ANSI version */
#define CLOCKS_PER_SEC 1000
表示1秒钟内有多少个时钟计时单元,在标准C/C++中,最小的计时单位是1毫秒。


五、自定义时间格式
C/C++在time.h中提供了一个自定义时间格式的函数strftime,函数原型:
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
参数说明:
char *strDest:用来存放格式化后的字符串缓存,
size_t maxsize:指定最多可以输出的字符数,
const char *format:格式化字符串,
const struct tm *timeptr:要转换的时间。

可使用的格式化字符串:
%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 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t nowtime;
  7. nowtime = time(NULL); //获取日历时间
  8. cout << nowtime << endl; //输出nowtime
  9. struct tm *local;
  10. local=localtime(&nowtime); //获取当前系统时间
  11. char buf[80];
  12. strftime(buf,80,"格式化输出:%Y-%m-%d %H:%M:%S",local);
  13. cout << buf << endl;
  14. return 0;
  15. }

输出结果:
1268575163
格式化输出:2010-03-14 21:59:23

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值