程式隨筆 timestamp(時間戳)

2021-08-03 18:41:30 字數 2213 閱讀 4453

在實際程式設計中,經常需要用到時間戳。因此,對linux上的一些時間api進行封裝,實現了個ctimestamp類,該類的內部用乙個int64_t型別來儲存時間,時間精度為微秒。類過載了比較,提供了轉換為字串的方法。

1. timestamp.h

#ifndef __timestamp_h__

#define __timestamp_h__

#include #include #include class ctimestamp : public boost::less_than_comparable, public boost::equality_comparable;

//less_than_comparable

inline bool operator<(const ctimestamp //equality_comparable

inline bool operator==(const ctimestamp #endif //#ifndef __timestamp_h__

2. timestamp.cpp

#include #include #include #include "timestamp.h"

ctimestamp::ctimestamp()

:m_microseconds(0)

ctimestamp::ctimestamp(int64_t microseconds)

:m_microseconds(microseconds)

ctimestamp::~ctimestamp()

bool ctimestamp::empty()

void ctimestamp::swap(ctimestamp ×tamp)

int64_t ctimestamp::getmicroseconds() const

std::string ctimestamp::toformattedstring(bool blocal) const

; //gmtime()能夠把日曆時間轉換為乙個對應於utc的分解時間,gmtime_r()是它的可重入版

//原型:struct tm *gmtime_r(const time_t *timep, struct tm *result);

//localtime()需要考慮時區和夏令時設定,localtime_r()是它的可重入版本

//原型:struct tm *localtime_r(const time_t *timep, struct tm *result);

struct tm tm_time;

time_t second_time = static_cast(m_microseconds / ctimestamp::s_microsecondspersecond);

if (blocal)

localtime_r(&second_time, &tm_time);

else

gmtime_r(&second_time, &tm_time);

int imicroseconds = static_cast(m_microseconds % ctimestamp::s_microsecondspersecond);

snprintf(arrbuffer, sizeof(arrbuffer), "%04d-%02d-%02d %02d:%02d:%02d-%06d",

tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,

tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, imicroseconds);

return arrbuffer;

}ctimestamp ctimestamp::now()

3. main.cpp

#include #include "timestamp.h"

using namespace std;

int main(void)

else

return 0;

}

輸出如下:

oldtimestamp : 2017-07-12 22:12:35-880046

newtimestamp : 2017-07-12 22:12:40-881864

newtimestamp > oldtimestamp

時間戳TimeStamp處理

我獲得這個時間戳是得想除以1000再處理的,看看你們的需要先除多少再處理 時間戳處理 nsinteger time timestamp 1000 nsnumber timer nsnumber numberwithinteger time nstimeinterval interval timer ...

TimesTen 時間戳 timestamp 用法

很多時候我們需要對資料庫進行增量更新,比如從timesten資料庫匯入資料到mysql資料庫中。為了保證匯入是增量的,我們必須跳過那些沒有 被修改過的資料,現在比較流行的方法是使用時間戳,也就是定義乙個型別為timestamp的新列。當我們對這個表增加或刪除行時,必須更新這個列的資料,以反映行被更新...

MySQL 時間戳(Timestamp)函式

1.mysql 獲得當前時間戳函式 current timestamp,current timestamp mysql select current timestamp,current timestamp current timestamp current timestamp 2008 08 09 ...