C++ 将日期时间转换为时间戳

suk*_*nej 1 c++ datetime

我知道这些价值观

unsigned char year = 17; // means 2017
unsigned char month = 8;
unsigned char day = 25;
unsigned char hour = 14;
unsigned char minute = 23;
unsigned char second = 54; 
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它们转换成unix时间戳?我不确定这是否unsigned char是表示该值的正确方法,我只需要每个值的大小为 1 个字节。

sch*_*312 6

Ubervan回答了您的问题

将日期分解为各个组成部分,即日、月、年,然后:

struct tm  tm;
time_t rawtime;
time ( &rawtime );
tm = *localtime ( &rawtime );
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
mktime(&tm);
Run Code Online (Sandbox Code Playgroud)

tm 现在可以转换为 time_t 并进行操作。

您的问题也在这里得到解决。