From 8e2de8587ea1086efcfdeecc580f3e50eb3e5363 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sun, 28 Aug 2016 14:52:41 -0400 Subject: [PATCH] Eliminate dependence on OS's gmtime_s / gmtime_r When compiled with -arch i386, Apple's gmtime_s has a 32 bit bug meaning it can't format dates earlier than 1901-12-13 20:45:52. --- date.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/date.h b/date.h index cf28bd5..77cbe5c 100644 --- a/date.h +++ b/date.h @@ -4021,14 +4021,18 @@ format(const std::locale& loc, std::basic_string fmt, } auto& f = use_facet>(loc); basic_ostringstream os; - auto tt = system_clock::to_time_t( - floor(sys_time{tp.time_since_epoch()})); + auto ld = floor(tp); + auto ymd = year_month_day{ld}; + auto hms = make_time(floor(tp - ld)); std::tm tm{}; -#ifndef _MSC_VER - gmtime_r(&tt, &tm); -#else - gmtime_s(&tm, &tt); -#endif + tm.tm_sec = static_cast(hms.seconds().count()); + tm.tm_min = static_cast(hms.minutes().count()); + tm.tm_hour = static_cast(hms.hours().count()); + tm.tm_mday = static_cast(unsigned{ymd.day()}); + tm.tm_mon = static_cast(unsigned{ymd.month()} - 1); + tm.tm_year = int{ymd.year()} - 1900; + tm.tm_wday = static_cast(unsigned{weekday{ld}}); + tm.tm_yday = static_cast((ld - local_days{ymd.year()/1/1}).count()); f.put(os, os, os.fill(), &tm, fmt.data(), fmt.data() + fmt.size()); return os.str(); }