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年至今经过的时间

MemberTypeMeaningRange
tm_secintseconds after the minute0-60*
tm_minintminutes after the hour0-59
tm_hourinthours since midnight0-23
tm_mdayintday of the month1-31
tm_monintmonths since January0-11
tm_yearintyears since 1900
tm_wdayintdays since Sunday0-6
tm_ydayintdays since January 10-365
tm_isdstintDaylight 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个时间转换函数功能如下:

函数说明
asctimetm 转 string
ctimetime_t 转 string
gmtimeUTC时间的time_t 转 tm
localtime本地时间的time_t 转 tm
strftime格式化为string
mktimetm 转time_t

举例说明:

#include <iostream>
#include <time.h>       /* 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 <iostream>
#include <time.h>

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

  return 0;
}

(2) clock()

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

#include <iostream>
#include <time.h>       /* 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 <iostream>
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */
#include <string>

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/

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非晚非晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值