Skip to content

Commit

Permalink
Merge pull request #35 from lwindg/develop
Browse files Browse the repository at this point in the history
2.1.0
  • Loading branch information
imZack authored Mar 24, 2017
2 parents 5342e8a + 8daf0a6 commit 977a0c9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 20 deletions.
7 changes: 7 additions & 0 deletions build-deb/debian/changelog
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> 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.
Expand Down
2 changes: 1 addition & 1 deletion bundle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "time",
"version": "2.0.1",
"version": "2.1.0",
"author": "Zack YL Shih",
"email": "[email protected]",
"description": "Provides the system-time management function",
Expand Down
14 changes: 11 additions & 3 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -96,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"
Expand Down
9 changes: 8 additions & 1 deletion schema/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -137,6 +137,7 @@ definitions:
required:
- cca2
- name
- offset
properties:
cca2:
description: ISO 3166 alpha-2 country code
Expand All @@ -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
Expand Down Expand Up @@ -186,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": [
Expand Down
8 changes: 6 additions & 2 deletions systime/systime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = []
Expand Down
46 changes: 35 additions & 11 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_systime/test_systime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down

0 comments on commit 977a0c9

Please sign in to comment.