fix formatting of timezone in iso timestamps
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Fri, 26 Apr 2019 07:40:39 +0000 (09:40 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 1 Aug 2024 12:55:33 +0000 (14:55 +0200)
The `Z' indicates UTC and has no place in timestamps with an
explicit offset.

src/timefunc.cpp
test/test_timefunc.cpp

index 26fbb23..dd69369 100644 (file)
@@ -806,17 +806,17 @@ namespace iso8601 {
     static const char *const formatter [ISO8601_SIZE] =
     { /* [iso8601::d  ] = */ "%4Y-%m-%d",
       /* [iso8601::t  ] = */ "%T",
-      /* [iso8601::tz ] = */ "%TZ%z",
+      /* [iso8601::tz ] = */ "%T%z",
       /* [iso8601::dt ] = */ "%4Y-%m-%dT%T",
-      /* [iso8601::dtz] = */ "%4Y-%m-%dT%TZ%z",
+      /* [iso8601::dtz] = */ "%4Y-%m-%dT%T%z",
     };
 
     static const char *const scanner [ISO8601_SIZE] =
     { /* [iso8601::d  ] = */ "%Y-%m-%d",
       /* [iso8601::t  ] = */ "%T",
-      /* [iso8601::tz ] = */ "%TZ%z",
+      /* [iso8601::tz ] = */ "%T%z",
       /* [iso8601::dt ] = */ "%Y-%m-%dT%T",
-      /* [iso8601::dtz] = */ "%Y-%m-%dT%TZ%z",
+      /* [iso8601::dtz] = */ "%Y-%m-%dT%T%z",
     };
 
     static inline const char *
@@ -952,7 +952,7 @@ scan_iso8601 (const char *s,
         }
         /*
          * Contrary to what the man page indicates, strptime(3) is *not*
-         * the inverse operation of strftime(3)! The later correctly formats
+         * the inverse operation of strftime(3)! The latter correctly formats
          * negative year numbers with the %F modifier wheres the former trips
          * over the sign character.
          */
index ebc22ae..1ee6e47 100644 (file)
@@ -511,14 +511,14 @@ BOOST_AUTO_TEST_CASE(FormatISO8601_TZ_local)
 {
     this->set_tz ("CET");
     const time_t moment = 1515492684;
-    BOOST_CHECK_EQUAL("11:11:24Z+0100",
+    BOOST_CHECK_EQUAL("11:11:24+0100",
                       format_iso8601 (moment, false, false, true, true));
 }
 
 BOOST_AUTO_TEST_CASE(FormatISO8601_TZ)
 {
     const time_t moment = 1515492684;
-    BOOST_CHECK_EQUAL("10:11:24Z+0000",
+    BOOST_CHECK_EQUAL("10:11:24+0000",
                       format_iso8601 (moment, true, false, true, true));
 }
 
@@ -526,14 +526,14 @@ BOOST_AUTO_TEST_CASE(FormatISO8601_DTZ_local)
 {
     this->set_tz ("CET");
     const time_t moment = 1515492684;
-    BOOST_CHECK_EQUAL("2018-01-09T11:11:24Z+0100",
+    BOOST_CHECK_EQUAL("2018-01-09T11:11:24+0100",
                       format_iso8601 (moment, false, true, true, true));
 }
 
 BOOST_AUTO_TEST_CASE(FormatISO8601_DTZ)
 {
     const time_t moment = 1515492684;
-    BOOST_CHECK_EQUAL("2018-01-09T10:11:24Z+0000",
+    BOOST_CHECK_EQUAL("2018-01-09T10:11:24+0000",
                       format_iso8601 (moment, true, true, true, true));
 }
 
@@ -566,7 +566,7 @@ BOOST_AUTO_TEST_CASE(FormatISO8601_DTZ_struct_tm)
     helau.tm_gmtoff = 0;
     helau.tm_zone   = NULL;
 
-    BOOST_CHECK_EQUAL("2018-11-11T11:11:11Z+0000",
+    BOOST_CHECK_EQUAL("2018-11-11T11:11:11+0000",
                       format_iso8601 (helau, true, true, true));
 }
 
@@ -574,7 +574,7 @@ BOOST_AUTO_TEST_CASE(FormatISO8601_DTZ_struct_timespec)
 {
     struct timespec ts = { 1541934671, 11 };
 
-    BOOST_CHECK_EQUAL("2018-11-11T11:11:11Z+0000",
+    BOOST_CHECK_EQUAL("2018-11-11T11:11:11+0000",
                       format_iso8601 (ts, true, true, true, true));
 }
 
@@ -1115,7 +1115,7 @@ BOOST_AUTO_TEST_SUITE(Clock)
         I2n::clock::Time t (1541934671, 0);
         boost::optional<std::string> s = t.format_iso8601 (true, true, true, true);
 
-        BOOST_CHECK_EQUAL("2018-11-11T11:11:11Z+0000", *s);
+        BOOST_CHECK_EQUAL("2018-11-11T11:11:11+0000", *s);
     }
 
     BOOST_AUTO_TEST_CASE(Format_make_nice_time)
@@ -1149,8 +1149,8 @@ BOOST_AUTO_TEST_SUITE(Clock)
 
     BOOST_AUTO_TEST_CASE(FromString_iso8601_full)
     {
-        const std::string in1 ("0001-01-01T00:00:00Z+0000");
-        const std::string in2 ("2018-11-11T11:11:11Z+0000");
+        const std::string in1 ("0001-01-01T00:00:00+0000");
+        const std::string in2 ("2018-11-11T11:11:11+0000");
 
         this->set_utc ();
 
@@ -1170,8 +1170,8 @@ BOOST_AUTO_TEST_SUITE(Clock)
 
     BOOST_AUTO_TEST_CASE(FromString_iso8601_full_negyear)
     {
-        const std::string in1 ("-0001-01-01T00:00:00Z+0000");
-        const std::string in2 ("-2018-11-11T11:11:11Z+0000");
+        const std::string in1 ("-0001-01-01T00:00:00+0000");
+        const std::string in2 ("-2018-11-11T11:11:11+0000");
 
         this->set_utc ();
 
@@ -1189,6 +1189,23 @@ BOOST_AUTO_TEST_SUITE(Clock)
 # endif
     }
 
+    BOOST_AUTO_TEST_CASE(FromString_iso8601_Z)
+    {
+        const std::string in1 ("2019-04-25T13:41:47+0000");
+        const std::string in2 ("2019-04-25T13:41:47Z");
+
+        this->set_utc ();
+        boost::optional<I2n::clock::Time> t1 = I2n::clock::time_of_iso8601 (in1, true, true, true);
+        boost::optional<I2n::clock::Time> t2 = I2n::clock::time_of_iso8601 (in2, true, true, true);
+
+        BOOST_CHECK(t1);
+        BOOST_CHECK(t2);
+
+        BOOST_CHECK_EQUAL(*t1, *t2);
+        BOOST_CHECK_EQUAL(*t1->format_iso8601 (), *t2->format_iso8601 ());
+        BOOST_CHECK_EQUAL(*t2->format_iso8601 (), in1);
+    }
+
     BOOST_AUTO_TEST_CASE(FromString_iso8601_partial)
     {
         const std::string in1 ("2018-11-11T11:11:11");