C++时间与日期及相关 *** 作

C++时间与日期及相关 *** 作,第1张

1. 概念

(1) Coordinated Universal Time(UTC)

        协调世界时,又称世界标准时间,即格林威治标准时间(Greenwich Mean Time,GMT)。


UTC 通用标准时,以z来标识,时间格式如下:

2022-04-07T07:04:43.995Z

        北京时间为UTC+8,UTC时间转化为:

2019-4-7 15:04:43

(2)Calendar Time

        日历时间,是用"从一个标准时间点到此时的时间经过的秒数"来表示的时间。


这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是"相对时间",但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。


一般表示距离UTC时间 1970-01-01 00:00:00的秒数。


(3) epoch

        时间点。


在标准c/c++中是一个整数,用此时的时间和标准时间点相差的秒数(即日历时间)来表示。


2. 数据类型

(1)time_t

        这是一个适合存储日历时间类型,实质是long int型。


(2)struct tm

        tm包括日历日期和时间的各个组成。


struct tm {
    int tm_sec;     // 秒 0-59(一般)
    int tm_min;     // 分 0-59
    int tm_hour;    // 小时0-23
    int tm_mday;    // day 1-31
    int tm_mon;     // 月0-11
    int tm_year;    // 距 1900 的年数 如2022-1900 = 122
    int tm_wday;    // 星期 0-6
    int tm_yday;    // 距1月1号天数,0-365
    int tm_isdst;   // 夏令时  
}

(3)struct timeval

struct timeval{
    long int tv_sec;     // 秒数
    long int tv_usec;    // 微秒数
}
3. *** 作函数

(1)time函数获得日历时间

函数原型: time_t time(time_t *timer)

参数说明: timer=NULL时得到当前日历时间(从1900-01-01 00:00:00到现在的秒数(本地时区)),timer=时间数值时,用于设置日历时间,time_t是一个unsigned long类型。


如果 timer不为空,则返回值也存储在变量 timer中。


函数功能: 得到当前日历时间或者设置日历时间

函数返回: 当前日历时间

(2)localtime函数获得日期时间

函数原型: struct tm *localtime(const time_t *timer)

函数功能: 使用 timer 的值来填充 tm 结构。


timer 的值被分解为 tm 结构,并用本地时区表示。


函数返回: 以tm结构表达的时间

(3)asctime函数

函数原型: char* asctime(struct tm * ptr)

函数功能:将结构struct tm * ptr所表示的时间以字符串表示

函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年

参数说明: 结构指针ptr应通过函数localtime()或gmtime()得到

#include 
#include 
using namespace std;

int main ()
{
   time_t timer;
   struct tm *ltm;

   time( &timer );
   ltm= localtime( &timer );

   //printf("当前的本地时间和日期:%s", asctime(ltm));

   // 输出 tm 结构的各个组成部分
   cout << "当前的本地时间和日期:" << endl;
   cout << "年: "<< 1900 + ltm->tm_year << endl;
   cout << "月: "<< 1 + ltm->tm_mon<< endl;
   cout << "日: "<<  ltm->tm_mday << endl;
   cout << "时间: "<< ltm->tm_hour << ":";
   cout << ltm->tm_min << ":";
   cout << ltm->tm_sec << endl;

   return(0);
}

(3)mktime函数

函数原型:time_t mktime(struct tm *timeptr)

函数功能:把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值

函数返回:该函数返回一个 time_t 值,该值对应于以参数传递的日历时间。


如果发生错误,则返回 -1 值。


#include 
#include 

int main ()
{
   int ret;
   struct tm info;
   char buffer[80];

   info.tm_year = 2001 - 1900;
   info.tm_mon = 7 - 1;
   info.tm_mday = 4;
   info.tm_hour = 0;
   info.tm_min = 0;
   info.tm_sec = 1;
   info.tm_isdst = -1;

   ret = mktime(&info);
   if( ret == -1 )
   {
       printf("错误:不能使用 mktime 转换时间。


\n"); } else { strftime(buffer, sizeof(buffer), "%c", &info ); print(buffer); } return(0); }

(4)gettimeday函数

函数原型:int gettimeofday(struct timeval*tv, struct timezone *tz)

函数功能:获得当前时间,精确到微秒(1e-6 s)量级

函数返回:该函数执行成功后返回0,失败后返回-1,错误代码存于errno中

函数参数:参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:

struct timezone{
    int tz_minuteswest;    // 格林威治时间往西方的时差
    int tz_dsttime;        // DST 时间的修正方式
}

timezone 参数若不使用则传入NULL即可。


在一段代码前后分别使用gettimeofday可以计算代码执行时间:

#include 
#include 

int main()
{
    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    fun();
    gettimeofday(&tv_end, NULL);
    //转化成小数点表示
    double t_s = (double)(tv_begin.tv_sec*1000000+tv_begin.tv_usec)/1000000.0f;
    double t_e = (double)(tv_end.tv_sec*1000000+tv_end.tv_usec)/1000000.0f;
    printf("elapsedTime: %.6f s\n",(t_e - t_s));
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/577834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存