core (utils): added a method to generate XEP-0082 style dates

This commit is contained in:
Goffi 2015-08-27 17:59:11 +02:00
commit e8c3e3500d

View file

@ -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