diff options
| author | lat9nq <22451773+lat9nq@users.noreply.github.com> | 2023-07-12 02:34:02 -0400 |
|---|---|---|
| committer | lat9nq <22451773+lat9nq@users.noreply.github.com> | 2023-07-12 02:34:02 -0400 |
| commit | 9fde7a84ea395660c29b510f4e8cb4ff9d92922f (patch) | |
| tree | 893fcca0318e4e7162f875e8f6d44d36f29ff942 /src/common/time_zone.cpp | |
| parent | 53d03b9a0ef86b1532002c1f03ff3ef17f4a0a71 (diff) | |
time_zone: Account for leap years
Protects against invalid hour offsets during transitions to years
following leap years.
Diffstat (limited to 'src/common/time_zone.cpp')
| -rw-r--r-- | src/common/time_zone.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index a25f8b040d..1ee63bf802 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp @@ -35,11 +35,13 @@ std::string GetDefaultTimeZone() { // Results are not comparable to seconds since Epoch static std::time_t TmSpecToSeconds(const struct std::tm& spec) { + const int year = spec.tm_year - 1; // Years up to now + const int leap_years = year / 4 - year / 100; std::time_t cumulative = spec.tm_year; - cumulative = cumulative * 365 + spec.tm_yday; // Years to days - cumulative = cumulative * 24 + spec.tm_hour; // Days to hours - cumulative = cumulative * 60 + spec.tm_min; // Hours to minutes - cumulative = cumulative * 60 + spec.tm_sec; // Minutes to seconds + cumulative = cumulative * 365 + leap_years + spec.tm_yday; // Years to days + cumulative = cumulative * 24 + spec.tm_hour; // Days to hours + cumulative = cumulative * 60 + spec.tm_min; // Hours to minutes + cumulative = cumulative * 60 + spec.tm_sec; // Minutes to seconds return cumulative; } |
