Take DST into account when converting to UTC
The UTC conversion was not taking DST into account. Now, it will be properly calculated. Added more tests to cover. Change-Id: I991dcf1fa6fddad28a1ab9e341c55d4396e4496f Closes-Bug: #1675388
This commit is contained in:
parent
6ae1bad9a0
commit
4075484cb5
mistralclient
@ -159,9 +159,12 @@ class Create(command.ShowOne):
|
||||
if the_time:
|
||||
the_time = datetime.datetime.strptime(
|
||||
the_time, datetime_format)
|
||||
the_second = time.mktime(the_time.timetuple())
|
||||
the_utc_time = datetime.datetime.utcfromtimestamp(the_second)
|
||||
the_time = the_utc_time.strftime(datetime_format)
|
||||
|
||||
is_dst = time.daylight and time.localtime().tm_isdst > 0
|
||||
utc_offset = - (time.altzone if is_dst else time.timezone)
|
||||
|
||||
the_time = (the_time - datetime.timedelta(
|
||||
0, utc_offset)).strftime(datetime_format)
|
||||
|
||||
return the_time
|
||||
|
||||
|
@ -14,9 +14,7 @@
|
||||
# under the License.
|
||||
#
|
||||
|
||||
import datetime
|
||||
import mock
|
||||
import time
|
||||
|
||||
from mistralclient.api.v2 import cron_triggers
|
||||
from mistralclient.commands.v2 import cron_triggers as cron_triggers_cmd
|
||||
@ -88,17 +86,54 @@ class TestCLITriggersV2(base.BaseCommandTest):
|
||||
result[1]
|
||||
)
|
||||
|
||||
def test_convert_time_string_to_utc(self):
|
||||
@mock.patch('mistralclient.commands.v2.cron_triggers.time')
|
||||
def test_convert_time_string_to_utc_from_utc(self, mock_time):
|
||||
cmd = cron_triggers_cmd.Create(self.app, None)
|
||||
|
||||
mock_time.daylight = 0
|
||||
mock_time.altzone = 0
|
||||
mock_time.timezone = 0
|
||||
mock_localtime = mock.Mock()
|
||||
mock_localtime.tm_isdst = 0
|
||||
mock_time.localtime.return_value = mock_localtime
|
||||
|
||||
utc_value = cmd._convert_time_string_to_utc('4242-12-20 13:37')
|
||||
|
||||
is_dst = time.daylight and time.localtime().tm_isdst > 0
|
||||
utc_offset = - (time.altzone if is_dst else time.timezone)
|
||||
expected_time = '4242-12-20 13:37'
|
||||
|
||||
expected_time = (datetime.datetime(
|
||||
4242, 12, 20, 13, 37) - datetime.timedelta(
|
||||
0, utc_offset)).strftime('%Y-%m-%d %H:%M')
|
||||
self.assertEqual(expected_time, utc_value)
|
||||
|
||||
@mock.patch('mistralclient.commands.v2.cron_triggers.time')
|
||||
def test_convert_time_string_to_utc_from_dst(self, mock_time):
|
||||
cmd = cron_triggers_cmd.Create(self.app, None)
|
||||
|
||||
mock_time.daylight = 1
|
||||
mock_time.altzone = (4 * 60 * 60)
|
||||
mock_time.timezone = (5 * 60 * 60)
|
||||
mock_localtime = mock.Mock()
|
||||
mock_localtime.tm_isdst = 1
|
||||
mock_time.localtime.return_value = mock_localtime
|
||||
|
||||
utc_value = cmd._convert_time_string_to_utc('4242-12-20 13:37')
|
||||
|
||||
expected_time = '4242-12-20 17:37'
|
||||
|
||||
self.assertEqual(expected_time, utc_value)
|
||||
|
||||
@mock.patch('mistralclient.commands.v2.cron_triggers.time')
|
||||
def test_convert_time_string_to_utc_no_dst(self, mock_time):
|
||||
cmd = cron_triggers_cmd.Create(self.app, None)
|
||||
|
||||
mock_time.daylight = 1
|
||||
mock_time.altzone = (4 * 60 * 60)
|
||||
mock_time.timezone = (5 * 60 * 60)
|
||||
mock_localtime = mock.Mock()
|
||||
mock_localtime.tm_isdst = 0
|
||||
mock_time.localtime.return_value = mock_localtime
|
||||
|
||||
utc_value = cmd._convert_time_string_to_utc('4242-12-20 13:37')
|
||||
|
||||
expected_time = '4242-12-20 18:37'
|
||||
|
||||
self.assertEqual(expected_time, utc_value)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user