C++time相关

1. time

1.1. 简介

C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。

time

typedef /* unspecified */ time_t;//未定义,可以简单理解为 long
typedef long clock_t;//ticker数,CLOCKS_PER_SEC表示每秒ticker数

struct tm {
  int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
  int tm_min;   // 分,范围从 0 到 59
  int tm_hour;  // 小时,范围从 0 到 23
  int tm_mday;  // 一月中的第几天,范围从 1 到 31
  int tm_mon;   // 月,范围从 0 到 11
  int tm_year;  // 自 1900 年起的年数
  int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起
  int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
  int tm_isdst; // 夏令时
};
time_t time(time_t *time);//返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 -1。

struct tm *localtime(const time_t *time);//该函数返回一个指向表示本地时间的 tm 结构的指针。

char *ctime(const time_t *time);//该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0。
char * asctime ( const struct tm * time );//该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。

struct tm *gmtime(const time_t *time);//该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
time_t mktime(struct tm *time);//该函数返回日历时间,相当于 time 所指向结构中存储的时间。

double difftime ( time_t time2, time_t time1 );//time1 - time2

//https://www.cplusplus.com/reference/ctime/strftime/
size_t strftime();//该函数可用于格式化日期和时间为指定的格式。

//CLOCKS_PER_SEC
clock_t clock(void);//该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 -1。
#include <ctime>
#include <iostream>
#include <thread> // std::thread, std::thread::id, std::this_thread::get_id

using namespace std;

int main() {
    time_t now = time(0);                        //本机时钟当前时间,单位秒
    cout << "localtime:" << ctime(&now); //转化为字符串显示

    tm *gmtm = gmtime(&now);                     //转换为utc时间
    cout << "UTLtime:" << asctime(gmtm); //转化为字符串显示

    time_t rawtime;
    struct tm *timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    printf("Current local time and date: %s", asctime(timeinfo));

    char buffer[80];
    strftime(buffer, 80, "Now it's %I:%M%p.", timeinfo);
    puts(buffer);

    clock_t t = clock(); //开机执行的ticker,一般1秒1000个ticker,取决于 CLOCKS_PER_SEC 定义,不依赖系统时间修改
    this_thread::sleep_for(chrono::seconds(2));
    cout << "difftime:" << difftime(clock(), t) << endl; //该函数返回 time1 和 time2 之间相差的秒数。
    t = clock() - t;
    printf("It took me %d clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
    return 0;
}

// localtime:Thu Feb 04 17:05:23 2021
// UTLtime:Thu Feb 04 09:05:23 2021
// Current local time and date: Thu Feb 04 17:05:23 2021
// Now it's 05:05PM.
// difftime:2009
// It took me 2009 clicks (2.009000 seconds).

1.3. chrono

#include <chrono>
using namespace chrono;

1.3.1. duration

duration 是 chrono 裡面,用来记录时间长度的类别,他基本上是一个 template class,可以自行定义他的意义;chrono 也有提供一些比较常见的时间类别,可以直接拿来使用,下面就是内建的 duration 的型别:

typedef duration<long long, nano> nanoseconds;
typedef duration<long long, micro> microseconds;
typedef duration<long long, milli> milliseconds;
typedef duration<long long> seconds;
typedef duration<int, ratio<60> > minutes;
typedef duration<int, ratio<3600> > hours;
std::chrono::minutes t1( 10 );
std::chrono::seconds t2( 60 );
std::chrono::seconds t3 = t1 - t2;
std::cout << t3.count() << " second" << std::endl;//540 .count()取的一个duration的值
cout << chrono::duration_cast<chrono::minutes>( t3 ).count() << endl;//9

1.3.2. time_point

相较于 duration 是用来记录时间的长度的
time_point 是用来记录一个特定时间点的资料类别。
他一样是一个 template class,需要指定要使用的 clock 与时间单位(duration)。

system_clock 是直接去抓系统的时间,1970-01-01 00:00:00 UTC逝去的时间单位是1个tick
steady_clock 系统开机到现在逝去的纳秒
high_resolution_clock typedef steady_clock high_resolution_clock;

#include <ctime>
#include <iostream>
#include <thread> // std::thread, std::thread::id, std::this_thread::get_id

using namespace std;
using TIME_POINT = std::chrono::steady_clock::time_point;

TIME_POINT  getCurrentTimePoint(){
    return std::chrono::steady_clock::now();
}

int64_t getDeltaS(TIME_POINT beg){
    return getDeltaS(beg, getCurrentTimePoint());
}
int64_t getDeltaS(TIME_POINT beg, TIME_POINT end){
    return std::chrono::duration_cast<std::chrono::seconds>(end - beg).count();
}

int64_t getDeltaMs(TIME_POINT beg){
    return getDeltaMs(beg, getCurrentTimePoint());
}
int64_t getDeltaMs(TIME_POINT beg, TIME_POINT end){
    return std::chrono::duration_cast<std::chrono::milliseconds>(end - beg).count();
}

int64_t getDeltaUs(TIME_POINT beg){
    return getDeltaUs(beg, getCurrentTimePoint());
}
int64_t getDeltaUs(TIME_POINT beg, TIME_POINT end){
    return std::chrono::duration_cast<std::chrono::microseconds>(end - beg).count();
}

int64_t getDeltaNs(TIME_POINT beg){
    return getDeltaNs(beg, getCurrentTimePoint());
}
int64_t getDeltaNs(TIME_POINT beg, TIME_POINT end){
    return std::chrono::duration_cast<std::chrono::nanoseconds>(end - beg).count();
}

int main() {
    std::chrono::minutes t1(10);
    std::chrono::seconds t2(60);
    auto t3 = t1 - t2;                                                  //默认转化为更细粒度,t3是std::chrono::seconds
    std::cout << t3.count() << " second" << std::endl;                  //540 .count()取的一个duration的值
    cout << chrono::duration_cast<chrono::minutes>(t3).count() << endl; //9

    auto now1 = getCurrentTimePoint();
    this_thread::sleep_for(chrono::milliseconds(200));
    std::cout << "cost " << getDeltaUs(now1) << " us.\n";//cost 211927 us.

    auto sysNow = getCurrentTimePoint();
    auto sysNow2 = sysNow + std::chrono::hours(2);
    std::time_t now_c = std::chrono::system_clock::to_time_t(sysNow);     //输出先转为time_t
    std::time_t now_c2 = std::chrono::system_clock::to_time_t(sysNow2); //输出先转为time_t
    std::cout << "system_clock now:" << std::ctime(&now_c) << std::endl;//system_clock now:Thu Feb 04 17:24:29 2021
    std::cout << "system_clock now+2h:" << std::ctime(&now_c2) << std::endl;// system_clock now+2h:Thu Feb 04 19:24:29 2021
    return 0;
}

1.4. 参考资料

  1. C++11 时间工具chrono
### C++ 中 `timezone` 未定义的解决方案 在 C++ 标准库中,`timezone` 并不是一个标准定义的符号。它通常出现在 POSIX 系统中,作为 `<ctime>` 或 `<time.h>` 的扩展功能[^4]。然而,在 C++11 及之后的标准中,C++ 提供了更现代化的时间处理工具,例如 `<chrono>` 和 `<tzdb>`(通过第三方库支持)。如果需要解决 `timezone` 未定义的问题,可以考虑以下方法: #### 方法一:使用 POSIX 扩展 如果目标平台支持 POSIX,并且允许使用非标准扩展,可以通过包含 `<ctime>` 或 `<time.h>` 来访问 `timezone`。需要注意的是,这种方法依赖于具体平台的支持,并不具有跨平台特性。 ```cpp #include <ctime> #include <iostream> int main() { std::time_t t = std::time(nullptr); char* time_str = std::ctime(&t); std::cout << "Local time: " << time_str; std::cout << "Timezone offset: " << timezone << " seconds" << std::endl; // POSIX extension return 0; } ``` 此代码段展示了如何在 POSIX 系统上使用 `timezone`[^5]。 #### 方法二:使用 `<chrono>` 和 `<ctime>` C++11 引入了 `<chrono>` 库,提供了更高精度的时间操作接口。虽然 `<chrono>` 不直接支持时区偏移,但可以通过结合 `<ctime>` 实现类似功能。 ```cpp #include <chrono> #include <ctime> #include <iostream> int main() { auto now = std::chrono::system_clock::now(); std::time_t now_time = std::chrono::system_clock::to_time_t(now); std::tm local_tm = *std::localtime(&now_time); std::tm utc_tm = *std::gmtime(&now_time); std::time_t local_time = std::mktime(&local_tm); std::time_t utc_time = std::mktime(&utc_tm); long timezone_offset = (std::difftime(local_time, utc_time) / 60); // 分钟为单位 std::cout << "Timezone offset: " << timezone_offset << " minutes" << std::endl; return 0; } ``` 上述代码通过计算本地时间和 UTC 时间的差异来获取时区偏移量[^6]。 #### 方法三:使用第三方库(如 `tzdb`) 对于需要跨平台支持和更复杂时区处理的情况,推荐使用第三方库,例如 Howard Hinnant 的 `date` 库,该库基于 C++11/14/17 标准开发,并提供了对时区的全面支持。 安装 `date` 库后,可以使用以下代码: ```cpp #include "date/tz.h" #include <iostream> int main() { using namespace date; using namespace std::chrono; auto now = system_clock::now(); zoned_time zt{"America/New_York", now}; std::cout << "Current time in New York: " << zt << std::endl; auto tz = current_zone()->name(); std::cout << "Current timezone: " << tz << std::endl; return 0; } ``` 此代码展示了如何使用 `date` 库处理时区信息[^7]。 ### 注意事项 - 如果项目需要严格遵守 C++ 标准,则应避免使用非标准扩展。 - 在跨平台开发中,推荐使用标准库或成熟的第三方库(如 `date`)来替代平台特定的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值