[gnote] Prepare for replacing sharp::DateTime with Glib::DateTime



commit 88b08de560064bc893673f3c3039331c3cb3f389
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 19 22:45:57 2020 +0200

    Prepare for replacing sharp::DateTime with Glib::DateTime

 src/sharp/datetime.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/sharp/datetime.hpp |  14 ++++++
 2 files changed, 144 insertions(+)
---
diff --git a/src/sharp/datetime.cpp b/src/sharp/datetime.cpp
index a484c2f4..defdf0c6 100644
--- a/src/sharp/datetime.cpp
+++ b/src/sharp/datetime.cpp
@@ -207,4 +207,134 @@ namespace sharp {
   }
 
 
+Glib::ustring date_time_to_string(const Glib::DateTime & dt, const char *format)
+{
+  struct tm t;
+  Glib::TimeVal date(dt.to_unix(), dt.get_microsecond());
+  localtime_r((const time_t *)&date.tv_sec, &t);
+  char output[256];
+  strftime(output, sizeof(output), format, &t);
+  return Glib::locale_to_utf8(output);
 }
+
+Glib::ustring date_time_to_string(const Glib::DateTime & dt, const Glib::ustring & format)
+{
+  return date_time_to_string(dt, format.c_str());
+}
+
+Glib::ustring date_time_to_iso8601(const Glib::DateTime & dt)
+{
+  Glib::ustring retval;
+  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);
+  }
+  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);
+  }
+  return Glib::DateTime();
+}
+
+}
+
+bool operator==(const Glib::DateTime & x, const Glib::DateTime & y)
+{
+  bool x_valid = bool(x);
+  bool y_valid = bool(y);
+  if(x_valid && y_valid) {
+    return x.compare(y) == 0;
+  }
+  return x_valid == y_valid;
+}
+
+bool operator!=(const Glib::DateTime & x, const Glib::DateTime & y)
+{
+  bool x_valid = bool(x);
+  bool y_valid = bool(y);
+  if(x_valid && y_valid) {
+    return x.compare(y) != 0;
+  }
+  return x_valid != y_valid;
+}
+
+bool operator<(const Glib::DateTime & x, const Glib::DateTime & y)
+{
+  bool x_valid = bool(x);
+  bool y_valid = bool(y);
+  if(x_valid && y_valid) {
+    return x.compare(y) < 0;
+  }
+  if(x_valid == y_valid) {
+    return false;
+  }
+  else if(x_valid) {
+    return false;
+  }
+  return true;
+}
+
+bool operator<=(const Glib::DateTime & x, const Glib::DateTime & y)
+{
+  bool x_valid = bool(x);
+  bool y_valid = bool(y);
+  if(x_valid && y_valid) {
+    return x.compare(y) <= 0;
+  }
+  if(x_valid == y_valid) {
+    return true;
+  }
+  else if(x_valid) {
+    return false;
+  }
+  return true;
+}
+
+bool operator>(const Glib::DateTime & x, const Glib::DateTime & y)
+{
+  bool x_valid = bool(x);
+  bool y_valid = bool(y);
+  if(x_valid && y_valid) {
+    return x.compare(y) > 0;
+  }
+  if(x_valid == y_valid) {
+    return false;
+  }
+  else if(x_valid) {
+    return true;
+  }
+  return false;
+}
+
+bool operator>=(const Glib::DateTime & x, const Glib::DateTime & y)
+{
+  bool x_valid = bool(x);
+  bool y_valid = bool(y);
+  if(x_valid && y_valid) {
+    return x.compare(y) >= 0;
+  }
+  if(x_valid == y_valid) {
+    return true;
+  }
+  else if(x_valid) {
+    return true;
+  }
+  return false;
+}
+
diff --git a/src/sharp/datetime.hpp b/src/sharp/datetime.hpp
index 013ffa22..2cd28652 100644
--- a/src/sharp/datetime.hpp
+++ b/src/sharp/datetime.hpp
@@ -110,6 +110,20 @@ private:
 };
 
 
+Glib::ustring date_time_to_string(const Glib::DateTime & dt, const char *format);
+Glib::ustring date_time_to_string(const Glib::DateTime & dt, const Glib::ustring & format);
+Glib::ustring date_time_to_iso8601(const Glib::DateTime & dt);
+Glib::DateTime date_time_from_iso8601(const Glib::ustring & dt);
+
+
 }
 
+
+bool operator==(const Glib::DateTime & x, const Glib::DateTime & y);
+bool operator!=(const Glib::DateTime & x, const Glib::DateTime & y);
+bool operator<(const Glib::DateTime & x, const Glib::DateTime & y);
+bool operator<=(const Glib::DateTime & x, const Glib::DateTime & y);
+bool operator>(const Glib::DateTime & x, const Glib::DateTime & y);
+bool operator>=(const Glib::DateTime & x, const Glib::DateTime & y);
+
 #endif


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