From fc76b4d94f3b8a37fd50d6a24aa3e9ecafbd8d01 Mon Sep 17 00:00:00 2001
From: juga0 <juga@riseup.net>
Date: Wed, 23 May 2018 10:12:37 +0000
Subject: [PATCH] Add functions to convert timestamp formats

---
 sbws/util/timestamp.py | 55 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 sbws/util/timestamp.py

diff --git a/sbws/util/timestamp.py b/sbws/util/timestamp.py
new file mode 100644
index 00000000..0de7551a
--- /dev/null
+++ b/sbws/util/timestamp.py
@@ -0,0 +1,55 @@
+"""Util functions to convert between timestamp formats"""
+from datetime import datetime
+
+
+def dt_obj_to_isodt_str(dt):
+    """
+    Convert datetime object to ISO 8601 string.
+
+    :param datetime dt: datetime object in UTC timezone
+    :returns: ISO 8601 string
+    """
+    assert isinstance(dt, datetime)
+    # Using naive datetime object without timezone, assumed utc
+    return dt.replace(microsecond=0).isoformat()
+
+
+def unixts_to_dt_obj(unixts):
+    """
+    Convert unix timestamp to naive datetime object in UTC time zone.
+
+    :param float/int/str unixts: unix timestamp
+    :returns: datetime object in UTC timezone
+    """
+    if isinstance(unixts, str):
+        try:
+            unixts = int(unixts)
+        except ValueError as e:
+            unixts = float(unixts)
+    if isinstance(unixts, float):
+        unixts = int(unixts)
+    assert isinstance(unixts, int)
+    return datetime.utcfromtimestamp(unixts)
+
+
+def unixts_to_isodt_str(unixts):
+    """
+    Convert unix timestamp to ISO 8601 string in UTC time zone.
+
+    :param float/int/str unixts: unix timestamp
+    :returns: ISO 8601 string in UTC time zone
+    """
+    return dt_obj_to_isodt_str(unixts_to_dt_obj(unixts))
+
+
+def now_isodt_str():
+    """Return datetime now as ISO 8601 string in UTC time zone."""
+    return dt_obj_to_isodt_str(datetime.utcnow())
+
+
+def unixts_to_str(unixts):
+    """Convert unix timestamp integer or float to string"""
+    # even if it is only converting to str, ensure that input is nothing else
+    # than int or float
+    assert isinstance(unixts, int) or isinstance(unixts, float)
+    return str(unixts)
-- 
GitLab