From e8c3e3500d3bdb250c49f2bd75bea6775048849c Mon Sep 17 00:00:00 2001 From: Goffi Date: Thu, 27 Aug 2015 17:59:11 +0200 Subject: [PATCH] core (utils): added a method to generate XEP-0082 style dates --- src/tools/utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/tools/utils.py b/src/tools/utils.py index 35b938a7..c715381b 100644 --- a/src/tools/utils.py +++ b/src/tools/utils.py @@ -23,12 +23,15 @@ import unicodedata 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 @@ def clean_ustr(ustr): 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