[Xfce4-commits] <postler:master> Rework date formatting and solve timezone issues

Christian Dywan noreply at xfce.org
Fri Jan 28 21:38:01 CET 2011


Updating branch refs/heads/master
         to bd182686d649009ba6983eb2fb9dc6ec4086db4a (commit)
       from 069fba1ed7238c25f0fb7adf47d20bfa85e1ca34 (commit)

commit bd182686d649009ba6983eb2fb9dc6ec4086db4a
Author: Christian Dywan <christian at twotoasts.de>
Date:   Fri Jan 28 21:01:46 2011 +0100

    Rework date formatting and solve timezone issues
    
    Fixes: https://bugs.launchpad.net/postler/+bug/671631

 postler/postler-content.vala  |   32 ++++++++++++++++++++++++++++----
 postler/postler-messages.vala |   37 +++++--------------------------------
 2 files changed, 33 insertions(+), 36 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index 158125f..d375f2f 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -403,6 +403,33 @@ public class Postler.Content : WebKit.WebView {
         return "";
     }
 
+    internal static string format_date (DateTime the_time) {
+        var now = new DateTime.now_local ();
+        if (the_time.get_day_of_month () == now.get_day_of_month ())
+            return _("Today") + the_time.format (" %X");
+        if (the_time.get_day_of_year () == now.get_day_of_year () - 1)
+            return _("Yesterday") + the_time.format (" %X");
+        if (the_time.get_year () == now.get_year ()) {
+            /* i18n: strftime format for full month name and day number */
+            return the_time.format (_("%B %e") + (" %X"));
+        }
+        /* i18n: strftime format for full month name, day number and year */
+        return the_time.format (_("%B %e, %Y") + (" %X"));
+    }
+
+    internal static string format_timestamp (time_t timestamp, int offset=0) {
+        var the_time = new DateTime.from_unix_local (timestamp).add_minutes (offset);
+        return format_date (the_time);
+    }
+
+    internal static DateTime date_from_string (string date) {
+        var parsed = new Soup.Date.from_string (date);
+        TimeVal time = new TimeVal ();
+        if (parsed != null)
+            parsed.to_timeval (time);
+        return new DateTime.from_timeval_local (time).add_minutes (parsed.offset);
+    }
+
     public bool prepare_reply (string? location, AccountInfo account_info,
         string recipient, bool quote, int part) {
 
@@ -624,8 +651,6 @@ public class Postler.Content : WebKit.WebView {
             content_encoding = "";
             string from = _("Unknown");
             date = _("(No date)");
-            var now = GLib.Date ();
-            now.set_time_val (GLib.TimeVal ());
             recipient = "";
             carbon_copy = "";
             blind_copy = "";
@@ -658,8 +683,7 @@ public class Postler.Content : WebKit.WebView {
                     from = parse_encoded (parts[1], out from_charset);
                 }
                 else if (field == "date") {
-                    time_t timestamp;
-                    date = Postler.Messages.format_date (parts[1], now, out timestamp);
+                    date = format_date (date_from_string (parts[1]));
                 }
                 else if (field == "to")
                     recipient = parts[1].strip ();
diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index f65f976..8a6e796 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -147,9 +147,7 @@ public class Postler.Messages : Gtk.TreeView {
         model.get (iter, Columns.FROM, out from);
 
         subject = escape_text (parse_encoded (subject, out charset));
-        var now = GLib.Date ();
-        now.set_time_val (GLib.TimeVal ());
-        string date = Postler.Messages.format_timestamp (timestamp, now);
+        string date = Postler.Content.format_timestamp (timestamp);
         from = escape_text (parse_address (parse_encoded (from, out charset))[0]);
         from = dexter.get_name (from) ?? from;
 
@@ -185,9 +183,7 @@ public class Postler.Messages : Gtk.TreeView {
         else {
             time_t timestamp;
             model.get (iter, Columns.TIMESTAMP, out timestamp);
-            var now = GLib.Date ();
-            now.set_time_val (GLib.TimeVal ());
-            renderer.text = Postler.Messages.format_timestamp (timestamp, now);
+            renderer.text = Postler.Content.format_timestamp (timestamp);
         }
     }
 
@@ -444,29 +440,6 @@ public class Postler.Messages : Gtk.TreeView {
         store.clear ();
     }
 
-    internal static string format_timestamp (time_t timestamp, GLib.Date now) {
-        var the_time = GLib.Date ();
-        the_time.set_time_t (timestamp);
-        int days = now.days_between (the_time);
-        if (days == 0)
-            return _("Today") + GLib.Time.local (timestamp).format (" %X");
-        if (days == 1)
-            return _("Yesterday") + GLib.Time.local (timestamp).format (" %X");
-        if (the_time.get_year () == now.get_year ()) {
-            /* i18n: strftime format for full month name and day number */
-            return GLib.Time.local (timestamp).format (_("%B %e") + (" %X"));
-        }
-        /* i18n: strftime format for full month name, day number and year */
-        return GLib.Time.local (timestamp).format (_("%B %e, %Y") + (" %X"));
-    }
-
-    internal static string format_date (string date, GLib.Date now,
-        out time_t timestamp) {
-        var parsed = new Soup.Date.from_string (date);
-        timestamp = parsed != null ? (time_t)parsed.to_time_t () : 0;
-        return format_timestamp (timestamp, now);
-    }
-
     static string flags_from_filename (string filename, out string bare_filename) {
         string[]? parts = filename.split (":");
         if (parts == null || parts[0] == null || parts[1] == null) {
@@ -531,7 +504,7 @@ public class Postler.Messages : Gtk.TreeView {
         public string get_from () {
             return from != null ? from : _("Unknown");
         }
-        time_t timestamp;
+        int64 timestamp;
         int64 size;
         string attachment;
     }
@@ -600,8 +573,8 @@ public class Postler.Messages : Gtk.TreeView {
                         break;
                     }
                 else if (field == "date") {
-                    var parsed = new Soup.Date.from_string (parts[1]);
-                    message.timestamp = parsed != null ? (time_t)parsed.to_time_t () : 0;
+                    var the_time = Postler.Content.date_from_string (parts[1]);
+                    message.timestamp = the_time.to_local ().to_unix ();
                     if (message.subject != null && message.from != null
                      && content_type != null && filters[0] == null)
                         break;



More information about the Xfce4-commits mailing list