獲取當(dāng)前時間最簡單的函數(shù)方法
#include <ctime>
time_t time(time_t* timer);
注:time_t是長整形類型(long int)似舵,返回值為返回距計算機(jī)元年的秒數(shù),一般timer置為NULL贸弥。
使用C++標(biāo)準(zhǔn)庫函數(shù)
參考:C++時間函數(shù)標(biāo)準(zhǔn)庫
函數(shù)原型
#include <ctime>
// time_t是長整形類型(long int)
struct tm
{
int tm_sec; // 秒 – 取值區(qū)間為[0,59]
int tm_min; // 分 - 取值區(qū)間為[0,59]
int tm_hour; // 時 - 取值區(qū)間為[0,23]
int tm_mday; // 一個月中的日期 - 取值區(qū)間為[1,31]
int tm_mon; // 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11]
int tm_year; // 年份,其值等于實際年份減去1900
int tm_wday; // 星期 – 取值區(qū)間為[0,6]缚俏,其中0代表星期天,1代表星期一贮乳,以此類推
int tm_yday; // 從每年的1月1日開始的天數(shù) – 取值區(qū)間為[0,365]忧换,其中0代表1月1日以此類推
int tm_isdst; // 夏令時標(biāo)識符,實行夏令時的時候向拆,tm_isdst為正亚茬。不實行夏令時的時候,tm_isdst為0
};
// 獲取當(dāng)前時間浓恳,返回值為距計算機(jī)元年的秒數(shù)
time_t time (time_t* timer);
// 將tm結(jié)構(gòu)體的時間轉(zhuǎn)換為time_t
time_t mktime (struct tm * timeptr);
// 將tm結(jié)構(gòu)體的時間轉(zhuǎn)換為時間字符串
char* asctime (const struct tm * timeptr);
// 將time_t時間轉(zhuǎn)換為時間字符串
char* ctime (const time_t * timer);
// 將time_t時間轉(zhuǎn)換為世界統(tǒng)一tm結(jié)構(gòu)體時間
struct tm * gmtime (const time_t * timer);
// 將time_t時間轉(zhuǎn)換位tm結(jié)構(gòu)體時間
struct tm * localtime (const time_t * timer);
// 將tm結(jié)構(gòu)體字符串轉(zhuǎn)化為格式化時間字符串
size_t strftime (char* ptr, size_t maxsize, const char* format, const struct tm* timeptr);
注:strftime格式化函數(shù)的format格式
%a 星期幾的簡寫
%A 星期幾的全稱
%b 月分的簡寫
%B 月份的全稱
%c 標(biāo)準(zhǔn)的日期的時間串
%C 年份的后兩位數(shù)字
%d 十進(jìn)制表示的每月的第幾天
%D 月/天/年
%e 在兩字符域中刹缝,十進(jìn)制表示的每月的第幾天
%F 年-月-日
%g 年份的后兩位數(shù)字,使用基于周的年
%G 年分颈将,使用基于周的年
%h 簡寫的月份名
%H 24小時制的小時
%I 12小時制的小時
%j 十進(jìn)制表示的每年的第幾天
%m 十進(jìn)制表示的月份
%M 十時制表示的分鐘數(shù)
%n 新行符
%p 本地的AM或PM的等價顯示
%r 12小時的時間
%R 顯示小時和分鐘:hh:mm
%S 十進(jìn)制的秒數(shù)
%t 水平制表符
%T 顯示時分秒:hh:mm:ss
%u 每周的第幾天梢夯,星期一為第一天 (值從0到6,星期一為0)
%U 第年的第幾周晴圾,把星期日做為第一天(值從0到53)
%V 每年的第幾周颂砸,使用基于周的年
%w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)
%W 每年的第幾周死姚,把星期一做為第一天(值從0到53)
%x 標(biāo)準(zhǔn)的日期串
%X 標(biāo)準(zhǔn)的時間串
%y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)
%Y 帶世紀(jì)部分的十制年份
%z人乓,%Z 時區(qū)名稱,如果不能得到時區(qū)名稱則返回空字符都毒∩#
%% 百分號
C++時間標(biāo)準(zhǔn)庫函數(shù)之間的關(guān)系
示例代碼
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
int main()
{
time_t curTime;
time_t tmToCurTime;
struct tm *utcTm;
struct tm *curTm;
char szTime[40];
// 獲取當(dāng)前時間
curTime = time(NULL);
cout << "Cur Time: " << curTime << endl;
// 將time_t時間轉(zhuǎn)換為世界統(tǒng)一struct tm結(jié)構(gòu)體時間
utcTm = gmtime(&curTime);
// 將time_t時間轉(zhuǎn)換為struct tm結(jié)構(gòu)體時間
curTm = localtime(&curTime);
// 將struct tm結(jié)構(gòu)體時間轉(zhuǎn)換為時間字符串
cout << "struct tm to time string: " << asctime(curTm) << endl;
// 將struct tm結(jié)構(gòu)體時間轉(zhuǎn)換為time_t
cout << "struct tm to time_t: " << mktime(curTm) << endl;
// 將time_t字符串轉(zhuǎn)換為時間字符串
cout << "time_t to time string: " << ctime(&curTime) << endl;
// 將struct tm結(jié)構(gòu)體時間轉(zhuǎn)為格式化時間字符串
memset(szTime, 0, sizeof(szTime));
strftime(szTime, sizeof(szTime) - 1, "%F %T", curTm);
cout << "Format time string: " << szTime << endl;
return 0;
}
執(zhí)行結(jié)果
Cur Time: 1474986855
struct tm to time string: Tue Sep 27 22:34:15 2016
struct tm to time_t: 1474986855
time_t to time string: Tue Sep 27 22:34:15 2016
Format time string: 2016-09-27 22:34:15
使用linux系統(tǒng)函數(shù)
參考:Linux系統(tǒng)函數(shù)標(biāo)準(zhǔn)庫
函數(shù)原型
#include<sys/time.h>
// timeval 結(jié)構(gòu)體
struct timeval
{
long tv_sec; //秒
long tv_usec; //微秒
};
// timezone 結(jié)構(gòu)體
struct timezone
{
int tz_minuteswest; // 和Greenwich 時間差了多少分鐘
int tz_dsttime; // DST時間的修正方式
};
// 獲取當(dāng)前時間
int gettimeofday(struct timeval*tv, struct timezone *tz);
示例代碼
#include <iostream>
#include <sys/time.h>
using namespace std;
int main()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
// 返回秒
cout << "seconds: " << tv.tv_sec << endl;
// 返回毫秒
cout << "microseconds: " << tv.tv_usec << endl;
// 返回和Greenwich 時間差了多少分鐘
cout << tz.tz_minuteswest << endl;
// 返回DST時間的修正方式
cout << tz.tz_dsttime << endl;
return 0;
}
執(zhí)行結(jié)果
seconds: 1474989732
microseconds: 105807
0
0
綜合使用C++標(biāo)準(zhǔn)函數(shù)庫和Linux系統(tǒng)函數(shù)
格式化輸出微秒級當(dāng)前的時間(可在C++項目日志打印中使用)
#include <iostream>
#include <sys/time.h>
#include <sstream>
#include <cstring>
using namespace std;
string GetCurrentFormatTimeString()
{
struct timeval tv;
char timeArray[40];
stringstream ss;
gettimeofday(&tv, NULL);
memset(timeArray, 0, sizeof(timeArray));
strftime(timeArray, sizeof(timeArray) - 1, "%F %T", localtime(&tv.tv_sec));
ss << string(timeArray) << "." << tv.tv_usec;
return ss.str();
}
int main()
{
cout << GetCurrentFormatTimeString() << endl;
return 0;
}
執(zhí)行結(jié)果
2016-09-27 23:52:35.225960