[meld] dirdiff: Handle pre-epoch timestamps better (#265)



commit ebd3dc759be721d75de99c81d4c16df30451b500
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sat Jun 22 10:02:31 2019 +1000

    dirdiff: Handle pre-epoch timestamps better (#265)
    
    There's two parts to this handling. The first is that our cell renderer
    no longer fails when asked to render timestamps before -2**32-1... it
    now just renders them as missing values.
    
    The other part is that now that we kind of support negative timestamps,
    I felt like we couldn't keep using -1 as the sentinel. Ideally we'd use
    NaN or something, but that doesn't work in GObject currently, so this
    current change now uses -2**32-1 as the sentinel. This isn't great,
    since it's still a valid timestamp... but it's the best I could come up
    with given the limitations.
    
    If we didn't mind giving up on using our `DiffTreeStore.unsafe_set()`
    helper, then we could store -2**32-2 or something instead, but
    discarding the significant speed-ups from that helper just so that we
    can render timestamps that vanishingly-few users will ever see feels
    like a bad trade-off.

 meld/const.py            |  3 +++
 meld/dirdiff.py          | 12 ++++++------
 meld/ui/cellrenderers.py | 13 +++++++++----
 3 files changed, 18 insertions(+), 10 deletions(-)
---
diff --git a/meld/const.py b/meld/const.py
index f4195775..c5ec9afd 100644
--- a/meld/const.py
+++ b/meld/const.py
@@ -29,3 +29,6 @@ NEWLINES = {
 
 FILE_FILTER_ACTION_FORMAT = 'folder-custom-filter-{}'
 TEXT_FILTER_ACTION_FORMAT = 'text-custom-filter-{}'
+
+#: Sentinel value for mtimes on files that don't exist.
+MISSING_TIMESTAMP = -2147483648
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index f675a403..fe3bb885 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -36,7 +36,7 @@ from gi.repository import Gtk
 from meld import misc
 from meld import tree
 from meld.conf import _
-from meld.const import FILE_FILTER_ACTION_FORMAT
+from meld.const import FILE_FILTER_ACTION_FORMAT, MISSING_TIMESTAMP
 from meld.iohelpers import trash_or_confirm
 from meld.melddoc import MeldDoc
 from meld.misc import all_same, apply_text_filters, with_focused_pane
@@ -254,7 +254,7 @@ class DirDiffTreeStore(tree.DiffTreeStore):
 
     def add_error(self, parent, msg, pane):
         defaults = {
-            COL_TIME: -1.0,
+            COL_TIME: MISSING_TIMESTAMP,
             COL_SIZE: -1,
             COL_PERMS: -1
         }
@@ -1517,11 +1517,11 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
             else:
                 self.model.set_path_state(
                     it, j, tree.STATE_NONEXIST, any(isdir))
-                # SET time, size and perms to -1 since None of GInt is 0
-                # TODO: change it to math.nan some day
-                # https://gitlab.gnome.org/GNOME/glib/issues/183
+                # Set sentinel values for time, size and perms
+                # TODO: change sentinels to float('nan'), pending:
+                #   https://gitlab.gnome.org/GNOME/glib/issues/183
                 self.model.unsafe_set(it, j, {
-                    COL_TIME: -1.0,
+                    COL_TIME: MISSING_TIMESTAMP,
                     COL_SIZE: -1,
                     COL_PERMS: -1
                 })
diff --git a/meld/ui/cellrenderers.py b/meld/ui/cellrenderers.py
index e1effc9a..13326add 100644
--- a/meld/ui/cellrenderers.py
+++ b/meld/ui/cellrenderers.py
@@ -23,19 +23,24 @@ class CellRendererDate(Gtk.CellRendererText):
 
     __gtype_name__ = "CellRendererDate"
 
+    #: We use negative 32-bit Unix timestamp to threshold our valid values
+    MIN_TIMESTAMP = -2147483648
     DATETIME_FORMAT = "%a %d %b %Y %H:%M:%S"
 
     def get_timestamp(self):
-        return getattr(self, '_datetime', -1.0)
+        return getattr(self, '_datetime', self.MIN_TIMESTAMP)
 
     def set_timestamp(self, value):
         if value == self.get_timestamp():
             return
-        if value == -1.0:
+        if value <= self.MIN_TIMESTAMP:
             time_str = ''
         else:
-            mod_datetime = datetime.datetime.fromtimestamp(value)
-            time_str = mod_datetime.strftime(self.DATETIME_FORMAT)
+            try:
+                mod_datetime = datetime.datetime.fromtimestamp(value)
+                time_str = mod_datetime.strftime(self.DATETIME_FORMAT)
+            except Exception:
+                time_str = ''
         self.props.markup = time_str
         self._datetime = value
 


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