返回系统日期的C++函数

1 c++

我需要c ++中的函数,它允许我检索和存储系统日期.我有一个用于存储日期的课程.

Evg*_*zin 7

处理日期和时间很困难,这就是人们使用图书馆的原因.我更喜欢boost :: date_time.

boost::posix_time::ptime local_time = boost::posix_time::second_clock::local_time();
boost::gregorian::date d = local_time.date();
Run Code Online (Sandbox Code Playgroud)

d是当地时间的当前日期,它使用计算机时区设置.要获得UTC时间,可以使用boost :: posix_time :: second_clock :: universal_time().


jas*_*son 5

来自time.h:

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 */
};

time_t time(time_t * timer);
struct tm* gmtime(const time_t *timer);
struct tm* localtime(const time_t * timer);
Run Code Online (Sandbox Code Playgroud)