From bb2fc58d43a2e8f2dfc0e4b606bdcd94d802860b Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:34:29 +0800 Subject: [PATCH 1/7] fix: Use "UTC" instead of local time --- systime/systime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systime/systime.py b/systime/systime.py index f113963..cb03347 100644 --- a/systime/systime.py +++ b/systime/systime.py @@ -15,7 +15,7 @@ class SysTime(object): @staticmethod def get_system_time(): - return datetime.now(tz.tzlocal()).strftime("%Y-%m-%dT%H:%M:%S.%f%z") + return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ") @staticmethod def set_system_time(time_string): From f0ba8a8b9f4fb670127b38ae89cd36dc9e207301 Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:35:06 +0800 Subject: [PATCH 2/7] fix: Update schema --- index.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.py b/index.py index 745d795..ebc594f 100755 --- a/index.py +++ b/index.py @@ -14,16 +14,23 @@ from voluptuous import Range from voluptuous import All from voluptuous import Length +from voluptuous import Optional +from datetime import datetime _logger = logging.getLogger("sanji.time") +def Timestamp(value): + datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ") + return value + + class Index(Sanji): PUT_SCHEMA = Schema({ - "time": All(str, Length(1, 255)), - "timezone": All(str, Length(0, 255)), - "ntp": { + Optional("time"): Timestamp, + Optional("timezone"): All(str, Length(0, 255)), + Optional("ntp"): { "enable": bool, "server": All(str, Length(1, 2048)), "interval": All(int, Range(min=60, max=60*60*24*30)) From 0f2784bf470bb89cc638b1b882c9aee244df9070 Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:35:40 +0800 Subject: [PATCH 3/7] feat: Provide timezone offset in zoneinfo --- schema/index.yaml | 4 ++++ systime/systime.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/schema/index.yaml b/schema/index.yaml index 3e9bd4a..b75d568 100644 --- a/schema/index.yaml +++ b/schema/index.yaml @@ -137,6 +137,7 @@ definitions: required: - cca2 - name + - offset properties: cca2: description: ISO 3166 alpha-2 country code @@ -145,6 +146,9 @@ definitions: name: description: zone name type: string + offset: + description: zone offset (format should be "+0800", etc) + type: string TimezoneIso3166: description: ISO 3166 alpha-2 country code and country name diff --git a/systime/systime.py b/systime/systime.py index cb03347..4eb9a42 100644 --- a/systime/systime.py +++ b/systime/systime.py @@ -50,7 +50,11 @@ def get_system_timezone_list(): for line in f: if not line.startswith("#"): zone = line.rstrip().split("\t") - zonetab.append({"cca2": zone[0], "name": zone[2]}) + zonetab.append({ + "cca2": zone[0], + "name": zone[2], + "offset": datetime.now( + tz.gettz(zone[2])).strftime("%z")}) # list iso3166.tab iso3166tab = [] From 6d403d1703eaaf21ce7e1f02444eee7e273958aa Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:36:02 +0800 Subject: [PATCH 4/7] test: Fix unittest --- tests/test_index.py | 46 +++++++++++++++++++++++------- tests/test_systime/test_systime.py | 4 +-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/tests/test_index.py b/tests/test_index.py index c6eb2fe..a419f79 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -14,6 +14,8 @@ from sanji.connection.mockup import Mockup from sanji.message import Message +from voluptuous import er + try: sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../") from index import Index @@ -54,6 +56,34 @@ def test_get(self): self.index.get(message=None, response=resp, test=True) resp.assert_called_once_with(data=result) + def test_put_schema__should_pass(self): + # arrange + SUT = { + "time": "2015-03-26T16:27:48.611441Z", + "timezone": "Asia/Taipei", + "ntp": { + "enable": True, + "server": "pool.ntp.org", + "interval": 7200 + } + } + + # act + data = Index.PUT_SCHEMA(SUT) + + # assert + self.assertEqual(SUT, data) + + def test_put_schema__time_empty_should_fail(self): + # arrange + SUT = { + "time": "" + } + + # act + with self.assertRaises(er.MultipleInvalid): + Index.PUT_SCHEMA(SUT) + def test_put(self): result = { "time": ANY, @@ -99,7 +129,8 @@ def test_put(self): with patch("index.SysTime.set_system_time") as set_system_time: resp = Mock() set_system_time.return_value = True - msg = Message({"data": {"time": ""}}) + msg = Message( + {"data": {"time": "2015-03-26T16:27:48.611441Z"}}) self.index.put(message=msg, response=resp, test=True) resp.assert_called_once_with(data=result) @@ -110,14 +141,7 @@ def test_put(self): resp.assert_called_once_with( code=500, data={"message": "Change system time failed."}) - # case 7: change system time (Abnormal 2) - resp = Mock() - msg = Message({"data": {"time": ""}}) - self.index.put(message=msg, response=resp, test=True) - resp.assert_called_once_with( - code=400, data={"message": "Time format error."}) - - # case 8: update ntp settings (Normal) + # case 7: update ntp settings (Normal) with patch.object(self.index.ntp, "update") as update: resp = Mock() update.return_value = True @@ -129,14 +153,14 @@ def test_put(self): self.index.put(message=msg, response=resp, test=True) resp.assert_called_once_with(data=result) - # case 9: update ntp settings (Abnormal 1) + # case 8: update ntp settings (Abnormal 1) resp = Mock() update.return_value = False self.index.put(message=msg, response=resp, test=True) resp.assert_called_once_with( code=500, data={"message": "Update ntp settings failed."}) - # case 10: update ntp settings (Abnormal 2) + # case 9: update ntp settings (Abnormal 2) resp = Mock() update.side_effect = RuntimeError("Some exception.") self.index.put(message=msg, response=resp, test=True) diff --git a/tests/test_systime/test_systime.py b/tests/test_systime/test_systime.py index a1a58a3..e6bac1b 100644 --- a/tests/test_systime/test_systime.py +++ b/tests/test_systime/test_systime.py @@ -23,7 +23,7 @@ class TestTimeClass(unittest.TestCase): def test_get_system_time(self): self.assertEqual( - datetime.now().strftime("%Y-%m-%dT%H:%M")[0:13], + datetime.utcnow().strftime("%Y-%m-%dT%H:%M")[0:13], SysTime.get_system_time()[0:13]) def test_set_system_time(self): @@ -42,7 +42,7 @@ def test_set_system_time(self): # case 3: invaild input with self.assertRaises(ValueError): - SysTime.set_system_time("2015-0-26T16:27:48.611441Z") + SysTime.set_system_time("2015-0-26T16:27:48.611441+0800") def test_set_system_timezone(self): with patch("systime.systime.subprocess") as subprocess: From 18f54e6f40b824e7cdaf44c5aa56263081301b6b Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:50:01 +0800 Subject: [PATCH 5/7] test: fix pylint --- index.py | 1 + 1 file changed, 1 insertion(+) diff --git a/index.py b/index.py index ebc594f..94dca9e 100755 --- a/index.py +++ b/index.py @@ -103,6 +103,7 @@ def put(self, message, response): return response( data=dict(self.model.db.items() + realtime_data.items())) + if __name__ == "__main__": from sanji.connection.mqtt import Mqtt FORMAT = "%(asctime)s - %(levelname)s - %(lineno)s - %(message)s" From 7feb0df2170424d46cc67fc93f26ff59283b3ef0 Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:45:39 +0800 Subject: [PATCH 6/7] docs: Update examples --- schema/index.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/schema/index.yaml b/schema/index.yaml index b75d568..d05e6b6 100644 --- a/schema/index.yaml +++ b/schema/index.yaml @@ -3,7 +3,7 @@ swagger: '2.0' info: title: System Time API description: System Time management - version: '1.0.0' + version: '1.1.0' schemes: - http @@ -190,14 +190,17 @@ externalDocs: { "cca2": "AD", "name": "Europe/Andorra", + "offset": "+0100" }, { "cca2": "AE", "name": "Asia/Dubai", + "offset": "+0400" }, { "cca2": "AF", "name": "Asia/Kabul", + "offset": "+0430" } ], "iso3166": [ From 8daf0a61355b9082cc5126d39fb1393e16000265 Mon Sep 17 00:00:00 2001 From: Aeluin Chen Date: Fri, 24 Mar 2017 17:39:29 +0800 Subject: [PATCH 7/7] 2.1.0 --- build-deb/debian/changelog | 7 +++++++ bundle.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/build-deb/debian/changelog b/build-deb/debian/changelog index 647178c..ae84670 100644 --- a/build-deb/debian/changelog +++ b/build-deb/debian/changelog @@ -1,3 +1,10 @@ +sanji-bundle-time (2.1.0-1) unstable; urgency=low + + * fix: Use "UTC" instead of local time. + * feat: Provide timezone offset in zoneinfo. + + -- Aeluin Chen Fri, 24 Mar 2017 17:38:47 +0800 + sanji-bundle-time (2.0.1-1) unstable; urgency=low * fix: Pass local time instead of UTC. diff --git a/bundle.json b/bundle.json index fd46b21..7eadb08 100644 --- a/bundle.json +++ b/bundle.json @@ -1,6 +1,6 @@ { "name": "time", - "version": "2.0.1", + "version": "2.1.0", "author": "Zack YL Shih", "email": "ZackYL.Shih@moxa.com", "description": "Provides the system-time management function",