C/C++时间函数的使用
来源:http://blog.csdn.net/apull/article/details/5379819
一、获取日历时间
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 *)
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main(void)
- {
- time_t nowtime;
- nowtime = time(NULL); //获取当前时间
- cout << nowtime << endl;
- return 0;
- }
#include <iostream>
#include <time.h>
using namespace std;
int main(void)
{
time_t nowtime;
nowtime = time(NULL); //获取当前时间
cout << nowtime << endl;
return 0;
}
输出结果: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 *);
- #include <iostream>
- #include <time.h>
- using namespace std;
- void dsptime(const struct tm *); //输出时间。
- int main(void)
- {
- time_t nowtime;
- nowtime = time(NULL); //获取日历时间
- cout << nowtime << endl; //输出nowtime
- struct tm *local,*gm;
- local=localtime(&nowtime); //获取当前系统时间
- dsptime(local);
- gm=gmtime(&nowtime); //获取格林尼治时间
- dsptime(gm);
- return 0;
- }
- void dsptime(const struct tm * ptm)
- {
- char *pxq[]={"日","一","二","三","四","五","六"};
- cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;
- cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;
- cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl;
- }
#include <iostream>
#include <time.h>
using namespace std;
void dsptime(const struct tm *); //输出时间。
int main(void)
{
time_t nowtime;
nowtime = time(NULL); //获取日历时间
cout << nowtime << endl; //输出nowtime
struct tm *local,*gm;
local=localtime(&nowtime); //获取当前系统时间
dsptime(local);
gm=gmtime(&nowtime); //获取格林尼治时间
dsptime(gm);
return 0;
}
void dsptime(const struct tm * ptm)
{
char *pxq[]={"日","一","二","三","四","五","六"};
cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;
cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;
cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl;
}
输出结果:
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 *)
这两个函数返回值都是一个表示时间的字符串,区别在于传入的参数不同。
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main(void)
- {
- time_t nowtime;
- nowtime = time(NULL); //获取日历时间
- cout << nowtime << endl; //输出nowtime
- struct tm *local;
- local=localtime(&nowtime); //获取当前系统时间
- cout << asctime(local) ;
- cout << ctime(&nowtime) ;
- return 0;
- }
#include <iostream>
#include <time.h>
using namespace std;
int main(void)
{
time_t nowtime;
nowtime = time(NULL); //获取日历时间
cout << nowtime << endl; //输出nowtime
struct tm *local;
local=localtime(&nowtime); //获取当前系统时间
cout << asctime(local) ;
cout << ctime(&nowtime) ;
return 0;
}
输出结果:
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时钟计时单元数。
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main(void)
- {
- time_t start, ends;
- clock_t cstart,cends;
- start=time(NULL);
- cstart=clock();
- system("pause");
- ends=time(NULL);
- cends=clock();
- cout << "时间差:" << difftime(ends,start) << endl;
- cout << "Clock时间差:" << cends-cstart << endl;
- return 0;
- }
#include <iostream>
#include <time.h>
using namespace std;
int main(void)
{
time_t start, ends;
clock_t cstart,cends;
start=time(NULL);
cstart=clock();
system("pause");
ends=time(NULL);
cends=clock();
cout << "时间差:" << difftime(ends,start) << endl;
cout << "Clock时间差:" << cends-cstart << endl;
return 0;
}
输出结果:
请按任意键继续. . .
时间差: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 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
- #include <iostream>
- #include <time.h>
- using namespace std;
- int main(void)
- {
- time_t nowtime;
- nowtime = time(NULL); //获取日历时间
- cout << nowtime << endl; //输出nowtime
- struct tm *local;
- local=localtime(&nowtime); //获取当前系统时间
- char buf[80];
- strftime(buf,80,"格式化输出:%Y-%m-%d %H:%M:%S",local);
- cout << buf << endl;
- return 0;
- }
#include <iostream>
#include <time.h>
using namespace std;
int main(void)
{
time_t nowtime;
nowtime = time(NULL); //获取日历时间
cout << nowtime << endl; //输出nowtime
struct tm *local;
local=localtime(&nowtime); //获取当前系统时间
char buf[80];
strftime(buf,80,"格式化输出:%Y-%m-%d %H:%M:%S",local);
cout << buf << endl;
return 0;
}
输出结果:
1268575163
格式化输出:2010-03-14 21:59:23
C/C++时间函数的使用的更多相关文章
- C++中的时间函数
C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...
- 借助JavaScript中的时间函数改变Html中Table边框的颜色
借助JavaScript中的时间函数改变Html中Table边框的颜色 <html> <head> <meta http-equiv="Content-Type ...
- Loadrunner时间函数、用时间生成订单编号例子
Loadrunner中取时间函数.用时间函数生成订单编号例子: <如要转载,请注明网络来源及作者:Cheers_Lee> 问题的提出: (1)有时候在Loadrunner中用C语言设计脚本 ...
- Sql Server函数全解(四)日期和时间函数
日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外,也可以使用datetime类型的参数,但会忽略这些值的时间部分.相同 ...
- Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数
dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...
- mysql与oracle的日期/时间函数小结
前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...
- C库函数使用与总结之时间函数
1. localtime(取得当地目前时间和日期) [头文件]#include <time.h> [函数原型]struct tm *localtime(const time_t * tim ...
- 分享一些关于PHP时间函数的常用时间
<?php // 各种时间函数 echo "现在:".date("Y-m-d H:i:s")."<br>"; echo & ...
- MySQL数据库9 - 日期与时间函数
一 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为数 ...
- Oracle日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
随机推荐
- Contest Balloons
Contest Balloons 题目链接:http://codeforces.com/problemset/problem/725/D 贪心+枚举 每次在排名在自己前面的选出w-t值最小的送气球,更 ...
- <hr/>标签改变颜色注意事项
1.css改变颜色 <hr style="border:0;background-color:#093;height:1px;"> 注意: 如果不加border:0 ...
- get与post 获取参数值的方式
get方式 参数带在url后面,form表单中的域 可以 没有value 属性, 后台可以直接在方法的参数中加入和url一样的参数就能直接获得该参数的值(效率高,不安全) post方式 url链接 ...
- oracle常用的数据字典
一.oracle数据字典主要由以下几种视图构成:1.user视图以user_为前缀,用来记录用户对象的信息 2.all视图以all_为前缀,用来记录用户对象的信息及被授权访问的对象信息 3.dba视图 ...
- 【转发】彻底理解 JS 中 this 的指向
为什么要学习this?如果你学过函数式编程,面向对象编程,那你肯定知道干什么用的,如果你没有学过,那么暂时可以不用看这篇文章,当然如果你有兴趣也可以看看,毕竟这是js中必须要掌握的东西. 例子1: f ...
- 强大DOM选择器querySelector
今天碰到问题,用了下不经常用的querySelector还不错 querySelector 和 querySelectorAll 的使用非常的简单,就像标题说到的一样,它和 CSS 的写法完全一样,对 ...
- 使用Angular构建单页面应用(SPA)
什么是SPA?看下图就是SPA: 下面说正经的,个人理解SPA就是整个应用只有一个页面,所有的交互都在一个页面完成,不需要在页面之间跳转. 单页面的好处是更快的响应速度,更流畅的用户体验,甚至和桌面应 ...
- 大D实例化model-->调用自定义类方法,大M调用原声model方法
class ContactsModel extends Model{ public function addxxx(){ } } $conmodel = D('contacts','Model'); ...
- CMake VS工程总结
1.设置输出后缀 set(CMAKE_DEBUG_POSTFIX "d") 2.设置输出目录 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_ ...
- OpenGL 茶壶
void MyRenderer::Init_Teapot_VBO() { m_fun->glGenBuffers(, &m_teapot_vbo); m_fun->glBindBu ...