Fix time formatting for timezone values
Fixes: https://pagure.io/koji/issue/2423
This commit is contained in:
parent
44db1bc8ee
commit
b0efed5e37
2 changed files with 50 additions and 3 deletions
|
|
@ -50,6 +50,7 @@ import xml.sax
|
|||
import xml.sax.handler
|
||||
from fnmatch import fnmatch
|
||||
|
||||
import dateutil.parser
|
||||
import requests
|
||||
import six
|
||||
import six.moves.configparser
|
||||
|
|
@ -3290,10 +3291,18 @@ def formatTimeLong(value):
|
|||
"""
|
||||
if not value:
|
||||
return ''
|
||||
if isinstance(value, six.string_types):
|
||||
t = dateutil.parser.parse(value)
|
||||
elif isinstance(value, xmlrpc_client.DateTime):
|
||||
t = dateutil.parser.parse(value.value)
|
||||
else:
|
||||
# Assume the string value passed in is the local time
|
||||
localtime = time.mktime(time.strptime(formatTime(value), '%Y-%m-%d %H:%M:%S'))
|
||||
return time.strftime('%a, %d %b %Y %H:%M:%S %Z', time.localtime(localtime))
|
||||
t = value
|
||||
# return date in local timezone, py 2.6 has tzone as astimezone required parameter
|
||||
# would work simply as t.astimezone() for py 2.7+
|
||||
if t.tzinfo is None:
|
||||
t = t.replace(tzinfo=dateutil.tz.gettz())
|
||||
t = t.astimezone(dateutil.tz.gettz())
|
||||
return datetime.datetime.strftime(t, '%a, %d %b %Y %H:%M:%S %Z')
|
||||
|
||||
|
||||
def buildLabel(buildInfo, showEpoch=False):
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
from __future__ import absolute_import
|
||||
import datetime
|
||||
import os
|
||||
import time
|
||||
import locale
|
||||
try:
|
||||
import unittest2 as unittest
|
||||
|
|
@ -11,6 +13,16 @@ import six.moves.xmlrpc_client as xmlrpc_client
|
|||
from koji import formatTime, formatTimeLong
|
||||
|
||||
class TestFormatTime(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._orig_tz = os.environ.get('TZ')
|
||||
|
||||
def tearDown(self):
|
||||
if self._orig_tz:
|
||||
os.environ['TZ'] = self._orig_tz
|
||||
elif 'TZ' in os.environ:
|
||||
del os.environ['TZ']
|
||||
time.tzset()
|
||||
|
||||
def test_format_time(self):
|
||||
self.assertEqual(formatTime(None), '')
|
||||
self.assertEqual(formatTime(''), '')
|
||||
|
|
@ -33,6 +45,8 @@ class TestFormatTime(unittest.TestCase):
|
|||
def test_format_time_long(self):
|
||||
# force locale to compare 'desired' value
|
||||
locale.setlocale(locale.LC_ALL, ('en_US', 'UTF-8'))
|
||||
os.environ['TZ'] = 'GMT'
|
||||
time.tzset()
|
||||
|
||||
self.assertEqual(formatTimeLong(None), '')
|
||||
self.assertEqual(formatTimeLong(''), '')
|
||||
|
|
@ -62,4 +76,28 @@ class TestFormatTime(unittest.TestCase):
|
|||
r = r[:r.rfind(' ')]
|
||||
self.assertEqual(r, desired)
|
||||
|
||||
# str + timezone
|
||||
d3 = '2017-10-05 09:52:31+02:00'
|
||||
desired = 'Thu, 05 Oct 2017 07:52:31 GMT'
|
||||
os.environ['TZ'] = 'GMT'
|
||||
time.tzset()
|
||||
r = formatTimeLong(d3)
|
||||
self.assertEqual(r, desired)
|
||||
|
||||
# non-GMT without DST
|
||||
d3 = '2017-06-05 09:52:31+02:00'
|
||||
desired = 'Mon, 05 Jun 2017 09:52:31 CEST'
|
||||
os.environ['TZ'] = 'Europe/Prague'
|
||||
time.tzset()
|
||||
r = formatTimeLong(d3)
|
||||
self.assertEqual(r, desired)
|
||||
|
||||
# non-GMT with DST
|
||||
d3 = '2017-12-05 09:52:31+02:00'
|
||||
desired = 'Tue, 05 Dec 2017 08:52:31 CET'
|
||||
os.environ['TZ'] = 'Europe/Prague'
|
||||
time.tzset()
|
||||
r = formatTimeLong(d3)
|
||||
self.assertEqual(r, desired)
|
||||
|
||||
locale.resetlocale()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue