C++中日期和时间
1. chrono库
参考文献:c++11中的日期和时间库
chrono库主要包含三种类型:duration(时间间隔)、clock(时钟)、time_point(时间点)
-
Duration
表示一段时间,只要能换算成秒即可
template <class Rep,class Period = ratio<1> > class duration
解释:
Rep:数据类型,表示Period的数量,如int,float,double
Period:ratio类型,用来表示用秒表示的时间单位
例子:
#include <iostream> #include <ratio> #include <chrono> using namespace std; int main() { typedef chrono::duration<int> seconds_type; typedef chrono::duration<int,milli> milliseconds_type; typedef chrono::duration<int,ratio<60*60>> hours_type; hours_type h_oneday(24); seconds_type s_oneday(60*60*24); milliseconds_type ms_oneday(s_oneday); seconds_type s_onehour(60*60); //向上进行转换需要进行强制类型转换 hours_type h_onehour(chrono::duration_cast<hours_type>(s_onehour)); milliseconds_type ms_onehour(s_onehour);//向下转换可以直接转换 cout << ms_onehour.count() << "ms in 1h" << endl; return 0; }
-
time_point
表示一个具体时间,只要它能用计算机时钟表示。一个time_point必须有一个clock计时。
例子:
int testTime_point(){ chrono::system_clock::time_point tp_epoch; chrono::time_point<chrono::system_clock,chrono::> tp_seconds (chrono::duration<int>(1)); chrono::system_clock::time_point tp(tp_seconds); cout << "1 second since system_clock epoch = "; cout << tp.time_since_epoch().count();//time_fron_epoch()用来获取1970年1月1日到time_point时间经过的duration. cout << "system clock period." << endl; //显示时间点 time_t tt = chrono::system_clock::to_time_t(tp); cout << "time_point tp is:" << ctime(&tt) << endl; return 0; }
-
clock
std::chrono::system_clock它表示当前的系统时钟,系统中的所有进程使用now()得到的时间是一致的。每一个clock类中都有确定的time_point,duration,Rep,period类型。
函数:
函数 功能 now() 当前时间time_point to_time_t() time_point转换成time_t秒 from_time_t() 从time_t转换成time_point int testTime_point(){ chrono::system_clock::time_point tp_epoch; chrono::time_point<chrono::system_clock,chrono::seconds> tp_seconds (chrono::duration<int>(1)); chrono::system_clock::time_point tp(tp_seconds); cout << "1 second since system_clock epoch = "; cout << tp.time_since_epoch().count(); cout << "system clock period." << endl; //显示时间点 time_t tt = chrono::system_clock::to_time_t(tp); cout << "time_point tp is:" << ctime(&tt) << endl; return 0; }
2.ctime
参考文献:
-
类型和结构体
-
struct tm
struct tm { int tm_sec; /* Seconds. [0-60] (1 leap second) */ int tm_min; /* Minutes. [0-59] */ int tm_hour; /* Hours. [0-23] */ int tm_mday; /* Day. [1-31] */ int tm_mon; /* Month. [0-11] */ int tm_year; /* Year - 1900. */ int tm_wday; /* Day of week. [0-6] */ int tm_yday; /* Days in year.[0-365] */ int tm_isdst; /* DST. [-1/0/1]*/ # ifdef __USE_MISC long int tm_gmtoff; /* Seconds east of UTC. */ const char *tm_zone; /* Timezone abbreviation. */ # else long int __tm_gmtoff; /* Seconds east of UTC. */ const char *__tm_zone; /* Timezone abbreviation. */ # endif };
-
clock_t:其实就是long int的别名,用来保存时钟计数。它的大小和CLOCK_PER_SEC有关,每个计算机的CLOCK_PER_SEC有区别。
-
time_t:通常将其实现为一个整数值,该整数值表示自UTC(1970年1月1日)00:00小时以来经过的秒数.
-
-
时间函数
-
clock
返回程序消耗的处理器时间。返回值类型为clock_t,它的大小与CLOCK_PER_SEC有关,一般通过clock来计算程序的实际处理时间,在调用程序前后分别调用clock()函数,计算两者的差值。要想得到消耗的实际时间需要将结果除以CLOCK_PER_SEC.
例子:
#include <stdio.h> #include <time.h> #include <math.h> using namespace std; int testCtimeClock(){ clock_t t; int f; t = clock(); printf("Calculating...\n"); int freq = 999998; for(int i = 2;i <= 999999;++i){ for(int j = sqrt(i);j > 1;--j){ if(i%j == 0){ --freq; break; } } } printf("The number of primes lower than 1000000 is:%d\n",freq); t = clock() - t; printf("It took me %d clicks(%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC); return 0; } int main(int argc,char *argv[]){ testCtimeClock(); return 0; }
-
time
获取当前时间time_t的值。该函数返回该值,并且入座参数不是空指针,则将此值设置为传入的指针参数的值。返回的值是相对于UTC1970年1月1日00:00::00以来的秒数。
/* time example */ #include <stdio.h> /* printf */ #include <time.h> /* time_t, struct tm, difftime, time, mktime */ int main () { time_t timer; struct tm y2k = {0}; double seconds; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time(&timer); /* get current time; same as: timer = time(NULL) */ seconds = difftime(timer,mktime(&y2k)); printf ("%.f seconds since January 1, 2000 in the current timezone", seconds); return 0; }
-
mktime
将tm结构转换为time_t。
/* mktime example: weekday calculator */ #include <stdio.h> /* printf, scanf */ #include <time.h> /* time_t, struct tm, time, mktime */ int main () { time_t rawtime; struct tm * timeinfo; int year, month ,day; const char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* prompt user for date */ printf ("Enter year: "); fflush(stdout); scanf ("%d",&year); printf ("Enter month: "); fflush(stdout); scanf ("%d",&month); printf ("Enter day: "); fflush(stdout); scanf ("%d",&day); /* get current timeinfo and modify it to the user's choice */ time ( &rawtime ); timeinfo = localtime ( &rawtime ); timeinfo->tm_year = year - 1900; timeinfo->tm_mon = month - 1; timeinfo->tm_mday = day; /* call mktime: timeinfo->tm_wday will be set */ mktime ( timeinfo ); printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]); return 0; }
-
difftime
计算两个时间之间的差值(一秒为单位)。
函数定义为:
/* Return the difference between TIME1 and TIME0. */ extern double difftime (time_t __time1, time_t __time0)
例子:
int testCtimeDifftime(){ time_t now; struct tm newyear; double seconds; time(&now);//获取当前时间,等同与now = time(NULL); newyear = *localtime(&now); newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0; newyear.tm_mon = 0; newyear.tm_mday = 1; seconds = difftime(now,mktime(&newyear)); printf("%.f seconds since new year in the current timezone.\n",seconds); return 0; }
-
-
转换函数
-
asctime
将结构体tm转换为字符串,形式为:Www Mmm dd hh:mm:ss yyyy
asctime函数可以看作如下:
char* asctime(const struct tm *timeptr) { static const char wday_name[][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static const char mon_name[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static char result[26]; sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", wday_name[timeptr->tm_wday], mon_name[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, 1900 + timeptr->tm_year); return result; }
例子:
/* asctime example */ #include <stdio.h> /* printf */ #include <time.h> /* time_t, struct tm, time, localtime, asctime */ int main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "The current date/time is: %s", asctime (timeinfo) ); return 0; }
-
ctime
将time_t值转换为字符串,形式为Www Mmm dd hh:mm:ss yyyy
此功能等同于:
asctime(localtime(timer));
例子:
/* ctime example */ #include <stdio.h> /* printf */ #include <time.h> /* time_t, time, ctime */ int main () { time_t rawtime; time (&rawtime); printf ("The current local time is: %s", ctime (&rawtime)); return 0; }
-
struct tm * gmtime(const time_t * timer);
将time_t转换为tm作为UTC时间
例子:
/* gmtime example */ #include <stdio.h> /* puts, printf */ #include <time.h> /* time_t, struct tm, time, gmtime */ #define MST (-7) #define UTC (0) #define CCT (+8) int main () { time_t rawtime; struct tm * ptm; time ( &rawtime ); ptm = gmtime ( &rawtime ); puts ("Current time around the World:"); printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min); printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min); printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min); return 0; }
-
struct tm * localtime(const time_t * timer);
将time_t转换为本地时间,使用timer指向的值来填充tm结构体,其值代表相应的时间,以当地时区表示。
例子:
/* localtime example */ #include <stdio.h> /* puts, printf */ #include <time.h> /* time_t, struct tm, time, localtime */ int main () { time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime (&rawtime); printf ("Current local time and date: %s", asctime(timeinfo)); return 0; }
-
size_t strftime (char ptr, size_t maxsize, const char format,const struct tm* timeptr );**
将时间格式化为字符串,将格式化的内容复制到ptr中,将其格式化说明符扩展为表示timeptr中描述的时间的相应值,并限制为maxsize个字符。如果生成的C字符串的长度(包括终止的空字符)不超过maxsize,则该函数返回复制到ptr的字符总数(不包括终止的空字符)。否则,它返回零,并且ptr指向的数组的内容是不确定的。
参数说明
参数 解释 ptr 指向将目标字符串复制到的目标数组的指针 maxsize 要复制到ptr的最大字符数,包括终止的null字符 format 字符串,包含常规字符和特殊格式说明符的任意组合。这些格式说明符由函数替换为相应的值,以表示timeptr中指定的时间。它们都以百分号( %
)开头timeptr tm结构体指针 格式说明
说明符 取而代之 例 %a
工作日缩写名称* Thu
%A
工作日全名* Thursday
%b
月份缩写名称* Aug
%B
月份全名* August
%c
日期和时间表示* Thu Aug 23 14:55:02 2001
%C
年份除以100,并截断为整数( 00-99
)20
%d
每月的某天,零填充( 01-31
)23
%D
短 MM/DD/YY
日期,相当于%m/%d/%y
08/23/01
%e
每月的某天,以空格填充( 1-31
)23
%F
短 YYYY-MM-DD
日期,相当于%Y-%m-%d
2001-08-23
%g
以周为基础的年份,后两位数字( 00-99
)01
%G
基于周的年份 2001
%h
缩写的月份名称*(与相同 %b
)Aug
%H
24小时制小时( 00-23
)14
%I
12小时制小时( 01-12
)02
%j
一年中的一天( 001-366
)235
%m
以十进制数表示的月份( 01-12
)08
%M
分钟( 00-59
)55
%n
换行符( '\n'
)`` %p
AM或PM指定 PM
%r
12小时制* 02:55:02 pm
%R
24小时 HH:MM
制,相当于%H:%M
14:55
%S
秒( 00-61
)02
%t
水平制表符( '\t'
)`` %T
ISO 8601时间格式( HH:MM:SS
),等效于%H:%M:%S
14:55:02
%u
ISO 8601工作日为数字,星期一为 1
(1-7
)4
%U
以第一个星期日为第一个星期的第一天的星期数( 00-53
)33
%V
ISO 8601周编号( 01-53
)34
%w
工作日为小数,星期日为 0
(0-6
)4
%W
第一个星期一作为第一个星期的第一天的星期数( 00-53
)34
%x
日期表示* 08/23/01
%X
时间表示* 14:55:02
%y
年,最后两位数字( 00-99
)01
%Y
年 2001
%z
ISO 8601与时区UTC的偏移量(1分钟= 1,1小时= 100) 如果无法确定时区,则没有字符 +100
%Z
时区名称或缩写* 如果无法确定时区,则没有字符 CDT
%%
一个 %
标志%
例子:
/* strftime example */ #include <stdio.h> /* puts */ #include <time.h> /* time_t, struct tm, time, localtime, strftime */ int main () { time_t rawtime; struct tm * timeinfo; char buffer [80]; time (&rawtime); timeinfo = localtime (&rawtime); strftime (buffer,80,"Now it's %I:%M%p.",timeinfo); puts (buffer); return 0; }
-