33# Copyright 2020-2021, Stefan Valouch (svalouch)
44# SPDX-License-Identifier: GPL-3.0-only
55
6+ import pytz
67import struct
78from datetime import datetime
89from typing import overload , Dict , Tuple , Union
1617
1718from .types import DataType , EventEntry
1819
20+ timezone = pytz .timezone ('Europe/Berlin' )
1921
2022# pylint: disable=invalid-name
2123def CRC16 (data : Union [bytes , bytearray ]) -> int :
@@ -187,12 +189,12 @@ def _decode_timeseries(data: bytes) -> Tuple[datetime, Dict[datetime, int]]:
187189 '''
188190 Helper function to decode the timeseries type.
189191 '''
190- timestamp = datetime .fromtimestamp (struct .unpack ('>I' , data [0 :4 ])[0 ])
192+ timestamp = timezone . localize ( datetime .utcfromtimestamp (struct .unpack ('>I' , data [0 :4 ])[0 ]) )
191193 tsval : Dict [datetime , int ] = dict ()
192194 assert len (data ) % 4 == 0 , 'Data should be divisible by 4'
193195 assert int (len (data ) / 4 % 2 ) == 1 , 'Data should be an even number of 4-byte pairs plus the starting timestamp'
194196 for pair in range (0 , int (len (data ) / 4 - 1 ), 2 ):
195- pair_ts = datetime .fromtimestamp (struct .unpack ('>I' , data [4 + pair * 4 :4 + pair * 4 + 4 ])[0 ])
197+ pair_ts = timezone . localize ( datetime .utcfromtimestamp (struct .unpack ('>I' , data [4 + pair * 4 :4 + pair * 4 + 4 ])[0 ]) )
196198 pair_val = struct .unpack ('>f' , data [4 + pair * 4 + 4 :4 + pair * 4 + 4 + 4 ])[0 ]
197199 tsval [pair_ts ] = pair_val
198200 return timestamp , tsval
@@ -202,15 +204,15 @@ def _decode_event_table(data: bytes) -> Tuple[datetime, Dict[datetime, EventEntr
202204 '''
203205 Helper function to decode the event table type.
204206 '''
205- timestamp = datetime .fromtimestamp (struct .unpack ('>I' , data [0 :4 ])[0 ])
207+ timestamp = timezone . localize ( datetime .utcfromtimestamp (struct .unpack ('>I' , data [0 :4 ])[0 ]) )
206208 tabval : Dict [datetime , EventEntry ] = dict ()
207209 assert len (data ) % 4 == 0
208210 assert (len (data ) - 4 ) % 20 == 0
209211 for pair in range (0 , int (len (data ) / 4 - 1 ), 5 ):
210212 # this is most likely a single byte of information, but this is not sure yet
211213 # entry_type = bytes([struct.unpack('>I', data[4 + pair * 4:4 + pair * 4 + 4])[0]]).decode('ascii')
212214 entry_type = struct .unpack ('>I' , data [4 + pair * 4 :4 + pair * 4 + 4 ])[0 ]
213- timestamp = datetime .fromtimestamp (struct .unpack ('>I' , data [4 + pair * 4 + 4 :4 + pair * 4 + 8 ])[0 ])
215+ timestamp = timezone . localize ( datetime .utcfromtimestamp (struct .unpack ('>I' , data [4 + pair * 4 + 4 :4 + pair * 4 + 8 ])[0 ]) )
214216 element2 = struct .unpack ('>I' , data [4 + pair * 4 + 8 :4 + pair * 4 + 12 ])[0 ]
215217 element3 = struct .unpack ('>I' , data [4 + pair * 4 + 12 :4 + pair * 4 + 16 ])[0 ]
216218 element4 = struct .unpack ('>I' , data [4 + pair * 4 + 16 :4 + pair * 4 + 20 ])[0 ]
@@ -225,8 +227,8 @@ def _decode_event_table(data: bytes) -> Tuple[datetime, Dict[datetime, EventEntr
225227 # value_old=value_old, value_new=value_new)
226228 # the rest is assumed to be range-based events
227229 # else:
228- # timestamp_end = datetime.fromtimestamp (
229- # struct.unpack('>I', data[4 + pair * 4 + 12:4 + pair * 4 + 16])[0])
230+ # timestamp_end = timezone.localize( datetime.utcfromtimestamp (
231+ # struct.unpack('>I', data[4 + pair * 4 + 12:4 + pair * 4 + 16])[0]))
230232 # object_id = struct.unpack('>I', data[4 + pair * 4 + 16:4 + pair * 4 + 20])[0]
231233 # tabval[timestamp] = EventEntry(timestamp=timestamp, object_id=object_id, entry_type=entry_type,
232234 # timestamp_end=timestamp_end)
0 commit comments