comparison src/tools/utils.py @ 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 934e402c90bf
children d17772b0fe22
comparison
equal deleted inserted replaced
1501:cb1b0fe10415 1502:566908d483f6
21 21
22 import unicodedata 22 import unicodedata
23 import os.path 23 import os.path
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25 log = getLogger(__name__) 25 log = getLogger(__name__)
26 import datetime
27 import time
26 28
27 29
28 def clean_ustr(ustr): 30 def clean_ustr(ustr):
29 """Clean unicode string 31 """Clean unicode string
30 32
31 remove special characters from unicode string""" 33 remove special characters from unicode string
34 """
32 def valid_chars(unicode_source): 35 def valid_chars(unicode_source):
33 for char in unicode_source: 36 for char in unicode_source:
34 if unicodedata.category(char) == 'Cc' and char!='\n': 37 if unicodedata.category(char) == 'Cc' and char!='\n':
35 continue 38 continue
36 yield char 39 yield char
37 return ''.join(valid_chars(ustr)) 40 return ''.join(valid_chars(ustr))
41
42 def xmpp_date(timestamp=None, with_time=True):
43 """Return date according to XEP-0082 specification
44
45 to avoid reveling the timezone, we always return UTC dates
46 @param timestamp(None, float): posix timestamp. If None current time will be used
47 @param with_time(bool): if True include the time
48 @return(unicode): XEP-0082 formatted date and time
49 """
50 template_date = u"%Y-%m-%d"
51 template_time = u"%H:%M:%SZ"
52 template = u"{}T{}".format(template_date, template_time) if with_time else template_date
53 return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template)
54
38 55
39 def getRepositoryData(module, as_string=True): 56 def getRepositoryData(module, as_string=True):
40 """Retrieve info on current mecurial repository 57 """Retrieve info on current mecurial repository
41 58
42 @param module: module to look for (e.g. sat, libervia) 59 @param module: module to look for (e.g. sat, libervia)