Skip to content

Commit 0f83289

Browse files
authored
Merge pull request #138 from lcmcninch/fix_weekday
receive_time: fix week day
2 parents 6587c92 + c5d1975 commit 0f83289

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

Adafruit_IO/client.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
21+
import time
2122
from time import struct_time
2223
import json
2324
import platform
@@ -182,9 +183,19 @@ def receive_time(self):
182183
https://docs.python.org/3.7/library/time.html#time.struct_time
183184
"""
184185
path = 'integrations/time/struct.json'
185-
time = self._get(path)
186-
return struct_time((time['year'], time['mon'], time['mday'], time['hour'],
187-
time['min'], time['sec'], time['wday'], time['yday'], time['isdst']))
186+
return self._parse_time_struct(self._get(path))
187+
188+
@staticmethod
189+
def _parse_time_struct(time_dict: dict) -> time.struct_time:
190+
"""Parse the time data returned by the server and return a time_struct
191+
192+
Corrects for the weekday returned by the server in Sunday=0 format
193+
(Python expects Monday=0)
194+
"""
195+
wday = (time_dict['wday'] - 1) % 7
196+
return struct_time((time_dict['year'], time_dict['mon'], time_dict['mday'],
197+
time_dict['hour'], time_dict['min'], time_dict['sec'],
198+
wday, time_dict['yday'], time_dict['isdst']))
188199

189200
def receive_weather(self, weather_id=None):
190201
"""Adafruit IO Weather Service, Powered by Dark Sky

tests/test_client.py

+38-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TestClient(base.IOTestCase):
2323
# If your IP isn't put on the list of non-throttled IPs, uncomment the
2424
# function below to waste time between tests to prevent throttling.
2525
#def tearDown(self):
26-
time.sleep(30.0)
26+
# time.sleep(30.0)
2727

2828
# Helper Methods
2929
def get_client(self):
@@ -138,7 +138,7 @@ def test_create_data(self):
138138
data = Data(value=42)
139139
result = aio.create_data('testfeed', data)
140140
self.assertEqual(int(result.value), 42)
141-
141+
142142
def test_location_data(self):
143143
"""receive_location
144144
"""
@@ -160,11 +160,41 @@ def test_time_data(self):
160160
"""receive_time
161161
"""
162162
aio = self.get_client()
163-
time = aio.receive_time()
163+
server_time = aio.receive_time()
164164
# Check that each value is rx'd properly
165165
# (should never be None type)
166-
for time_data in time:
166+
for time_data in server_time:
167167
self.assertIsNotNone(time_data)
168+
# Check that the week day was interpreted properly
169+
adjusted_time = time.localtime(time.mktime(server_time))
170+
self.assertEqual(server_time.tm_wday, adjusted_time.tm_wday)
171+
172+
def test_parse_time_struct(self):
173+
"""Ensure the _parse_time_struct method properly handles all 7
174+
week days. Particularly important to make sure Sunday is 6,
175+
not -1"""
176+
# Zero time is a dictionary as would be provided by server
177+
# (wday is one higher than it should be)
178+
zero_time = {'year': 1970,
179+
'mon': 1,
180+
'mday': 1,
181+
'hour': 0,
182+
'min': 0,
183+
'sec': 0,
184+
'wday': 4,
185+
'yday': 1,
186+
'isdst': 0}
187+
188+
# Create a good struct for each day of the week and make sure
189+
# the server-style dictionary is parsed correctly
190+
for k in range(7):
191+
real_struct = time.gmtime(k * 86400)
192+
d = zero_time.copy()
193+
d['mday'] += k
194+
d['wday'] += k
195+
d['yday'] += k
196+
newd = Client._parse_time_struct(d)
197+
self.assertEqual(newd.tm_wday, real_struct.tm_wday)
168198

169199
# Test Feed Functionality
170200
def test_append_by_feed_name(self):
@@ -269,3 +299,7 @@ def test_receive_group_by_key(self):
269299
group = io.create_group(Group(name='grouprx'))
270300
response = io.groups(group.key)
271301
self.assertEqual(response.key, 'grouprx')
302+
303+
304+
if __name__ == "__main__":
305+
unittest.main()

0 commit comments

Comments
 (0)