mardi 21 juillet 2020

Does Howard Hinnant's date::parse() function work with floating-point durations?

I'm trying to parse 2020-03-25T08:27:12.828Z into std::chrono::time_point<system_clock, duration<double>> using Howard Hinnant's Date library.

It is expected that the following code outputs two identical strings:

#include "date.h"
#include <chrono>
#include <string>
#include <iostream>

using namespace std;
using namespace std::chrono;
using namespace date;

int main() {
  double d = 1585124832.828;

  time_point<system_clock, duration<double>> t{duration<double>{d}}, t1;

  string s {format("%FT%TZ", t) };
  cout << s << "\n";

  stringstream ss {s};
  ss >> parse("%FT%TZ", t1);
  cout << format("%FT%TZ", t1) << "\n";

}

But I get:

2020-03-25T08:27:12.828000Z
1970-01-01T00:00:00.000000Z

When I declare t and t1 as follows:

time_point<system_clock, milliseconds> t{duration_cast<milliseconds>(duration<double>{d})}, t1;

the code works as expected i.e. it outputs two identical lines

Aucun commentaire:

Enregistrer un commentaire