Added 'localgmtime' function to convert locally entered datetimes to GMT
authorThierry Florac <thierry.florac@onf.fr>
Wed, 07 Oct 2015 17:46:56 +0200
changeset 47 2b580a3b2142
parent 46 c9a917e7875c
child 48 8d0e2e33ae90
Added 'localgmtime' function to convert locally entered datetimes to GMT
src/pyams_utils/timezone/__init__.py
--- a/src/pyams_utils/timezone/__init__.py	Wed Oct 07 17:46:02 2015 +0200
+++ b/src/pyams_utils/timezone/__init__.py	Wed Oct 07 17:46:56 2015 +0200
@@ -49,6 +49,10 @@
 
 
 def tztime(value):
+    """Convert datetime to local timezone
+
+    Value is assumed to be in GMT if no timezone is given
+    """
     if not value:
         return None
     if not isinstance(value, datetime):
@@ -59,6 +63,10 @@
 
 
 def gmtime(value):
+    """Convert datetime to GMT
+
+    Value is assumed to be in GMT if no timezone is given
+    """
     if not value:
         return None
     if not isinstance(value, datetime):
@@ -66,3 +74,17 @@
     if not value.tzinfo:
         value = GMT.localize(value)
     return value.astimezone(GMT)
+
+
+def localgmtime(value):
+    """Convert datetime to GMT
+
+    Value is assumed to be in server timezone if none is given
+    """
+    if not value:
+        return None
+    if not isinstance(value, datetime):
+        return value
+    if not value.tzinfo:
+        value = tzinfo().localize(value)
+    return value.astimezone(GMT)