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:
@@ -159,9 +159,12 @@ class Create(command.ShowOne):
|
|||||||
if the_time:
|
if the_time:
|
||||||
the_time = datetime.datetime.strptime(
|
the_time = datetime.datetime.strptime(
|
||||||
the_time, datetime_format)
|
the_time, datetime_format)
|
||||||
the_second = time.mktime(the_time.timetuple())
|
|
||||||
the_utc_time = datetime.datetime.utcfromtimestamp(the_second)
|
is_dst = time.daylight and time.localtime().tm_isdst > 0
|
||||||
the_time = the_utc_time.strftime(datetime_format)
|
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
|
return the_time
|
||||||
|
|
||||||
|
@@ -14,9 +14,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
import datetime
|
|
||||||
import mock
|
import mock
|
||||||
import time
|
|
||||||
|
|
||||||
from mistralclient.api.v2 import cron_triggers
|
from mistralclient.api.v2 import cron_triggers
|
||||||
from mistralclient.commands.v2 import cron_triggers as cron_triggers_cmd
|
from mistralclient.commands.v2 import cron_triggers as cron_triggers_cmd
|
||||||
@@ -88,17 +86,54 @@ class TestCLITriggersV2(base.BaseCommandTest):
|
|||||||
result[1]
|
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)
|
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')
|
utc_value = cmd._convert_time_string_to_utc('4242-12-20 13:37')
|
||||||
|
|
||||||
is_dst = time.daylight and time.localtime().tm_isdst > 0
|
expected_time = '4242-12-20 13:37'
|
||||||
utc_offset = - (time.altzone if is_dst else time.timezone)
|
|
||||||
|
|
||||||
expected_time = (datetime.datetime(
|
self.assertEqual(expected_time, utc_value)
|
||||||
4242, 12, 20, 13, 37) - datetime.timedelta(
|
|
||||||
0, utc_offset)).strftime('%Y-%m-%d %H:%M')
|
@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)
|
self.assertEqual(expected_time, utc_value)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user