VC++时间函数的使用(附源码)

1、通过time函数获取当前的时间

       time函数返回的是64位UTC时间,然后再调用localtime函数转换成本地的北京时间(东八区时间),该函数的北京时间存放到tm结构体:

struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };

然后我们取tm结构体中的字段格式化一下就得到系统当前时间了,代码如下:

__time64_t n64Time = ::time(NULL);
tm tmCur = *localtime(&n64Time);

CString strCurTime;
strTime.Format( _T("%04d-%02d-%02d_%02d:%02d:%02d"), tmCur.t

猜你喜欢

转载自blog.csdn.net/chenlycly/article/details/125128879