From 9dfb45148670f712f6d3cf7f5000a4b4e393d83d Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 25 Apr 2016 17:36:19 +0300 Subject: Fix timestamp from_string() compilation error appeared when compiled with clang++ and libc++ --- butl/timestamp.cxx | 3 +- tests/timestamp/driver.cxx | 76 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 24 deletions(-) diff --git a/butl/timestamp.cxx b/butl/timestamp.cxx index 0da9f4c..41dc5ba 100644 --- a/butl/timestamp.cxx +++ b/butl/timestamp.cxx @@ -537,6 +537,7 @@ namespace butl if (time == -1) throw system_error (errno, system_category ()); - return timestamp::clock::from_time_t (time) + t.second; + return timestamp::clock::from_time_t (time) + + chrono::duration_cast (t.second); } } diff --git a/tests/timestamp/driver.cxx b/tests/timestamp/driver.cxx index 0c0f2ef..4c366fc 100644 --- a/tests/timestamp/driver.cxx +++ b/tests/timestamp/driver.cxx @@ -4,10 +4,12 @@ #include // tzset() +#include #include #include #include #include +#include #include #include @@ -20,9 +22,9 @@ using namespace butl; // same format string, ensure the output matches the input. // static bool -parse (const char* in, const char* fmt, bool local, const char* out) +parse (const char* in, const char* fmt, bool local, string out) { - if (out == nullptr) + if (out.empty ()) out = in; try @@ -43,7 +45,7 @@ parse (const char* in, const char* fmt, bool local, const char* out) } static bool -parse (const char* in, const char* fmt, const char* out = nullptr) +parse (const char* in, const char* fmt, const string& out = "") { return parse (in, fmt, true, out) && parse (in, fmt, false, out); } @@ -62,6 +64,20 @@ fail (const char* in, const char* fmt) } } +// Convert nanoseconds to string according to the butl::duration period. +// +static string +ns (unsigned long long t) +{ + duration d (chrono::duration_cast (chrono::nanoseconds (t))); + chrono::nanoseconds n (chrono::duration_cast (d)); + + ostringstream o; + o.fill ('0'); + o << setw (9) << n.count (); + return o.str (); +} + int main () { @@ -110,37 +126,51 @@ main () assert (parse ("Feb 11 19:31:10 2016 GMT", "%b %d %H:%M:%S%[.N] %Y")); assert (parse ("2016-02-11 19:31:10 GMT", "%Y-%m-%d %H:%M:%S%[.N]")); - assert (parse ( - "Feb 21 19:31:10.384902285 2016 GMT", "%b %d %H:%M:%S%[.N] %Y")); - assert (parse ( - "2016-02-21 19:31:10.384902285 GMT", "%Y-%m-%d %H:%M:%S%[.N]")); + assert (parse ("Feb 21 19:31:10.384902285 2016 GMT", + "%b %d %H:%M:%S%[.N] %Y", + "Feb 21 19:31:10." + ns (384902285) + " 2016 GMT")); - assert (parse ( - "Feb 21 19:31:10 .384902285 2016 GMT", "%b %d %H:%M:%S %[.N] %Y")); - assert (parse ( - "2016-02-21 19:31:10 .384902285 GMT", "%Y-%m-%d %H:%M:%S %[.N]")); + assert (parse ("2016-02-21 19:31:10.384902285 GMT", + "%Y-%m-%d %H:%M:%S%[.N]", + "2016-02-21 19:31:10." + ns (384902285) + " GMT")); + + assert (parse ("Feb 21 19:31:10 .384902285 2016 GMT", + "%b %d %H:%M:%S %[.N] %Y", + "Feb 21 19:31:10 ." + ns (384902285) + " 2016 GMT")); + + assert (parse ("2016-02-21 19:31:10 .384902285 GMT", + "%Y-%m-%d %H:%M:%S %[.N]", + "2016-02-21 19:31:10 ." + ns (384902285) + " GMT")); assert (parse ( "2016-02-21 19:31:10 .384902285 GMT", "%Y-%m-%d %H:%M:%S %[.N]", - "2016-02-21 19:31:10 .384902285 GMT")); + "2016-02-21 19:31:10 ." + ns (384902285) + " GMT")); assert (parse ( "2016-02-21 19:31:10 .384902285 GMT", "%Y-%m-%d %H:%M:%S %[.N]", - "2016-02-21 19:31:10 .384902285 GMT")); + "2016-02-21 19:31:10 ." + ns (384902285) + " GMT")); - assert (parse ( - "Feb 21 19:31:10 .384902285NS 2016 GMT", "%b %d %H:%M:%S %[.N]NS %Y")); - assert (parse ( - "2016-02-21 19:31:10 .384902285NS GMT", "%Y-%m-%d %H:%M:%S %[.N]NS")); + assert (parse ("Feb 21 19:31:10 .384902285NS 2016 GMT", + "%b %d %H:%M:%S %[.N]NS %Y", + "Feb 21 19:31:10 ." + ns (384902285) + "NS 2016 GMT")); - assert (parse ( - ".384902285 Feb 21 19:31:10 2016", "%[.N] %b %d %H:%M:%S %Y")); - assert (parse ( - ".384902285 2016-02-21 19:31:10", "%[.N] %Y-%m-%d %H:%M:%S")); - assert (parse ( - ".3849022852016-02-21 19:31:10", "%[.N]%Y-%m-%d %H:%M:%S")); + assert (parse ("2016-02-21 19:31:10 .384902285NS GMT", + "%Y-%m-%d %H:%M:%S %[.N]NS", + "2016-02-21 19:31:10 ." + ns (384902285) + "NS GMT")); + + assert (parse (".384902285 Feb 21 19:31:10 2016", + "%[.N] %b %d %H:%M:%S %Y", + "." + ns (384902285) + " Feb 21 19:31:10 2016")); + + assert (parse (".384902285 2016-02-21 19:31:10", + "%[.N] %Y-%m-%d %H:%M:%S", + "." + ns (384902285) + " 2016-02-21 19:31:10")); + + assert (parse (".3849022852016-02-21 19:31:10", + "%[.N]%Y-%m-%d %H:%M:%S", + "." + ns (384902285) + "2016-02-21 19:31:10")); /* setlocale (LC_ALL, "de_DE.utf-8"); -- cgit v1.1