cqlsh: fix timestamp formatting for tz info
Patch by Aleksey Yeschenko, reviewed by brandonwilliams for
CASSANDRA-4746


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/3d486710
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/3d486710
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/3d486710

Branch: refs/heads/trunk
Commit: 3d4867109fb09d9a995e0b3f5a9a30e11231db58
Parents: 64f3ab2
Author: Brandon Williams <[email protected]>
Authored: Fri Oct 26 08:47:59 2012 -0500
Committer: Brandon Williams <[email protected]>
Committed: Fri Oct 26 08:47:59 2012 -0500

----------------------------------------------------------------------
 pylib/cqlshlib/formatting.py |   21 +++++++++++++++++++--
 1 files changed, 19 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/3d486710/pylib/cqlshlib/formatting.py
----------------------------------------------------------------------
diff --git a/pylib/cqlshlib/formatting.py b/pylib/cqlshlib/formatting.py
index 11b4dcb..d15c083 100644
--- a/pylib/cqlshlib/formatting.py
+++ b/pylib/cqlshlib/formatting.py
@@ -132,15 +132,32 @@ formatter_for('counter')(format_integer_type)
 
 @formatter_for('timestamp')
 def format_value_timestamp(val, colormap, time_format, **_):
-    bval = time.strftime(time_format, time.localtime(val))
+    bval = strftime(time_format, val)
     return colorme(bval, colormap, 'timestamp')
 
 @formatter_for('timeuuid')
 def format_value_timeuuid(val, colormap, time_format, **_):
     utime = cqltypes.unix_time_from_uuid1(val)
-    bval = time.strftime(time_format, time.localtime(utime))
+    bval = strftime(time_format, utime)
     return colorme(bval, colormap, 'timestamp')
 
+def strftime(time_format, seconds):
+    local = time.localtime(seconds)
+    formatted = time.strftime(time_format, local)
+    if local.tm_isdst != 0:
+        offset = -time.altzone
+    else:
+        offset = -time.timezone
+    if formatted[-4] != '0000' or time_format[-2] != '%z' or offset == 0:
+        return formatted
+    # deal with %z on platforms where it isn't supported. see CASSANDRA-4746.
+    if offset < 0:
+        sign = '-'
+    else:
+        sign = '+'
+    hours, minutes = divmod(abs(offset) / 60, 60)
+    return formatted[:-5] + sign + '{0:0=2}{1:0=2}'.format(hours, minutes)
+
 @formatter_for('text')
 def format_value_text(val, encoding, colormap, **_):
     escapedval = val.replace(u'\\', u'\\\\')

Reply via email to