-1

This used to play once. I am trying to get some data from the C++ date library but an exception is caught. I am compiling with

-DUSE_AUTOLOAD=0 -DHAS_REMOTE_API=0 -DUSE_OS_TZDB=1

what is wrong with the code?

#include <iostream>
#include "date/tz.h"
#include <exception>

using namespace date;
using namespace std::chrono;

int main(int argc, char** argv) {


    try {

    auto current_time_zone = make_zoned("Europe/Athens", std::chrono::system_clock::now());

    auto current_day = date::format("%A", current_time_zone);
    auto current_time = date::format("%H:%M", current_time_zone);

    std::cout << "day: " << current_day << ", time: " << current_time << " in timezone: " << current_time_zone << std::endl;
    //std::cout << " in timezone: " << current_time_zone << std::endl;

    } catch ( std::exception& e) {
        std::cout << e.what() << std::endl;

    }

}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
cateof
  • 6,608
  • 25
  • 79
  • 153
  • I've just added the tag c++20, making this question on topic. The library the OP references is an example implementation of the C++20 draft specification. – Howard Hinnant Sep 23 '18 at 15:25
  • 2
    What is the message in the `.what()`? – Howard Hinnant Sep 23 '18 at 15:26
  • 2
    @SamVarshavchik: "*I'm curious what is the evidence that "the date library" being "quite popular".*" I don't know if it is genuinely "quite popular", but I think it being *voted into the standard library* for C++20 will improve its popularity ;) For those who keep abreast of C++ developments, it is known. Though the OP did not make it clear enough what he was talking about for those who have never heard of it. – Nicol Bolas Sep 23 '18 at 17:56
  • @HowardHinnant: Unknown error -1 – cateof Sep 23 '18 at 18:19
  • 1
    @cateof: I'm not positive what the problem is, but I can tell you that my lib doesn't throw an exception that contains the message "Unknown error". Try adding `-DONLY_C_LOCALE=1` to your build flags. This will avoid your std::lib's `time_put` facet, but will limit you to only the "C" locale. If this fixes the problem, then it is your std::lib's `std::time_put` facet that threw the exception. – Howard Hinnant Sep 23 '18 at 20:17

2 Answers2

1

You need to use the -pthread flag. tz.cpp uses call_once to do part of the initialisation. And without -pthread it's not going to work (as underneath it need something like __gthread_once). See this for more details.

You can verify if that's the problem by running your example with gdb (use the catch throw).

Anonymous
  • 18,162
  • 2
  • 41
  • 64
0

I'm not positive what the problem is, but I can tell you that this library does not throw an exception that contains the message "Unknown error".

Try adding -DONLY_C_LOCALE=1 to your build flags. This will avoid your std::lib's time_put facet, but will limit you to only the "C" locale. If this fixes the problem, then it is your std::lib's std::time_put facet that threw the exception.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577