date icon indicating copy to clipboard operation
date copied to clipboard

Ostream insert local_time on Visual Studio 2022 with C++20

Open tonygould opened this issue 2 years ago • 4 comments

There is a compile failure on Visual Studio 2022 with C++20. Repro

    constexpr date::local_time<date::days> d{date::year{2015} / date::March / 22};
    std::ostringstream& os;
    os << d;

leads to

error C2593: 'operator <<' is ambiguous
date.h(4221): could be 'std::basic_ostream<...> &date::operator <<...
or       'std::basic_ostream<...> &std::chrono::operator <<...

I have a fix (on a local branch but I can't seem to create remote branch). Would be good to incorporate this. I'd like to have the exact same date/timezone code on both windows and linux without worrying about platform/compiler-specific differences (and having control over the timezone database).

diff --git a/include/date/date.h b/include/date/date.h                                     
index 6960e8c..602b7da 100644                                                              
--- a/include/date/date.h                                                                  
+++ b/include/date/date.h                                                                  
@@ -4228,7 +4228,7 @@ inline                                                               
 std::basic_ostream<CharT, Traits>&                                                        
 operator<<(std::basic_ostream<CharT, Traits>& os, const local_time<Duration>& ut)         
 {                                                                                         
-    return (os << sys_time<Duration>{ut.time_since_epoch()});                             
+    return date::operator<<(os, sys_time<Duration>{ut.time_since_epoch()});               
 }                                                                                         
                                                                                           
 namespace detail                                                                                                                                                                     

tonygould avatar Jan 27 '23 09:01 tonygould

I'm very reluctant to start down this path. There are many places where this library will conflict with C++20 chrono. And I'm really not in a position to develop and maintain such a project. For cross-platform development I recommend a namespace alias that you can switch between date and std::chrono with a macro flag.

On control of the timezone database: I am working on a project to create my own database format, and (if successful) it will be interoperable with C++20's std::chrono::zoned_time. However it is still in the very early stages of development, and I'm still unsure if it will turn into a viable tool.

HowardHinnant avatar Jan 29 '23 22:01 HowardHinnant

Understood. We use vcpkg and I have integrated the patch above into our local system. Once we have it working in our system, we will update the vcpkg port of date. As it stands, with the patch, all our tests of our code using the date library pass on both vs2019/C++17, vs2022/C++17 and 20. On linux, they pass on both gcc10.3/C++17 and gcc12.2/C++17 and 20. We are not using the timezone piece yet, but intend to.

Creation of your own database format sounds positive. We'd like to have our own control of the timezone database, i.e. decide when we update it rather than the config being "random" (e.g. when we happen to rebuild a docker image or when updates hit IANA). Running the date library in manual mode (I also intend to make manual mode available in the vcpkg port), reading in the current text files takes of the order of 20ms, which is pretty good actually :), but some sort of binary format would clearly be a win.

To be clear for cross-platform development, I want to use the exact same code on both platforms, i.e. your library. Even if it were acceptable to have the possibility of different algorithms for provision of timezone database on linux and windows, my understanding is that the database is not available until windows server 2022. And not until gcc 13 for linux (what I read of the gcc implementation looks great, e.g. the ability to parameterise where the database will be read from).

tonygould avatar Jan 29 '23 22:01 tonygould

Thanks for all of this information. It is really helpful.

HowardHinnant avatar Jan 29 '23 22:01 HowardHinnant

Thanks for the date library! Links for the above: https://learn.microsoft.com/en-us/cpp/standard-library/time-zone-class?view=msvc-170#requirements, https://www.reddit.com/r/cpp/comments/zt1o9k/libstdc13_gets_c20_chrono/.

tonygould avatar Jan 30 '23 09:01 tonygould