Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_timeago_except(self):
date = ''
self.assertRaises(ParameterUnvalid, timeago.format, date)
date = '12:23:23a'
self.assertRaises(ParameterUnvalid, timeago.format, date)
date = '2016-5-27 12:23:23'
self.assertRaises(ParameterUnvalid, timeago.format, date)
def test_timeago_except(self):
date = ''
self.assertRaises(ParameterUnvalid, timeago.format, date)
date = '12:23:23a'
self.assertRaises(ParameterUnvalid, timeago.format, date)
date = '2016-5-27 12:23:23'
self.assertRaises(ParameterUnvalid, timeago.format, date)
def format(date, now=None, locale='en'):
'''
the entry method
'''
if not isinstance(date, timedelta):
if now is None:
now = datetime.now()
date = parser.parse(date)
now = parser.parse(now)
if date is None:
raise ParameterUnvalid('the parameter `date` should be datetime '
'/ timedelta, or datetime formated string.')
if now is None:
raise ParameterUnvalid('the parameter `now` should be datetime, '
'or datetime formated string.')
date = now - date
# the gap sec
diff_seconds = int(total_seconds(date))
# is ago or in
ago_in = 0
if diff_seconds < 0:
ago_in = 1 # date is later then now, is the time in future
diff_seconds *= -1 # chango to positive
tmp = 0
i = 0
def format(date, now=None, locale='en'):
'''
the entry method
'''
if not isinstance(date, timedelta):
if now is None:
now = datetime.now()
date = parser.parse(date)
now = parser.parse(now)
if date is None:
raise ParameterUnvalid('the parameter `date` should be datetime '
'/ timedelta, or datetime formated string.')
if now is None:
raise ParameterUnvalid('the parameter `now` should be datetime, '
'or datetime formated string.')
date = now - date
# the gap sec
diff_seconds = int(total_seconds(date))
# is ago or in
ago_in = 0
if diff_seconds < 0:
ago_in = 1 # date is later then now, is the time in future
diff_seconds *= -1 # chango to positive
tmp = 0
i = 0
while i < SEC_ARRAY_LEN:
tmp = SEC_ARRAY[i]
if diff_seconds >= tmp: