参考文献: 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

    参考文献:

    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中指定的时间。它们都以百分号(%)开头
          timeptrtm结构体指针

          格式说明

          说明符取而代之
          %a工作日缩写名称*Thu
          %A工作日全名*Thursday
          %b月份缩写名称*Aug
          %B月份全名*August
          %c日期和时间表示*Thu Aug 23 14:55:02 2001
          %C年份除以100,并截断为整数(00-9920
          %d每月的某天,零填充(01-3123
          %DMM/DD/YY日期,相当于%m/%d/%y08/23/01
          %e每月的某天,以空格填充(1-3123
          %FYYYY-MM-DD日期,相当于%Y-%m-%d2001-08-23
          %g以周为基础的年份,后两位数字(00-9901
          %G基于周的年份2001
          %h缩写的月份名称*(与相同%bAug
          %H24小时制小时(00-2314
          %I12小时制小时(01-1202
          %j一年中的一天(001-366235
          %m以十进制数表示的月份(01-1208
          %M分钟(00-5955
          %n换行符('\n'``
          %pAM或PM指定PM
          %r12小时制*02:55:02 pm
          %R24小时HH:MM制,相当于%H:%M14:55
          %S秒(00-6102
          %t水平制表符('\t'``
          %TISO 8601时间格式(HH:MM:SS),等效于%H:%M:%S14:55:02
          %uISO 8601工作日为数字,星期一为11-74
          %U以第一个星期日为第一个星期的第一天的星期数(00-5333
          %VISO 8601周编号(01-5334
          %w工作日为小数,星期日为00-64
          %W第一个星期一作为第一个星期的第一天的星期数(00-5334
          %x日期表示*08/23/01
          %X时间表示*14:55:02
          %y年,最后两位数字(00-9901
          %Y2001
          %zISO 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;
          炎热的夏日,KC 非常的不爽。他宁可忍受北极的寒冷,也不愿忍受厦门的夏天。最近,他开始研究天气的变化。他希望用研究的结果预测未来的天气。
          经历千辛万苦,他收集了连续 N(1≤N≤106) 的最高气温数据。
          现在,他想知道最高气温一直上升的最长连续天数。
          输入输出格式
          输入格式:
          第 1 行:一个整数 N 。1≤N≤106
          第 2 行:N个空格隔开的整数,表示连续 N 天的最高气温。0≤ ...
                                              文章目录一. 需要用到的相关知识1. 日期类实现2. 蔡勒公式二. 日期类题目1、淘宝网店2、一周的第几天3、美国节日4、计算日期到天数转换
          一. 需要用到的相关知识
          1. 日期类实现
          主要是通过operator++()来计算两个日期之间相差的天数。具体实现看下面这篇博客:日期类模拟实现。
          2. 蔡勒公式
          作用是通过输入的年月日算出今天是周几。
          公式:W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1 (其[ ]为取整符号)
          W是所求日期的星期数;
          如果求得的数大于7,可以直接对
                                              该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include#define N 13main(){int y,m,D,q,t=0,i,day=0,a=0,Day,n,k,O[N]={0,31,29,31,30,31,30,31,31,30,31,30,31},p[N]={0,31,28,31,30,31,30,31,31,30,31,30,31};//y是年,m是月,D是日,q计算周几,t,i,k...
          import java.text.DateFormat;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Calenda...
                                              日期时间是编程非常常用的功能。本文是对C++11到C++17相关编程接口的介绍。介绍C++可以使用的日期时间API主要分为两类:C-style 日期时间库,位于<ctime&...