1818import unittest
1919from abc import ABCMeta , abstractmethod
2020from contextlib import contextmanager
21- from datetime import datetime
21+ from datetime import datetime , timedelta , timezone
2222from itertools import zip_longest
2323from pathlib import Path
2424from unittest .mock import patch
2525
2626from parameterized import parameterized
2727
28- import can
29- from can .io import blf
28+ import can .io
29+ from can .io import asc , blf
30+
3031from .data .example_data import (
3132 TEST_COMMENTS ,
3233 TEST_MESSAGES_BASE ,
@@ -427,9 +428,11 @@ def _read_log_file(self, filename, **kwargs):
427428
428429 def test_read_absolute_time (self ):
429430 time_from_file = "Sat Sep 30 10:06:13.191 PM 2017"
430- start_time = datetime .strptime (
431- time_from_file , self .FORMAT_START_OF_FILE_DATE
432- ).timestamp ()
431+ start_time = (
432+ datetime .strptime (time_from_file , self .FORMAT_START_OF_FILE_DATE )
433+ .replace (tzinfo = asc ._LOCAL_TZ )
434+ .timestamp ()
435+ )
433436
434437 expected_messages = [
435438 can .Message (
@@ -629,51 +632,55 @@ def test_read_error_frame_channel(self):
629632 os .unlink (temp_file .name )
630633
631634 def test_write_millisecond_handling (self ):
635+ tz = asc ._LOCAL_TZ
632636 now = datetime (
633- year = 2017 , month = 9 , day = 30 , hour = 15 , minute = 6 , second = 13 , microsecond = 191456
637+ year = 2017 ,
638+ month = 9 ,
639+ day = 30 ,
640+ hour = 15 ,
641+ minute = 6 ,
642+ second = 13 ,
643+ microsecond = 191456 ,
644+ tzinfo = tz ,
634645 )
635646
636- # We temporarily set the locale to C to ensure test reproducibility
637- with override_locale (category = locale .LC_TIME , locale_str = "C" ):
638- # We mock datetime.now during ASCWriter __init__ for reproducibility
639- # Unfortunately, now() is a readonly attribute, so we mock datetime
640- with patch ("can.io.asc.datetime" ) as mock_datetime :
641- mock_datetime .now .return_value = now
642- writer = can .ASCWriter (self .test_file_name )
643-
644- msg = can .Message (
645- timestamp = now .timestamp (), arbitration_id = 0x123 , data = b"h"
646- )
647- writer .on_message_received (msg )
647+ with patch ("can.io.asc.datetime" ) as mock_datetime :
648+ mock_datetime .now .return_value = now
649+ writer = can .ASCWriter (self .test_file_name , tz = tz )
648650
649- writer .stop ()
651+ msg = can .Message (timestamp = now .timestamp (), arbitration_id = 0x123 , data = b"h" )
652+ writer .on_message_received (msg )
653+ writer .stop ()
650654
651655 actual_file = Path (self .test_file_name )
652656 expected_file = self ._get_logfile_location ("single_frame_us_locale.asc" )
653657
654658 self .assertEqual (expected_file .read_text (), actual_file .read_text ())
655659
656660 def test_write (self ):
657- now = datetime (
658- year = 2017 , month = 9 , day = 30 , hour = 15 , minute = 6 , second = 13 , microsecond = 191456
659- )
660-
661- # We temporarily set the locale to C to ensure test reproducibility
662- with override_locale (category = locale .LC_TIME , locale_str = "C" ):
663- # We mock datetime.now during ASCWriter __init__ for reproducibility
664- # Unfortunately, now() is a readonly attribute, so we mock datetime
665- with patch ("can.io.asc.datetime" ) as mock_datetime :
666- mock_datetime .now .return_value = now
667- writer = can .ASCWriter (self .test_file_name )
668-
669- msg = can .Message (
670- timestamp = now .timestamp (),
671- arbitration_id = 0x123 ,
672- data = range (64 ),
661+ tz = asc ._LOCAL_TZ
662+ with patch ("can.io.asc.datetime" ) as mock_datetime :
663+ now = datetime (
664+ year = 2017 ,
665+ month = 9 ,
666+ day = 30 ,
667+ hour = 15 ,
668+ minute = 6 ,
669+ second = 13 ,
670+ microsecond = 191456 ,
671+ tzinfo = tz ,
673672 )
673+ mock_datetime .now .return_value = now
674+ writer = can .ASCWriter (self .test_file_name , tz = tz )
674675
675- with writer :
676- writer .on_message_received (msg )
676+ msg = can .Message (
677+ timestamp = now .timestamp (),
678+ arbitration_id = 0x123 ,
679+ data = range (64 ),
680+ )
681+
682+ with writer :
683+ writer .on_message_received (msg )
677684
678685 actual_file = Path (self .test_file_name )
679686 expected_file = self ._get_logfile_location ("single_frame.asc" )
@@ -684,34 +691,48 @@ def test_write(self):
684691 [
685692 (
686693 "May 27 04:09:35.000 pm 2014" ,
687- datetime (2014 , 5 , 27 , 16 , 9 , 35 , 0 ).timestamp (),
694+ datetime (
695+ 2014 , 5 , 27 , 16 , 9 , 35 , 0 , tzinfo = timezone (timedelta (hours = 5 ))
696+ ).timestamp (),
688697 ),
689698 (
690699 "Mai 27 04:09:35.000 pm 2014" ,
691- datetime (2014 , 5 , 27 , 16 , 9 , 35 , 0 ).timestamp (),
700+ datetime (
701+ 2014 , 5 , 27 , 16 , 9 , 35 , 0 , tzinfo = timezone (timedelta (hours = 5 ))
702+ ).timestamp (),
692703 ),
693704 (
694705 "Apr 28 10:44:52.480 2022" ,
695- datetime (2022 , 4 , 28 , 10 , 44 , 52 , 480000 ).timestamp (),
706+ datetime (
707+ 2022 , 4 , 28 , 10 , 44 , 52 , 480000 , tzinfo = timezone (timedelta (hours = 5 ))
708+ ).timestamp (),
696709 ),
697710 (
698711 "Sep 30 15:06:13.191 2017" ,
699- datetime (2017 , 9 , 30 , 15 , 6 , 13 , 191000 ).timestamp (),
712+ datetime (
713+ 2017 , 9 , 30 , 15 , 6 , 13 , 191000 , tzinfo = timezone (timedelta (hours = 5 ))
714+ ).timestamp (),
700715 ),
701716 (
702717 "Sep 30 15:06:13.191 pm 2017" ,
703- datetime (2017 , 9 , 30 , 15 , 6 , 13 , 191000 ).timestamp (),
718+ datetime (
719+ 2017 , 9 , 30 , 15 , 6 , 13 , 191000 , tzinfo = timezone (timedelta (hours = 5 ))
720+ ).timestamp (),
704721 ),
705722 (
706723 "Sep 30 15:06:13.191 am 2017" ,
707- datetime (2017 , 9 , 30 , 15 , 6 , 13 , 191000 ).timestamp (),
724+ datetime (
725+ 2017 , 9 , 30 , 15 , 6 , 13 , 191000 , tzinfo = timezone (timedelta (hours = 5 ))
726+ ).timestamp (),
708727 ),
709728 ]
710729 )
711730 def test_datetime_to_timestamp (
712731 self , datetime_string : str , expected_timestamp : float
713732 ):
714- timestamp = can .ASCReader ._datetime_to_timestamp (datetime_string )
733+ timestamp = can .ASCReader ._datetime_to_timestamp (
734+ datetime_string , tz = timezone (timedelta (hours = 5 ))
735+ )
715736 self .assertAlmostEqual (timestamp , expected_timestamp )
716737
717738
0 commit comments