Incorrect result of operator<< for time_point when GMT time zone is set
- Dominant language
- C++
- Stars
- 20
- Forks
- 58
- PR merge metrics
- No merged PRs in 30d
Description
I discovered that Boost Chrono IO can occasionally add an incorrect time value (represented by a time _point object) to the stream. The problem occurs for the hour in which the local time shifts from standard to DST (summer time). Because the time is always displayed in GMT, it should not be affected by any changes in the local time zone, but in this case -- it is.
Example app:
```cpp
#include
#include
#include
using namespace boost::chrono;
static void print(const system_clock::time_point& tp)
{
std::cout << time_fmt(timezone::utc) <<
duration_cast(tp.time_since_epoch()).count() << ": " << tp <<
std::endl;
}
int main(int /*argc*/, char** /*argv*/)
{
const time_t ProblematicTP = 1048989600; // 03/30/2003 @ 2:00am (UTC)
print(system_clock::from_time_t(ProblematicTP - 1));
print(system_clock::from_time_t(ProblematicTP));
print(system_clock::from_time_t(ProblematicTP + 1));
print(system_clock::from_time_t(ProblematicTP + 3600));
return 0;
}
```
Compiled with boost 1.69, gcc 821 on fedora linux 29. Output:
```
1048989599: 2003-03-30 01:59:59.000000000 +0000
1048989600: 2003-03-30 03:00:00.000000000 +0000
1048989601: 2003-03-30 03:00:01.000000000 +0000
1048993200: 2003-03-30 03:00:00.000000000 +0000
```
I tested this also on VS2013 on Windows 10 and the problem occurred there as well, but instead of jumping to 3 o’clock, it jumped to 1 o’clock. It seems that the incorrect time display is affected by the local time zone. Therefore the above example may give correct results in some time zones and incorrect in others.
After using the debugger you can see that the problem arises from the use of the mktime function, which should operate only on local time, and in the code it does not: (boost 1.69, time_point_io.hpp:968).
```
if (gmtime_r(&t, &tm) == 0) failed = true;
tm.tm_isdst = -1;
(void)mktime(&tm);
```
After the call to gmtime_r, the tm struct has correct values (hour == 2), but after calling mktime its hour value equal to 3
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.