C++;返回系统日期的函数 我需要C++中的函数,允许我检索和存储系统日期。我有一个用于存储日期的类。

C++;返回系统日期的函数 我需要C++中的函数,允许我检索和存储系统日期。我有一个用于存储日期的类。,c++,C++,来自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] */

来自
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);

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);

<> P> Windows上的C++,尤其是Windows下的时间,

< P> C++在Windows上查看Windows时间,特别是./P>< P>处理日期和时间是困难的,这就是为什么人们使用库。我更喜欢boost::date\u time

boost::posix_time::ptime local_time = boost::posix_time::second_clock::local_time();
boost::gregorian::date d = local_time.date();

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

处理日期和时间很困难,这就是人们使用库的原因。我更喜欢boost::date\u time

boost::posix_time::ptime local_time = boost::posix_time::second_clock::local_time();
boost::gregorian::date d = local_time.date();

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

只需添加,
GetSystemTime
为您提供UTC时间,而要获取
时区
调整后的时间,您需要使用
GetLocalTime


WinBase time函数(通过windows.h)与time.h函数的另一个区别是windows time函数在1601之前一直是可靠的,而time.h仅在1900之前是可靠的。我不确定这是否是你需要考虑的问题。

< P>只是添加,<代码> GETStimeTime<代码>给了你UTC时间,而要获得<代码>时区< /代码>调整的时间,你需要使用<代码> GETLoalalTime< /Cord> WinBase time函数(通过windows.h)与time.h函数的另一个区别是windows time函数在1601之前一直是可靠的,而time.h仅在1900之前是可靠的。我不确定这是否是你需要考虑的问题。

时间()< /P> 但也可以查看localtime和asctime以查看显示


但也可以查看localtime和asctime的显示(
nowtm
用当前系统时间填充):

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

以下是我最后使用的(
nowtm
用当前系统时间填充):

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

几分钟前我写了非常相似的答案。您可以使用C++11中提供的chrono。

几分钟前我写了非常类似的答案。您可以使用C++11中提供的chrono。

请尝试澄清您的问题-我尽了最大努力,但很难理解您的意思。这样的问题能解释为什么C++程序员应该学习C。各种各样的答案是一个很好的解释,为什么提问者应该对他们使用的平台有明确的解释。请试着澄清你的问题——我尽了最大努力,但是很难理解你的意思。这样的问题对C++程序员为什么应该学习C的解释非常好,而且各种各样的答案对于为什么提问者应该对他们使用的平台有明确的解释是很好的。我也喜欢使用Boost库,而不是严格使用cTimes和相关联的。我也喜欢使用Boost库,而不是严格使用ctime和相关函数。