C++中日期和时间

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

参考文献:

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;
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值