core (utils): added a method to generate XEP-0082 style dates
This commit is contained in:
parent
6f1d742645
commit
e8c3e3500d
1 changed files with 18 additions and 1 deletions
|
|
@ -23,12 +23,15 @@ import unicodedata
|
||||||
import os.path
|
import os.path
|
||||||
from sat.core.log import getLogger
|
from sat.core.log import getLogger
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
def clean_ustr(ustr):
|
def clean_ustr(ustr):
|
||||||
"""Clean unicode string
|
"""Clean unicode string
|
||||||
|
|
||||||
remove special characters from unicode string"""
|
remove special characters from unicode string
|
||||||
|
"""
|
||||||
def valid_chars(unicode_source):
|
def valid_chars(unicode_source):
|
||||||
for char in unicode_source:
|
for char in unicode_source:
|
||||||
if unicodedata.category(char) == 'Cc' and char!='\n':
|
if unicodedata.category(char) == 'Cc' and char!='\n':
|
||||||
|
|
@ -36,6 +39,20 @@ def clean_ustr(ustr):
|
||||||
yield char
|
yield char
|
||||||
return ''.join(valid_chars(ustr))
|
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):
|
def getRepositoryData(module, as_string=True):
|
||||||
"""Retrieve info on current mecurial repository
|
"""Retrieve info on current mecurial repository
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue