comparison src/tools/utils.py @ 2238:228d208fb5db

core (tools/utils): added method to parse XMPP dates
author Goffi <goffi@goffi.org>
date Fri, 19 May 2017 12:33:11 +0200
parents 968b0d13bcc7
children 8b37a62336c3
comparison
equal deleted inserted replaced
2237:fd4111075e00 2238:228d208fb5db
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 26 import datetime
27 import dateutil
28 import calendar
27 import time 29 import time
28 import sys 30 import sys
29 import random 31 import random
30 32
31 33
52 """ 54 """
53 template_date = u"%Y-%m-%d" 55 template_date = u"%Y-%m-%d"
54 template_time = u"%H:%M:%SZ" 56 template_time = u"%H:%M:%SZ"
55 template = u"{}T{}".format(template_date, template_time) if with_time else template_date 57 template = u"{}T{}".format(template_date, template_time) if with_time else template_date
56 return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template) 58 return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template)
59
60 def date_parse(value):
61 """Parse a date and return corresponding unix timestamp
62
63 @param value(unicode): date to parse, in any format supported by dateutil.parser
64 """
65 return calendar.timegm(dateutil.parser.parse(unicode(value)).utctimetuple())
57 66
58 def generatePassword(vocabulary=None, size=20): 67 def generatePassword(vocabulary=None, size=20):
59 """Generate a password with random characters. 68 """Generate a password with random characters.
60 69
61 @param vocabulary(iterable): characters to use to create password 70 @param vocabulary(iterable): characters to use to create password