c++日期和时间相关操作-<time.h>(ctime)
来源:互联网 发布:外企软件侵权案例 编辑:程序博客网 时间:2024/06/05 03:30
time.h(ctime)是c time library,定义了获取和操作日期和时间的函数。
1. 结构
typedef long time_t;time_t是长整型,表示的是距(1970年,1月1日08:00:00)的秒数,常常通过time函数获得。
struct tm {int tm_sec; //秒 0-59(一般)int tm_min; //分 0-59int tm_hour; //小时0-23int tm_mday;//day 1-31int tm_mon; //月0-11int tm_year; // 距 1900 的年数 如2013-1900 = 113int tm_wday; //星期 0-6int tm_yday; //距1月1号天数,0-365int tm_isdst;
tm包括日历日期和时间的各个组成2.函数
time_t time ( time_t * timer );//获取当前时间time_t mktime ( struct tm * timeptr );//将struct tm转换为time_tstruct tm * localtime ( const time_t * timer ); //将time_t转换为struct tmsize_t strftime ( char * ptr, size_t maxsize, const char * format, const struct tm * timeptr ); //将struct tm转换为特定格式的字符串输出char *strptime(const char *buf,const char *format,struct tm *timeptr); //将format形式的时间字符串转换为struct tm
3.常用
#include <time.h>time_t now;now = time(NULL);//获取当前时间struct tm *timeinfo;timeinfo = localtime(&now);//转换为tmtime_t seconds;seconds = mktime(timeinfo);//转换为time_t
time_t now = time(NULL); struct tm timeinfo = *localtime(&now); char buf[40]; strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", &timeinfo); cout << buf << endl;//20130207142133 strptime("20130207112305", "%Y%m%d%H%M%S", &timeinfo); cout << timeinfo.tm_sec << endl;//5
4.获取当前时间的ms值
#include <time.h>#include <sys/time.h>struct timeval tv;gettimeofday (&tv, NULL);uint64_t mseconds=tv.tv_sec * 1000 + tv.tv_usec / 1000;
timeval用于指定时间值,结构如下
timeval{time_t tv_sec; //秒 [long int]suseconds_t tv_usec; //微秒 [long int]};
常用于计算一个方法的响应时间。
- c++日期和时间相关操作-<time.h>(ctime)
- c 时间相关学习 time.h
- C /C++标准库 - <ctime> (time.h)
- 时间相关 time() ctime time_t localtime() gettimeofday
- 时间相关 time() ctime time_t localtime() gettimeofday
- time.h ctime
- <ctime> (time.h)
- time.h ctime
- <C/C++>日期和时间的使用(time相关函数大全)
- c\c++ 时间相关函数 include<time.h>
- 时间和日期的相关类和方法Date&Time
- 彻底搞清楚C/C++中日期和时间 time_t与struct tm,time(NULL),ctime;strftime
- 彻底搞清楚C/C++中日期和时间 time_t与struct tm,time(NULL),ctime;strftime
- 彻底搞清楚C/C++中日期和时间 time_t与struct tm,time(NULL),ctime;strftime
- 彻底搞清楚C/C++中日期和时间 time_t与struct tm,time(NULL),ctime;strftime
- 彻底搞清楚 C/C++ 中日期和时间 time_t 与 struct tm,time(NULL),ctime;strftime
- [C++STDlib基础]关于日期时间的操作——C++标准库头文件<ctime>
- Matlab-------时间和日期的相关操作
- 代理类测试代码
- Box2d Tutorial
- SQL Server
- Android:多语言对应实现
- shell脚本从网络获取文件实现自动输入密码
- c++日期和时间相关操作-<time.h>(ctime)
- chm文件打开看不到内容的问题
- 软件包 xxx需要重新安装,但是我(apt)无法找到相应的安装文件的解决办法
- 网卡驱动学习之DCA
- 杭电1014Uniform Generator
- 对初学LoadRunner朋友的建意
- BFS走迷宫
- android in practice_Implementing custom message loops(ProducerConsumer project)
- 利用“抽象分支”做增量式大规模软件改造