提问人:Antonio 提问时间:1/8/2023 最后编辑:Daniel A. WhiteAntonio 更新时间:1/8/2023 访问量:55
mktime 和 gmtime_r 之间的转换问题
Problems with conversion between mktime and gmtime_r
问:
如果我使用 定义一个分解时间,然后将其转换为 using,结果会尝试再次返回,因为我认为我会得到相同的结果。struct tm
time_t
mktime
struct tm
好吧,也许第一次发生更改,因为我定义了可能会影响该值,但第二次不会。tm_isdst = -1
mktime
以下代码执行此操作:
void print(const tm& t)
{
std::cout << t.tm_mday << '/'
<< t.tm_mon << '/'
<< t.tm_year << ' '
<< t.tm_hour << ':'
<< t.tm_min << ':'
<< t.tm_sec << "; "
<< t.tm_wday << '|'
<< t.tm_isdst << '\n';
}
int main(){
tm t;
t.tm_sec = 32;
t.tm_min = 22;
t.tm_hour = 0;
t.tm_mday = 6;
t.tm_mon = 0;
t.tm_year = 2023 - 1900;
t.tm_wday = 5;
t.tm_isdst = -1;
print(t);
time_t timet0 = mktime(&t);
std::cout << "time_t0 = " << timet0 << '\n';
gmtime_r(&timet0, &t);
print(t);
time_t timet1 = mktime(&t);
std::cout << "time_t1 - time_t0 = " << (timet1 - timet0) << '\n';
std::cout << "time_t1 = " << timet1 << '\n';
gmtime_r(&timet1, &t);
print(t);
timet1 = mktime(&t);
std::cout << "time_t = " << timet1 << '\n';
gmtime_r(&timet1, &t);
print(t);
}
执行它的结果是
6/0/123 0:22:32; 5|-1
time_t0 = 1672960952
5/0/123 23:22:32; 4|0
time_t1 - time_t0 = -3600
time_t1 = 1672957352
5/0/123 22:22:32; 4|0
time_t = 1672953752
5/0/123 21:22:32; 4|0
为什么我每次打电话给这对夫妇时都会损失一个小时?mktime/gmtime_r
答: 暂无答案
评论
mktime
localtime
gmtime
localtime
gmtime
gmtime
timegm