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.
This commit is contained in:
Howard Hinnant
2016-08-28 14:52:41 -04:00
parent 7816c3b48f
commit 8e2de8587e

18
date.h
View File

@@ -4021,14 +4021,18 @@ format(const std::locale& loc, std::basic_string<CharT, Traits> fmt,
} }
auto& f = use_facet<time_put<CharT>>(loc); auto& f = use_facet<time_put<CharT>>(loc);
basic_ostringstream<CharT, Traits> os; basic_ostringstream<CharT, Traits> os;
auto tt = system_clock::to_time_t( auto ld = floor<days>(tp);
floor<system_clock::duration>(sys_time<Duration>{tp.time_since_epoch()})); auto ymd = year_month_day{ld};
auto hms = make_time(floor<seconds>(tp - ld));
std::tm tm{}; std::tm tm{};
#ifndef _MSC_VER tm.tm_sec = static_cast<int>(hms.seconds().count());
gmtime_r(&tt, &tm); tm.tm_min = static_cast<int>(hms.minutes().count());
#else tm.tm_hour = static_cast<int>(hms.hours().count());
gmtime_s(&tm, &tt); tm.tm_mday = static_cast<int>(unsigned{ymd.day()});
#endif tm.tm_mon = static_cast<int>(unsigned{ymd.month()} - 1);
tm.tm_year = int{ymd.year()} - 1900;
tm.tm_wday = static_cast<int>(unsigned{weekday{ld}});
tm.tm_yday = static_cast<int>((ld - local_days{ymd.year()/1/1}).count());
f.put(os, os, os.fill(), &tm, fmt.data(), fmt.data() + fmt.size()); f.put(os, os, os.fill(), &tm, fmt.data(), fmt.data() + fmt.size());
return os.str(); return os.str();
} }