@@ -42,12 +42,12 @@ WINRT_EXPORT namespace winrt
4242
4343 static time_t to_time_t (time_point const & time) noexcept
4444 {
45- return std::chrono::duration_cast<time_t_duration>( time - time_t_epoch). count ( );
45+ return std::chrono::system_clock::to_time_t ( to_sys ( time) );
4646 }
4747
4848 static time_point from_time_t (time_t time) noexcept
4949 {
50- return time_t_epoch + time_t_duration{ time } ;
50+ return from_sys ( std::chrono::system_clock::from_time_t ( time)) ;
5151 }
5252
5353 static file_time to_file_time (time_point const & time) noexcept
@@ -70,10 +70,25 @@ WINRT_EXPORT namespace winrt
7070 return from_file_time (time);
7171 }
7272
73+ template <typename Duration>
74+ static std::chrono::time_point<std::chrono::system_clock, std::common_type_t <Duration, std::chrono::seconds>>
75+ to_sys (std::chrono::time_point<clock, Duration> const & tp)
76+ {
77+ return epoch + tp.time_since_epoch ();
78+ }
79+
80+ template <typename Duration>
81+ static std::chrono::time_point<clock, std::common_type_t <Duration, std::chrono::seconds>>
82+ from_sys (std::chrono::time_point<std::chrono::system_clock, Duration> const & tp)
83+ {
84+ using result_t = std::chrono::time_point<clock, std::common_type_t <Duration, std::chrono::seconds>>;
85+ return result_t { tp - epoch };
86+ }
87+
7388 private:
7489
75- // Define 00:00:00, Jan 1 1970 UTC in FILETIME units .
76- static constexpr time_point time_t_epoch{ duration{ 0x019DB1DED53E8000 } };
77- using time_t_duration = std::chrono::duration< time_t > ;
90+ // system_clock epoch is 00:00:00, Jan 1 1970.
91+ // This is 11644473600 seconds after Windows FILETIME epoch of 00:00:00, Jan 1 1601.
92+ static constexpr std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> epoch{ std::chrono::seconds{ - 11644473600 } } ;
7893 };
7994}
0 commit comments