简体   繁体   English

在C ++中将星期几和星期几转换为日期时间

[英]Convert hour of the day and day of the week into datetime in C++

If I have hour of the day and day of the week as 1am and Monday. 如果我将一天中的小时和星期几设为凌晨1点和星期一。 How can I convert these into a datetime value in C++? 如何在C ++中将它们转换为日期时间值? thanks in advance. 提前致谢。

Regards, Jack 问候,杰克

may be this can help you: 可能这可以帮助您:

boost.org boost.org

2am on Sunday occurs at least once a week (twice when switching to/from daylight saving!). 星期日凌晨2点至少每周发生一次(切换为夏时制时为两次!)。

So you need the month and day in that month. 因此,您需要该月中的月份和日期。

Time in standard C/C++ can be represented with std::tm 标准C / C ++中的时间可以用std :: tm表示

Structure holding a calendar date and time 具有日历日期和时间的结构

std::tm tm;
tm.tm_wday = 1; // monday
tm.tm_hour = 1; // 1 am

How to get local time ? 如何获得当地时间?

#include <ctime>
...
time_t rawtime = time(&rawtime);
struct tm *tnow = localtime(&rawtime);

Maybe a simpler option would be to use C++11 time_point (must have compatible compiler) 也许更简单的选择是使用C ++ 11 time_point (必须具有兼容的编译器)

system_clock::time_point now = system_clock::now();

then go back to a tm object 然后回到一个tm对象

std::tm* tnow = localtime(now);

Time difference 时间差异

struct tm diff = difftime(mktime(&tnow),mktime(&tm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM