ctime 时间
1. 类型
clock_t: 是个long型,用来记录一段时间内的时钟计时单元数,即CPU的运行单元时间。
size_t: 标准C库中定义的,应为unsigned int,在64位系统中为long unsigned int。
time_t: 从1970年1月1日0时0分0秒到该时间点所经过的秒数。
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代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
2. 时间的操作
clock: 返回时钟计时单元数,自从这个程序开始运行。
time: 返回当前的time_t。
difftime: 计算time_t两个之间的时间差。
3. 转换
mktime: 转换tm structure成time_t
asctime: 转换tm structure成字符串
ctime: 转换time_t成字符串
gmtime: 转换time_t成tm as UTC time
localtime: 转换time_t成tm as local time
strftime: 格式时间成字符串
转换成字符串的几个函数:asctime, ctime, strftime
4. 宏
CLOCKS_PER_SEC: 它用来表示一秒钟会有多少个时钟计时单元。
// 测量事件的持续时间
void test_clock_t()
{
long i = 100000000L;
clock_t start, finish;
double duration;
start = clock();
/* 测量一个事件持续的时间 */
while(i--) {};
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("Time to do 100000000 empty loops is %f seconds\n", duration);
}
void test_time_t()
{
time_t t = time(NULL);
printf("The Calendar Time now is %d\n", t);
}
void test_difftime()
{
time_t start,end;
start = time(NULL);
system("pause");
end = time(NULL);
printf("The pause used %5.4f seconds.\n", difftime(end, start));
}
// 下面都是一些转换函数的应用
// mktime: tm --> time_c
void test_mktime()
{
struct tm t;
time_t t_of_day;
t.tm_year = 1997 - 1900;
t.tm_mon = 6;
t.tm_mday = 1;
t.tm_hour = 0;
t.tm_min = 0;
t.tm_sec = 1;
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0;
t_of_day = mktime(&t);
printf(ctime(&t_of_day));
}
// localtime: time_c --> tm
void test_localtime()
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Current local time and date: %s", asctime(timeinfo));
}
// gmtime: time_c --> tm
void test_gmtime()
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = gmtime(&rawtime);
printf("UTC time and date: %s", asctime(timeinfo));
}
// ctime: time_t --> string
void test_ctime()
{
time_t t = time(NULL);
std::string str = ctime(&t);
std::cout << str << std::endl;
}
ctime 时间的更多相关文章
- C语言获取时间
转载:http://www.cnblogs.com/fzhe/archive/2012/11/06/2757858.html C语言获取系统时间的几种方式 C语言中如何获取时间?精度如何? 1 使 ...
- C语言获取系统时间的几种方式[转]
C语言获取系统时间的几种方式 C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒 2 使用clock_t clock() 得到的是CPU时间 ...
- linux atime ctime mtime
touch testtime 1. stat testtime[为文件名] 可以查看这个文件名的三者状态 2.ll testtime;ll --time=atime testtime ;ll --ti ...
- 在CTime类中重载<<和>>
程序代码: #include <iostream> using namespace std; class CTime//时间类 { private: unsigned short int ...
- 关于Ctime库
--------------------- 本文来自 Fuko_Ibuki 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_31908675/article/de ...
- C语言获取系统时间的几种方式
C语言获取系统时间的几种方式 2009-07-22 11:18:50| 分类: 编程学习 |字号 订阅 C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * ...
- Linux 下的三种时间介绍
Linux 下的三种时间介绍: Access Time:简写为atime,表示文件访问的时间,当文件内容被访问时,更新atime时间 Modify Time:简写为mtime,表示文件内容修改的时间, ...
- VS2010/MFC编程入门之四十三(MFC常用类:CTime类和CTimeSpan类)
上一节中鸡啄米讲了MFC常用类CString类的用法,本节继续讲另外两个MFC常用类-日期和时间类CTime类和CTimeSpan类. 日期和时间类简介 CTime类的对象表示的时间是基于格林威治标准 ...
- MFC:CTime类和CTimeSpan类
CTime类 CTime类表示日期和时间,上限是3000年12月31日,下限是1970年1月1日 12:00:00 AM GMT. CTime(); 构造一个未经初始化的CTime对象.此状态的CTi ...
随机推荐
- 微信小程序 功能函数 密码验证*
//登录输入密码 userPasswordInput: function (e) { var that = this; this.setData({ userPassword: e.detail.va ...
- HDU4183_Pahom on Water
题意为给你若干个圆,每个圆的颜色对应一个频率,如果两个圆有公共部分,那么这两个圆之间可以走,你可以从起点开始,从频率小的圆走向频率大的圆并且到达终点后,从频率大的圆走向频率小的圆,最终回到起点,路径中 ...
- 内存映像分析工具Eclipse Memory Analyzer
1. Eclipse Memory Analyzer安装 Help ->Eclipse Marketplace,搜索Memory,点击install,->confirm->同意证书内 ...
- A Mist of Florescence CodeForces - 989C(思维构造)
题意: 让你构造一个图,使得A,B,C,D的个数为给定的个数,上下左右连通的算一个. 哎呀 看看代码就懂了..emm..很好懂的 #include <bits/stdc++.h> usin ...
- c++11 委托构造
c++11 委托构造 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #includ ...
- 51nod 1571 最近等对 | 线段树 离线
51nod 1571 最近等对 题面 现在有一个序列 a1, a2, ..., an ,还有m个查询 lj, rj (1 ≤ lj ≤ rj ≤ n) .对于每一个查询,请找出距离最近的两个元素 ax ...
- 洛谷 P1939 【模板】矩阵加速(数列) 解题报告
P1939 [模板]矩阵加速(数列) 题目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007(10^9+7)取余的值 ...
- Lowest Common Ancestor in a Binary Tree
二叉树最低公共祖先节点 acmblog If one key is present and other is absent, then it returns the present key as LC ...
- HDU 3271 数位dp+二分
SNIBB Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Hadoop生态圈-Hive的自定义函数之UDAF(User-Defined Aggregation Function)
Hadoop生态圈-Hive的自定义函数之UDAF(User-Defined Aggregation Function) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.