[gxml] Added GomDateTime for timestamp attributes



commit 458202eec13ecda78f2aee2028b5c1fd332a5ff1
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Feb 10 14:45:47 2017 -0600

    Added GomDateTime for timestamp attributes

 gxml/GomProperty.vala          |   42 ++++++++++++++++++++++++++++++++++++++-
 test/GomSerializationTest.vala |   40 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 2 deletions(-)
---
diff --git a/gxml/GomProperty.vala b/gxml/GomProperty.vala
index 735a636..329c3c3 100644
--- a/gxml/GomProperty.vala
+++ b/gxml/GomProperty.vala
@@ -368,12 +368,11 @@ public class GXml.GomEnum : GomBaseProperty {
   public void set_enum (int value) { _value = value; }
 }
 
-
 /**
  * Convenient class to handle {@link GomElement}'s attributes
  * using a {@link GLib.Date} as sources of values.
  *
- * Property is represented as a string using a %Y-%M-%D format
+ * Property is represented as a string using a %Y-%m-%d format
  */
 public class GXml.GomDate : GomBaseProperty {
   protected Date _value = Date ();
@@ -400,3 +399,42 @@ public class GXml.GomDate : GomBaseProperty {
    */
   public void set_date (Date date) { _value = date; }
 }
+
+/**
+ * Convenient class to handle {@link GomElement}'s attributes
+ * using a {@link GLib.DateTime} as sources of values.
+ *
+ * Timestamp is considered in local time.
+ *
+ * Property is represented as a string using a {@link GomDateTime.format}
+ * and {@link GLib.DateTime.format} method. If {@link GomDateTime.format}
+ * is not set '%FT%T' format is used by default.
+ */
+public class GXml.GomDateTime : GomBaseProperty {
+  protected DateTime _value = null;
+  public string format { get; set; }
+  public override string? value {
+    owned get {
+      if (_value == null) return null;
+      string s = format;
+      if (s == null)
+        s = "%FT%T";
+      return _value.format (s);
+    }
+    set {
+      var tv = new TimeVal ();
+      if (tv.from_iso8601 (value)) {
+        _value = new DateTime.from_timeval_local (tv);
+      } else
+        warning (_("Invalid timestamp for property: "+value));
+    }
+  }
+  /**
+   * Retrives current value.
+   */
+  public DateTime get_datetime () { return _value; }
+  /**
+   * Sets current value.
+   */
+  public void set_datetime (DateTime dt) { _value = dt; }
+}
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 9e88106..3671b8f 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -191,6 +191,8 @@ class GomSerializationTest : GXmlTest  {
     public Month month { get; set; }
     [Description (nick="::PayDate")]
     public GomDate pay_date { get; set; }
+    [Description (nick="::Timestamp")]
+    public GomDateTime timestamp { get; set; }
     construct { try { initialize ("Taxes"); } catch { assert_not_reached (); } }
     public string to_string () {
       var parser = new XParser (this);
@@ -479,6 +481,44 @@ class GomSerializationTest : GXmlTest  {
       assert_not_reached ();
     }
     });
+    Test.add_func ("/gxml/gom-serialization/write/property-datetime", () => {
+    try {
+      var t = new Taxes ();
+      string s = t.to_string ();
+      assert (s != null);
+#if DEBUG
+      GLib.message ("DOC:"+s);
+#endif
+      assert ("<Taxes " in s);
+      assert ("monthRate=\"0\"" in s);
+      assert ("Month=\"january\"" in s);
+      assert ("TaxFree=\"false\"" in s);
+      t.timestamp = new GomDateTime ();
+      var d = new DateTime.local (2017,2,10,14,14,20.345);
+#if DEBUG
+      s = t.to_string ();
+      assert (s != null);
+      GLib.message ("DOC:"+s);
+#endif
+      assert (t.timestamp != null);
+      t.timestamp.set_datetime (d);
+      assert (t.timestamp != null);
+      assert (t.timestamp.value != null);
+      assert (t.timestamp.value == "2017-02-10T14:14:20");
+      t.timestamp.value = "2023-3-10T15:23:10.356";
+      assert (t.timestamp.value != null);
+      assert (t.timestamp.value == "2023-03-10T15:23:10");
+      string s2 = t.to_string ();
+      assert (s2 != null);
+#if DEBUG
+      GLib.message ("DOC:"+s2);
+#endif
+      assert ("Timestamp=\"2023-03-10T15:23:10\"" in s2);
+    } catch (GLib.Error e) {
+      GLib.message ("Error: "+e.message);
+      assert_not_reached ();
+    }
+    });
     Test.add_func ("/gxml/gom-serialization/write/property-arraylist", () => {
     try {
       var bs = new BookStand ();


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