C/C++ time處理及其相互轉換

C 的時間

time_point

時間單位默認是秒,絕對時間獲取的時間點都是相對新紀元。相關概念:

新紀元:1970年1月1日00:00:00 UTC(GMT)

#include <time.h>

typedef long time_t //時間點,秒單位

time_t time(time_t *t) //返回自從新紀元以來流逝的秒數,將當前時間點寫入到t


/***************************/
// 少用
#include <sys/time.h>

struct timeval {
    time_t        tv_sec;   // 秒
    susenconds_t  tv_usec;  // 微秒
}

int gettimeofday(struct timeval *tv,
                 struct timezone *tz);


/***************************/
// 常用
#include <time.h>

struct timespec {
    time_t    tv_sec;   // 秒
    long      tv_nsec;  // 納秒
}

int clock_gettime(clockid_t clock_id,
                  struct timespec *ts);


/***************************/
// 時間差
double difftime(time_t time1, time_t time0);

tm 是人們容易理解的時間格式

#include <time.h>

struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;  /*該月的日期,0~31*/
    int tm_mon;   /*0~11*/
    int tm_year;  /*從1900年以來的年數*/
    int tm_wday;  /*從週日以來的天數 0~6*/
    int tm_yday;  /*從1月1日以來的日期數 0~356*/
    int tm_isdst;
#ifdef _BSD_SOURCE
    long tm_gmtoff;
    const char *tm_zone;
#endif /* _BSD_SOURCE */
};

時間字符串表達:

char * asctime(const struct tm *tm); // tm -> str

char * ctime(const time_t *timep);   // time_t -> str 

struct tm * localtime(const time_t *timep); // time_t -> tm

time_t mktime(struct tm *tm);               // tm -> time_t

C++

std::chrono是一個Time library,同時c庫的<time.h>在c++裏表示爲<ctime>。c++版本封裝了不同單位的時間功能,忽略了獲取的時間是秒、還是微秒抑或是納秒的概念

分爲三個模塊

Durations

默認是秒:

typedef std::chrono::duration<int> seconds_type;
typedef std::chrono::duration<int, std::milli> milliseconds_type;
typedef std::chrono::duration<int, std::ratio<60*60>> hours_type;

std::chrono::seconds
std::chrono::milliseconds


std::chrono::hours = typedef duration<int, ratio<3600, 1>> hours;
std::chrono::minutes = typedef duration<int, ratio<60, 1>> minutes;
std::chrono::seconds = typedef duration<int, ratio<1, 1>> seconds;
std::chrono::milliseconds = typedef duration<int, ratio<1, 1000>> milliseconds;
std::chrono::microseconds = typedef duration<int, ratio<1, 1000000>> microseconds;
std::chrono::nanoseconds = typedef duration<int, ratio<1, 1000000000>> nanoseconds;

不同單位的轉換  std::chrono::duration_cast,例子

std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds> (std::chrono::milliseconds ms);

將當前milliseconds轉成seconds

c的time_t 爲long 型,可以直接輸出,而durations需要使用count()方法輸出

 

Time points

std::chrono::system::clock::time_point today = std::chrono::system_clock::now();

time_t tt;

tt = system_clock::to_time_t ( today );
std::cout << "today is: " << ctime(&tt);

獲取now()的epoch時間,epoch即從1970年1月1日00:00:00以來的格林尼治時間,需要注意這裏的時區問題

std::chrono::system_clock::now().time_since_epoch()

Clocks

std::chrono::system_clock相當於C的CLOCK_REALTIME

std::chrono::steady_clock相當於C的CLOCK_MONOTONIC

std::chrono::high_resolution_clock 高精度版本,但是It may be a synonym for system_clock or steady_clock.[cplusplus.com]

三個static member functions

std::chrono::system_clock::now()  // get current time

C++時間與C時間的轉換

time_t std::chrono::system_clock::to_time_t(const time_point& tp);

time_point std::chrono::system_clock::from_time_t(time_t t)

UTC和CST的影響

time_since_epoch這個時間是獲取從1970.1.1以來UTC的duration。 就是說如果該機子使用的是UTC時間,那麼time_since_epoch就是UTC的duration,但是如果該機子使用了CST時間,那麼time_since_epoch.count()的具體過程:先將CST推算出UTC時間,即CST - 8個時區,之後得到就是現在的UTC時間和1970.1.1的UTC時間的duration。爲了避免同時區造成的影響,使用做差時間。本機用時間函數將1970.1.1轉成time_point,再用get_now()與之做差得到duration,去掉了時區的影響。如果本機直接使用UTC時間,time_since_epoch也是UTC,兩者單位都是UTC,自然沒有影響

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章