UNIX时间戳

UNIX日期时间,一个整数,从1970-01-01 开始的秒数,int64位

标准UTC时间,

世界统一时间,世界标准时间,国际协调时间,简称UTC

不属于任意时区

中国大陆、中国香港、中国澳门、中国台湾、蒙古国、新加坡、马来西亚、菲律宾、西澳大利亚州的时间与UTC的时差均为+8,也就是UTC+8。

时区,北京是东8区,就是要加 8个小时,8*60*60=28800

因此,获取北京时间的时间戳为

1
2
3
4
5
6
7
8
#include <iostream>
#include <time.h>

int main()
{
time_t t = time(nullptr) + 28800;
return 0;
}

然后,如果要获取当天的0时0分0秒,要对一天的秒数24 * 60 * 60 = 86400取模,即

1
2
3
4
5
6
7
8
#include <iostream>
#include <time.h>

int main()
{
time_t t = (time(nullptr) + 28800) % 86400;
return 0;
}