C/C++中的ctime用法总结

头文件time.h为C语言当中的头文件,被C++继承过来。可以结合chrono进行使用。

如果想要了解chrono格式与ctime格式的转换,或者需要学习chrono时间,可以点击链接进行学习。

1. 时间的数据类型

头文件time.h中,总共有四个时间类型clock_t、size_t、time_t和struct tm。下面分别进行介绍。

(1)struct tm

自1900年至今经过的时间

Member Type Meaning Range
tm_sec int seconds after the minute 0-60*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag

(2)clock_t

  • 表示时钟的滴答,可以用ticks 表示。实际是long类型。
  • 1秒的clicks为CLOCKS_PER_SEC个ticks,定义为1000个。

(3)time_t

从1900年1月1日0点UTC时间开始的时间, 实际是一个long类型。单位秒。

(4)size_t

size_t类型很多地方都可以使用,实际上是一个unsigned int类型。

总结:最常用的为struct tm和time_t类型。clock_t可以用于精确计算。

2. 时间转换函数

5个时间转换函数功能如下:

函数 说明
asctime tm 转 string
ctime time_t 转 string
gmtime UTC时间的time_t 转 tm
localtime 本地时间的time_t 转 tm
strftime 格式化为string
mktime tm 转time_t

举例说明:

#include 
#include        /* time_t, struct tm, difftime, time, mktime */

int main ()
{
     
  time_t now;
  time(&now);//获取现在的时间

  tm to_tm = *localtime(&now);//将刚刚获取的时间,转换为tm格式
  time_t to_time_t = mktime(&to_tm);//tm 转time_t;
  std::cout<<"to_time_t: "<<to_time_t<<std::endl;//time_t转tm
  std::cout <<"to_tm: "<<to_tm.tm_year<<std::endl;//从1990年经历了多少年

  std::string thisTime = ctime(&to_time_t);//time_t转string,需要传地址
  std::cout<<thisTime<<std::endl;
  thisTime = asctime(&to_tm);//tm转string
  std::cout<<thisTime<<std::endl;

  return 0;
}

3. 时间操作的函数

有4种处理时间的函数:

(1) time():

  • 获取当地时间,返回time_t类型。
  • 返回的时间为UTC格林尼治时间1970年1月1日00:00:00到当前时刻的时长,时长单位是秒
#include 
#include 

int main ()
{
     
  time_t timer;
  time(&timer);  //或者timer = time(NULL),两种方式获取当地时间
  std::cout << timer <<std::endl;

  return 0;
}

(2) clock()

获取时钟的clicks,返回clock_t类型。

#include 
#include        /* clock_t, clock, CLOCKS_PER_SEC */

int main ()
{
     
  clock_t t;
  int f;
  t = clock();//获取现在的clicks

  for (int i=0; i<=10000; ++i)
      for (int j=0;j<10000;++j)
  {
     
      int x = i + j;
  }

  t = clock() - t;//获取ticks差

  std::cout << "Time diff: " << t << " ticks" << std::endl;
  std::cout << "Time diff: " << ((float)t/CLOCKS_PER_SEC) << " s" <<std::endl;

  return 0;
}

(3) difftime()

获取时间差的函数,返回double类型。其实可以直接做减法,如下:

#include 
#include        /* time_t, struct tm, difftime, time, mktime */
#include 

int main ()
{
     
  time_t t1;
  time_t t2;
  struct tm tm1, tm2;
  double seconds;

  time(&t1);//获取现在的时间
  for (int i=0; i<=100000; ++i)
       for (int j=0;j<100000;++j)
   {
     
       int x = i + j;
   }
  time(&t2);//获取现在的时间

  seconds = difftime(t2,t1);//返回double类型
  int diffTime = t2 - t1;//因为time_t为long类型
  std::cout << "difftime(): " << seconds <<std::endl;
  std::cout << "sub: " << diffTime <<std::endl;

  return 0;
}

参考:http://www.cplusplus.com/reference/ctime/

你可能感兴趣的:(C/C++编程,ctime,时间)