[gnote] Do not use deprecated function when converting between date and string



commit b30cd91826d2ee3b953b2485683ba8011be0434d
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Feb 9 17:16:44 2020 +0200

    Do not use deprecated function when converting between date and string

 src/sharp/datetime.cpp           | 44 ++++++++++++++++++++++++++--------------
 src/test/unit/datetimeutests.cpp |  9 +++++++-
 2 files changed, 37 insertions(+), 16 deletions(-)
---
diff --git a/src/sharp/datetime.cpp b/src/sharp/datetime.cpp
index 17b6616c..43d22dfc 100644
--- a/src/sharp/datetime.cpp
+++ b/src/sharp/datetime.cpp
@@ -55,27 +55,41 @@ Glib::ustring date_time_to_iso8601(const Glib::DateTime & dt)
   if(!dt) {
     return retval;
   }
-  Glib::TimeVal time_val(dt.to_unix(), dt.get_microsecond());
-  char *iso8601 = g_time_val_to_iso8601(&time_val);
-  if(iso8601) {
-    retval = iso8601;
-    if(time_val.tv_usec == 0) {
-      // see http://bugzilla.gnome.org/show_bug.cgi?id=581844
-      // when usec is 0, glib/libc does NOT add the usec values
-      // to the output
-      retval.insert(19, ".000000");
-    }
-    g_free(iso8601);
-  }
+
+  Glib::DateTime date = dt.to_utc();
+  char buffer[36] = {0};
+  std::sprintf(buffer, "%d-%02d-%02dT%02d:%02d:%02.6lfZ", date.get_year(), date.get_month(), 
date.get_day_of_month(), date.get_hour(), date.get_minute(), date.get_seconds());
+  retval = buffer;
   return retval;
 }
 
 Glib::DateTime date_time_from_iso8601(const Glib::ustring & dt)
 {
-  Glib::TimeVal time_val;
-  if(g_time_val_from_iso8601(dt.c_str(), &time_val)) {
-    return Glib::DateTime::create_now_local(time_val);
+  int y, M, d, h, m, tzh = 0, tzm = 0;
+  double s;
+  int parsed = std::sscanf(dt.c_str(), "%d-%d-%dT%d:%d:%lf%d:%dZ", &y, &M, &d, &h, &m, &s, &tzh, &tzm);
+  if(6 <= parsed) {
+    auto ret = Glib::DateTime::create_utc(y, M, d, h, m, s).to_local();
+    if(tzh != 0) {
+      if(tzh < 0) {
+        tzh = -tzh;
+      }
+    }
+    else {
+      if(dt.size() > 27 && dt[27] == '+') {
+        tzm = -tzm;
+      }
+    }
+    if(tzh != 0) {
+      ret = ret.add_hours(tzh);
+    }
+    if(tzm != 0) {
+      ret = ret.add_minutes(tzm);
+    }
+
+    return ret;
   }
+
   return Glib::DateTime();
 }
 
diff --git a/src/test/unit/datetimeutests.cpp b/src/test/unit/datetimeutests.cpp
index 148e2279..eb7530ae 100644
--- a/src/test/unit/datetimeutests.cpp
+++ b/src/test/unit/datetimeutests.cpp
@@ -62,11 +62,18 @@ SUITE(DateTime)
     CHECK_EQUAL(34, d.get_second());
     CHECK_EQUAL(67890, d.get_microsecond());
 
-    Glib::DateTime d2 = sharp::date_time_from_iso8601("2009-03-24T03:34:35.2914680-04:00");
+    Glib::DateTime d2 = sharp::date_time_from_iso8601("2009-03-24T03:34:35.2914680-04:00Z");
     CHECK(bool(d2));
     CHECK_EQUAL(24, d2.get_day_of_month());
     CHECK_EQUAL(7, d2.get_hour());
     CHECK_EQUAL(291468, d2.get_microsecond());
+
+    Glib::DateTime d3 = sharp::date_time_from_iso8601("2009-03-24T03:34:35.2914680+00:30Z");
+    CHECK(bool(d3));
+    CHECK_EQUAL(24, d3.get_day_of_month());
+    CHECK_EQUAL(3, d3.get_hour());
+    CHECK_EQUAL(4, d3.get_minute());
+    CHECK_EQUAL(291468, d3.get_microsecond());
   }
 
   TEST(pretty_print_date)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]