C中的时间函数的用法
这个类展示了C语言中的时间函数的常用的用法。
源代码:
#include <ctime>
#include <iostream>
using namespace std;
class MyTime
{
public:
MyTime() { mPTime = 0; mStLocalTime = 0; mStGMTTime = 0; }
~MyTime() {};
//time_t time(time_t * timer) 返回自1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
time_t GetTime() { return time(0); }
//time_t time(time_t * timer) 将自1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数设定到t指向的一个长整形变量里。
time_t GetTimeB() { time(&mPTime); return mPTime; }
//struct tm *localtime(const time_t *clock) 可以将时间戳转换为一个时间结构体,且时间为经过时区转化的本地时间。
int GetYear() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_year; }
int GetMonth() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_mon; }
int GetDate() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_mday; }
int GetHour() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_hour; }
int GetMinute() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_min; }
int GetSecond() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_sec; }
int GetWeekDay() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_wday; }
int GetYearDay() { GetTimeB(); mStLocalTime = localtime(&mPTime); return mStLocalTime->tm_yday; }
//char *asctime(const struct tm *tblock) 将换日期和时间的结构体转换为相应的字符串。
char *GetTimeStr() { GetTimeB(); mStLocalTime = localtime(&mPTime); return asctime(mStLocalTime); }
//char *ctime(const time_t *time) 将时间戳转换为响应的字符串。
char *GetLocalTimeStr2() { GetTimeB(); return ctime(&mPTime); }
//tzset() 为设置时区。
//struct tm *gmtime(long *clock) 把日期和时间转换为格林威治(GMT)时间。
char *GetGMTTimeStr() { tzset(); GetTimeB(); mStGMTTime = gmtime(&mPTime); return asctime(mStGMTTime); }
//time_t mktime(strcut tm * timeptr) 将struct tm格式的时间转换为时间戳。
time_t GetTime3() { time(&mPTime); mStLocalTime = localtime(&mPTime); return mktime(mStLocalTime); }
//double difftime(time_t time1, time_t time0) 计算时间间隔才长度,以秒为单位,且只能精确到秒
double CalDuration(time_t t1, time_t t2) { return difftime(t1,t2); }
//size_t strftime(char *strDest,size_t maxsize,const char *format,const struct tm *timeptr);
//将时间格式化,或者说:格式化一个时间字符串。
//根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。
void FormatTime( char *str) { time(&mPTime); strftime(str,100,"%Y-%m-%d",localtime(&mPTime)); }
private:
time_t mPTime; //time_t 实际上是个长整形,用于保存自1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
struct tm *mStLocalTime;
struct tm *mStGMTTime;
};
int main(int argc, char *argv[])
{
cout<<"process begin @["<<(void*)&main<<"]"<<endl;
MyTime myTime;
cout<<"current Time 1 is ["<<myTime.GetTime()<<"]"<<endl;
cout<<"current Time 2 is ["<<myTime.GetTimeB()<<"]"<<endl;
cout<<"current Time 3 is ["
<<myTime.GetYear() + 1900<<"-"<<myTime.GetMonth()<<"-"<<myTime.GetDate()<<" "
<<myTime.GetHour()<<":"<<myTime.GetMinute()<<":"<<myTime.GetSecond()<<"]"<<endl;
cout<<"current Time week day is ["<<myTime.GetWeekDay()<<"]"<<endl;
cout<<"current Time year day is ["<<myTime.GetYearDay()<<"]"<<endl;
cout<<"current Time 4 is ["<<myTime.GetTimeStr()<<"]"<<endl;
cout<<"current Time 5 is ["<<myTime.GetLocalTimeStr2()<<"]"<<endl;
cout<<"current Time 6 (GMT time) is ["<<myTime.GetGMTTimeStr()<<"]"<<endl;
cout<<"current Time 7 is ["<<myTime.GetTime3()<<"]"<<endl;
cout<<"calculate duration of ["<<myTime.GetTime3()<<"] and ["<<myTime.GetTime3() - 1<<"] is ["
<<myTime.CalDuration(myTime.GetTime3(), myTime.GetTime3() - 1 )<<"]"<<endl;
char szTime[20] = {0};
myTime.FormatTime(szTime);
cout<<"current Time 8 is ["<<szTime<<"]"<<endl;
return 0;
}
输出结果:
process begin @[0x4016b0]
current Time 1 is [1416756697]
current Time 2 is [1416756697]
current Time 3 is [2014-10-23 23:31:37]
current Time week day is [0]
current Time year day is [326]
current Time 4 is [Sun Nov 23 23:31:37 2014
]
current Time 5 is [Sun Nov 23 23:31:37 2014
]
current Time 6 (GMT time) is [Sun Nov 23 15:31:37 2014
]
current Time 7 is [1416756697]
calculate duration of [1416756697] and [1416756696] is [1.41676e+009]
current Time 8 is [2014-11-23]