Skip to content

Commit 8baa4f6

Browse files
Refactor time zone handling
1 parent c98dca6 commit 8baa4f6

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/rctclient/utils.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright 2020-2021, Stefan Valouch (svalouch)
44
# SPDX-License-Identifier: GPL-3.0-only
55

6-
import pytz
76
import struct
87
from datetime import datetime
98
from typing import overload, Dict, Tuple, Union
@@ -17,8 +16,6 @@
1716

1817
from .types import DataType, EventEntry
1918

20-
timezone = pytz.timezone('Europe/Berlin')
21-
2219
# pylint: disable=invalid-name
2320
def CRC16(data: Union[bytes, bytearray]) -> int:
2421
'''
@@ -189,12 +186,12 @@ def _decode_timeseries(data: bytes) -> Tuple[datetime, Dict[datetime, int]]:
189186
'''
190187
Helper function to decode the timeseries type.
191188
'''
192-
timestamp = timezone.localize(datetime.utcfromtimestamp(struct.unpack('>I', data[0:4])[0]))
189+
timestamp = datetime.utcfromtimestamp(struct.unpack('>I', data[0:4])[0])
193190
tsval: Dict[datetime, int] = dict()
194191
assert len(data) % 4 == 0, 'Data should be divisible by 4'
195192
assert int(len(data) / 4 % 2) == 1, 'Data should be an even number of 4-byte pairs plus the starting timestamp'
196193
for pair in range(0, int(len(data) / 4 - 1), 2):
197-
pair_ts = timezone.localize(datetime.utcfromtimestamp(struct.unpack('>I', data[4 + pair * 4:4 + pair * 4 + 4])[0]))
194+
pair_ts = datetime.utcfromtimestamp(struct.unpack('>I', data[4 + pair * 4:4 + pair * 4 + 4])[0])
198195
pair_val = struct.unpack('>f', data[4 + pair * 4 + 4:4 + pair * 4 + 4 + 4])[0]
199196
tsval[pair_ts] = pair_val
200197
return timestamp, tsval
@@ -204,15 +201,15 @@ def _decode_event_table(data: bytes) -> Tuple[datetime, Dict[datetime, EventEntr
204201
'''
205202
Helper function to decode the event table type.
206203
'''
207-
timestamp = timezone.localize(datetime.utcfromtimestamp(struct.unpack('>I', data[0:4])[0]))
204+
timestamp = datetime.utcfromtimestamp(struct.unpack('>I', data[0:4])[0])
208205
tabval: Dict[datetime, EventEntry] = dict()
209206
assert len(data) % 4 == 0
210207
assert (len(data) - 4) % 20 == 0
211208
for pair in range(0, int(len(data) / 4 - 1), 5):
212209
# this is most likely a single byte of information, but this is not sure yet
213210
# entry_type = bytes([struct.unpack('>I', data[4 + pair * 4:4 + pair * 4 + 4])[0]]).decode('ascii')
214211
entry_type = struct.unpack('>I', data[4 + pair * 4:4 + pair * 4 + 4])[0]
215-
timestamp = timezone.localize(datetime.utcfromtimestamp(struct.unpack('>I', data[4 + pair * 4 + 4:4 + pair * 4 + 8])[0]))
212+
timestamp = datetime.utcfromtimestamp(struct.unpack('>I', data[4 + pair * 4 + 4:4 + pair * 4 + 8])[0])
216213
element2 = struct.unpack('>I', data[4 + pair * 4 + 8:4 + pair * 4 + 12])[0]
217214
element3 = struct.unpack('>I', data[4 + pair * 4 + 12:4 + pair * 4 + 16])[0]
218215
element4 = struct.unpack('>I', data[4 + pair * 4 + 16:4 + pair * 4 + 20])[0]
@@ -227,8 +224,8 @@ def _decode_event_table(data: bytes) -> Tuple[datetime, Dict[datetime, EventEntr
227224
# value_old=value_old, value_new=value_new)
228225
# the rest is assumed to be range-based events
229226
# else:
230-
# timestamp_end = timezone.localize(datetime.utcfromtimestamp(
231-
# struct.unpack('>I', data[4 + pair * 4 + 12:4 + pair * 4 + 16])[0]))
227+
# timestamp_end = datetime.utcfromtimestamp(
228+
# struct.unpack('>I', data[4 + pair * 4 + 12:4 + pair * 4 + 16])[0])
232229
# object_id = struct.unpack('>I', data[4 + pair * 4 + 16:4 + pair * 4 + 20])[0]
233230
# tabval[timestamp] = EventEntry(timestamp=timestamp, object_id=object_id, entry_type=entry_type,
234231
# timestamp_end=timestamp_end)

tools/timeseries2csv.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ def timeseries2csv(host: str, port: int, output: Optional[str], header_format: b
209209

210210
while highest_ts > ts_start and not iter_end:
211211
cprint(f'\ttimestamp: {highest_ts}')
212+
# rct power device seems to treat local time at GMT when converting from/to timestamps
212213
sock.send(make_frame(command=Command.WRITE, id=oid.object_id,
213214
payload=encode_value(DataType.INT32, int(highest_ts.replace(tzinfo=gmt).timestamp()))))
214215

@@ -256,6 +257,9 @@ def timeseries2csv(host: str, port: int, output: Optional[str], header_format: b
256257
# work with the data
257258
for t_ts, t_val in table.items():
258259

260+
# rct power device seems to treat local time at GMT when converting from/to timestamps
261+
t_ts = timezone.localize(t_ts)
262+
259263
# set the "highest" point in time to know what to request next when the day is not complete
260264
if t_ts < highest_ts:
261265
highest_ts = t_ts

0 commit comments

Comments
 (0)