changeset 1502:566908d483f6

core (utils): added a method to generate XEP-0082 style dates
author Goffi <goffi@goffi.org>
date Thu, 27 Aug 2015 17:59:11 +0200
parents cb1b0fe10415
children f681788097ba
files src/tools/utils.py
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/utils.py	Thu Aug 27 17:59:01 2015 +0200
+++ b/src/tools/utils.py	Thu Aug 27 17:59:11 2015 +0200
@@ -23,12 +23,15 @@
 import os.path
 from sat.core.log import getLogger
 log = getLogger(__name__)
+import datetime
+import time
 
 
 def clean_ustr(ustr):
     """Clean unicode string
 
-    remove special characters from unicode string"""
+    remove special characters from unicode string
+    """
     def valid_chars(unicode_source):
         for char in unicode_source:
             if unicodedata.category(char) == 'Cc' and char!='\n':
@@ -36,6 +39,20 @@
             yield char
     return ''.join(valid_chars(ustr))
 
+def xmpp_date(timestamp=None, with_time=True):
+    """Return date according to XEP-0082 specification
+
+    to avoid reveling the timezone, we always return UTC dates
+    @param timestamp(None, float): posix timestamp. If None current time will be used
+    @param with_time(bool): if True include the time
+    @return(unicode): XEP-0082 formatted date and time
+    """
+    template_date = u"%Y-%m-%d"
+    template_time = u"%H:%M:%SZ"
+    template = u"{}T{}".format(template_date, template_time) if with_time else template_date
+    return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template)
+
+
 def getRepositoryData(module, as_string=True):
     """Retrieve info on current mecurial repository