I'm quite new to c++ so thera are a bunch of questions I've got but for now this one drives my crazy:
我对c++很陌生,所以我有很多问题,但是现在这个问题让我抓狂:
I've ot a json response and want to parse one object as long (because it's a timestamp). After that I want tp parse that long to a time_point object via
我没有json响应,想要解析一个对象(因为它是一个时间戳)。之后,我希望tp解析到一个time_point对象
chrono::system_clock::from_time_t(...);
So this is what I got for now:
这就是我现在得到的
auto last_change_date_long = (long long)json_troubleticket["lastChangeDate"].int_value();
time_t last_change_date_raw = time_t(last_change_date_long);
auto last_change_date = chrono::system_clock::from_time_t(last_change_date_raw);
It compiles, but if i run this (while I know the value for lastChangeDate is 1480702672000
) it's result is 2147483647000
...
它编译,但是如果我运行这个(虽然我知道lastChangeDate的值是1480702672000),结果是2147483647000…
Does anyone have a suggestion what went wrong?
有人有什么问题吗?
1 个解决方案
#1
1
This will do it:
这将会这么做:
auto i = 1480702672000;
std::chrono::system_clock::time_point tp{std::chrono::milliseconds{i}};
Note that the above is not guaranteed to work by the standard because the epoch of system_clock
is unspecified. However all implementations are currently using Unix Time, and I have an informal agreement with the implementors that they will not deviate from this while I try to standardize this existing practice.
注意,由于没有指定system_clock的历元,因此不能保证上述标准能够正常工作。然而,目前所有的实现都在使用Unix时间,我与实现人员达成了非正式的协议,在我尝试对现有实践进行标准化时,他们不会偏离这一点。
The reason you're seeing the behavior you have is that your json is counting milliseconds since 1970-01-01 00:00:00 UTC, but time_t
typically counts seconds (though that is also not specified by the standard). So at the point where you create last_change_date_raw
from last_change_date_long
, you're implicitly converting milliseconds to seconds. This would result in a date midway through the year 48891. The implementation of from_time_t
is likely freaking out about that (overflowing).
看到这种行为的原因是,您的json从1970-01-01 00:00:00 UTC开始计算毫秒数,但是time_t通常计算秒数(尽管标准也没有指定)。因此,当您从last_change_date_long创建last_change_raw时,您将隐式地将毫秒转换为秒。这将导致在48891年中期的一个日期。from_time_t的实现可能会因为这个(溢出)而抓狂。
Fwiw, this particular time point represents:
Fwiw,这个特定的时间点代表:
2016-12-02 18:17:52.000 UTC
#1
1
This will do it:
这将会这么做:
auto i = 1480702672000;
std::chrono::system_clock::time_point tp{std::chrono::milliseconds{i}};
Note that the above is not guaranteed to work by the standard because the epoch of system_clock
is unspecified. However all implementations are currently using Unix Time, and I have an informal agreement with the implementors that they will not deviate from this while I try to standardize this existing practice.
注意,由于没有指定system_clock的历元,因此不能保证上述标准能够正常工作。然而,目前所有的实现都在使用Unix时间,我与实现人员达成了非正式的协议,在我尝试对现有实践进行标准化时,他们不会偏离这一点。
The reason you're seeing the behavior you have is that your json is counting milliseconds since 1970-01-01 00:00:00 UTC, but time_t
typically counts seconds (though that is also not specified by the standard). So at the point where you create last_change_date_raw
from last_change_date_long
, you're implicitly converting milliseconds to seconds. This would result in a date midway through the year 48891. The implementation of from_time_t
is likely freaking out about that (overflowing).
看到这种行为的原因是,您的json从1970-01-01 00:00:00 UTC开始计算毫秒数,但是time_t通常计算秒数(尽管标准也没有指定)。因此,当您从last_change_date_long创建last_change_raw时,您将隐式地将毫秒转换为秒。这将导致在48891年中期的一个日期。from_time_t的实现可能会因为这个(溢出)而抓狂。
Fwiw, this particular time point represents:
Fwiw,这个特定的时间点代表:
2016-12-02 18:17:52.000 UTC