文章目錄
一、源代碼說明
freecplus是一個Linux系統下的C/C++開源框架,源代碼請前往C語言技術網(www.freecplus.net)下載。
本文介紹的是freecplus框架的時間操作函數。
函數和類的聲明文件是freecplus/_freecplus.h。
函數和類的定義文件是freecplus/_freecplus.cpp。
示例程序位於freecplus/demo目錄中。
編譯規則文件是freecplus/demo/makefile。
二、計算機時間的表示方法
UNIX操作系統根據計算機產生的年代和應用採用1970年1月1日作爲UNIX的紀元時間,1970年1月1日0點作爲計算機表示時間的是中間點,將從1970年1月1日開始經過的秒數用一個整數存放,這種高效簡潔的時間表示方法被稱爲“Unix時間紀元”,向左和向右偏移都可以得到更早或者更後的時間。
在實際開發中,對日期和時間的操作場景非常多,例如程序啓動和退出的時間,程序執行任務的時間,數據生成的時間,數據處理的各環節的時間等等。
在Linux系統中,自定義了time_t類型,如下:
typedef long time_t; // 時間值time_t爲長整型long的別名。
三、獲取操作系統的時間
取操作系統的時間,並把整數表示的時間轉換爲字符串表示的格式。
函數聲明:
void LocalTime(char *out_stime,const char *in_fmt=0,const int in_interval=0);
參數說明:
stime:用於存放獲取到的時間字符串。
timetvl:時間的偏移量,單位:秒,0是缺省值,表示當前時間,30表示當前時間30秒之後的時間點,-30表示當前時間30秒之前的時間點。
fmt:輸出時間的格式,fmt每部分的含義:“yyyy”-年份;“mm”-月份;“dd”-日期;“hh24”-小時;“mi”-分鐘;“ss”-秒,缺省是"yyyy-mm-dd hh24:mi:ss",目前支持以下格式:
"yyyy-mm-dd hh24:mi:ss"
"yyyymmddhh24miss"
"yyyy-mm-dd"
"yyyymmdd"
"hh24:mi:ss"
"hh24miss"
"hh24:mi"
"hh24mi"
"hh24"
"mi"
注意:
1)小時的表示方法是hh24,不是hh,這麼做的目的是爲了保持與數據庫的時間表示方法一致;
2)以上列出了常用的時間格式,如果不能滿足您應用開發的需求,請修改源代碼timetostr函數增加更多的格式支持;
3)調用函數的時候,如果fmt與上述格式都匹配,stime的內容將爲空。
示例(demo24.cpp)
/*
* 程序名:demo24.cpp,此程序演示freecplus框架中LocalTime時間函數的使用。
* 作者:C語言技術網(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"
int main()
{
char strtime[20];
memset(strtime,0,sizeof(strtime));
LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss",-30); // 獲取30秒前的時間。
printf("strtime1=%s\n",strtime);
LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss"); // 獲取當前時間。
printf("strtime2=%s\n",strtime);
LocalTime(strtime,"yyyy-mm-dd hh24:mi:ss",30); // 獲取30秒後的時間。
printf("strtime3=%s\n",strtime);
}
四、時間轉換函數
1、把整數表示的時間轉換爲字符串表示的時間
函數聲明:
void timetostr(const time_t ltime,char *stime,const char *fmt=0);
參數說明:
ltime:整數表示的時間。
stime:字符串表示的時間。
fmt:輸出字符串時間stime的格式,與LocalTime函數的fmt參數相同,如果fmt的格式不正確,stime將爲空。
2、把字符串表示的時間轉換爲整數表示的時間
函數聲明:
time_t strtotime(const char *stime);
參數說明:
stime:字符串表示的時間,格式不限,但一定要包括yyyymmddhh24miss,一個都不能少。
返回值:整數表示的時間,如果stime的格式不正確,返回-1。
示例(demo26.cpp)
/*
* 程序名:demo26.cpp,此程序演示freecplus框架中整數表示的時間和字符串表示的時間之間的轉換。
* 作者:C語言技術網(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"
int main()
{
time_t ltime;
char strtime[20];
memset(strtime,0,sizeof(strtime));
strcpy(strtime,"2020-01-01 12:35:22");
ltime=strtotime(strtime); // 轉換爲整數的時間
printf("ltime=%ld\n",ltime); // 輸出ltime=1577853322
memset(strtime,0,sizeof(strtime));
timetostr(ltime,strtime,"yyyy-mm-dd hh24:mi:ss"); // 轉換爲字符串的時間
printf("strtime=%s\n",strtime); // 輸出strtime=2020-01-01 12:35:22
}
五、時間的運算
把字符串表示的時間加上一個偏移的秒數後得到一個新的字符串表示的時間。
函數聲明:
bool AddTime(const char *in_stime,char *out_stime,const int timetvl,const char *fmt=0);
參數說明:
in_stime:輸入的字符串格式的時間。
out_stime:輸出的字符串格式的時間。
timetvl:需要偏移的秒數,正數往後偏移,負數往前偏移。
fmt:輸出字符串時間out_stime的格式,與LocalTime函數的fmt參數相同。
注意:in_stime和out_stime參數可以是同一個變量的地址,如果調用失敗,out_stime的內容會清空。
返回值:true-成功,false-失敗,如果返回失敗,可以認爲是in_stime的格式不正確。
示例(demo28.cpp)
/*
* 程序名:demo28.cpp,此程序演示freecplus框架中採用AddTime進行時間的運算。
* 作者:C語言技術網(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"
int main()
{
time_t ltime;
char strtime[20];
memset(strtime,0,sizeof(strtime));
strcpy(strtime,"2020-01-01 12:35:22");
AddTime(strtime,strtime,0-1*24*60*60); // 減一天。
printf("strtime=%s\n",strtime); // 輸出strtime=2019-12-31 12:35:22
AddTime(strtime,strtime,2*24*60*60); // 加兩天。
printf("strtime=%s\n",strtime); // 輸出strtime=2020-01-02 12:35:22
}
六、計時器
CTimer類是一個精確到微秒的計時器。
類聲明:
// 這是一個精確到微秒的計時器。
class CTimer
{
private:
struct timeval m_start; // 開始計時的時間。
struct timeval m_end; // 計時完成的時間。
// 開始計時。
void Start();
public:
CTimer(); // 構造函數中會調用Start方法。
// 計算已逝去的時間,單位:秒,小數點後面是微秒。
double Elapsed();
};
CTimer創建對象後立即開始計時,每次調用Elapsed方法獲取已逝去的時間(單位:秒,小數點後面是微秒),並重新開始計時。
示例(demo29.cpp)
/*
* 程序名:demo29.cpp,此程序演示freecplus框架中的CTimer類(計時器)的用法。
* 作者:C語言技術網(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"
int main()
{
CTimer Timer;
printf("elapsed=%lf\n",Timer.Elapsed());
sleep(1);
printf("elapsed=%lf\n",Timer.Elapsed());
sleep(1);
printf("elapsed=%lf\n",Timer.Elapsed());
usleep(1000);
printf("elapsed=%lf\n",Timer.Elapsed());
usleep(100);
printf("elapsed=%lf\n",Timer.Elapsed());
sleep(10);
printf("elapsed=%lf\n",Timer.Elapsed());
}
運行效果
從demo29運行的效果上看,好像計時有誤差,同樣是睡1秒,實際耗時卻是1.000126或1.000171,這是因爲程序本身執行需要時間,雖然時間很短,那也是需要時間。
七、版權聲明
C語言技術網原創文章,轉載請說明文章的來源、作者和原文的鏈接。
來源:C語言技術網(www.freecplus.net)
作者:碼農有道
如果文章有錯別字,或者內容有錯誤,或其他的建議和意見,請您留言指正,非常感謝!!!