HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.14k stars 677 forks source link

Compiling with zoned_time visual studio #685

Open peterwilson136 opened 3 years ago

peterwilson136 commented 3 years ago

Hi, I am trying to compile with tz.cpp for the below code, and I am getting quite a lot of error messages. I have CURL linked and included in the project, as well as some steps in the installation included, i.e. #define NOMINMAX.

long double
SecondsSinceEpoch(const char* date)
{
    using namespace date;
    using namespace std;
    using namespace std::chrono;

    istringstream in{ date };
    local_days tpd;
    in >> parse("%F", tpd);
    if (in.fail())
        return -1;
    seconds s{};
    in >> parse(" %T", s);
    zoned_time zt{ current_zone(), tpd + s };
    return zt.get_sys_time().time_since_epoch().count();
}

tz.cpp is included in my source files, and the directory to the header files is part of the project too. The error messages I get are:

Severity    Code    Description Project File    Line    Suppression State
Error   C2641   cannot deduce template arguments for 'date::zoned_time' curl_test
Error   C2783   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(TimeZonePtr,const std::chrono::time_point<date::local_t,Duration> &)': could not deduce template argument for '<unnamed-symbol>'   curl_test       
Error   C2784   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(TimeZonePtr,const std::chrono::time_point<std::chrono::system_clock,Duration> &)': could not deduce template argument for 'const std::chrono::time_point<std::chrono::system_clock,Duration> &' from 'std::chrono::time_point<date::local_t,std::chrono::seconds>'     
Error   C2784   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(TimeZonePtr,const std::chrono::time_point<std::chrono::system_clock,Duration> &)': could not deduce template argument for 'const std::chrono::time_point<std::chrono::system_clock,Duration> &' from 'std::chrono::time_point<date::local_t,std::chrono::seconds>' curl_test
Error   C2780   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(const date::zoned_time<Duration2,TimeZonePtr> &) noexcept': expects 1 arguments - 2 provided   curl_test   
Error   C2780   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(std::string_view)': expects 1 arguments - 2 provided   curl_test   
Error   C2780   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(TimeZonePtr)': expects 1 arguments - 2 provided    curl_test   
Error   C2780   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(const std::chrono::time_point<std::chrono::system_clock,Duration> &)': expects 1 arguments - 2 provided    curl_test   
Error   C2780   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(void)': expects 0 arguments - 2 provided   curl_test   
Error   C2780   'date::zoned_time<Duration,TimeZonePtr> date::zoned_time(date::zoned_time<Duration,TimeZonePtr>)': expects 1 arguments - 2 provided curl_test   
Error   C2662   'std::chrono::time_point<std::chrono::system_clock,std::common_type<Duration,std::chrono::seconds>::type> date::zoned_time<Duration,TimeZonePtr>::get_sys_time(void) const': cannot convert 'this' pointer from 'date::zoned_time' to 'const date::zoned_time<Duration,TimeZonePtr> &'  curl_test   
HowardHinnant commented 3 years ago

If you're using the latest VS, you no longer need this library. It is implemented in <chrono>.

If you're using a version of VS that doesn't yet implement C++17 CTAD, then you'll need to explicitly supply the first template argument for zoned_time, which is a duration consistent with the second zoned_time constructor argument, for example:

    zoned_time<seconds> zt{ current_zone(), tpd + s };

For this particular example (seconds-precision), there also exists a convenience type alias that you could use:

    zoned_seconds zt{ current_zone(), tpd + s };
peterwilson136 commented 3 years ago

Ah yes, that fixes it, code compiles. If its alright, to extend this a little, the code crashes with dates such as "2000-01-01" 2000-01-01 00:00:00 at this line in tz.cpp

if (!file_exists(install))
 {
     std::string msg = "Timezone database not found at \"";
     msg += install;
     msg += "\"";
     throw std::runtime_error(msg);
 }

and I have tried even replacing current_zone() with

auto tz = locate_zone("Australia/Sydney");

but it still crashes at the same place.

HowardHinnant commented 3 years ago

You will need to follow these installation instructions: https://howardhinnant.github.io/date/tz.html#Installation, including the section marked "Windows specific:".