33import re
44from datetime import UTC , datetime , timedelta
55from math import fmod
6- from typing import Any , overload
6+ from typing import Any , SupportsInt , overload
77
88from .singleton_local_time_zone import local_time_zone
99from .time_span import TimeSpan , total_microseconds
1010from .time_span import create as create_time_span
11- from .types import FSharpRef , float64
11+ from .types import FSharpRef , float64 , int32 , int64
1212from .util import DateKind
1313
1414
@@ -86,7 +86,7 @@ def create(
8686 s : int = 0 ,
8787 ms : int = 0 ,
8888 mc : int = 0 ,
89- kind : DateKind | None = None ,
89+ kind : int32 | None = None ,
9090) -> datetime :
9191 if kind == DateKind .UTC :
9292 date = datetime (
@@ -107,36 +107,36 @@ def create(
107107 return date
108108
109109
110- def year (d : datetime ) -> int :
111- return d .year
110+ def year (d : datetime ) -> int32 :
111+ return int32 ( d .year )
112112
113113
114- def month (d : datetime ) -> int :
115- return d .month
114+ def month (d : datetime ) -> int32 :
115+ return int32 ( d .month )
116116
117117
118- def day (d : datetime ) -> int :
119- return d .day
118+ def day (d : datetime ) -> int32 :
119+ return int32 ( d .day )
120120
121121
122- def hour (d : datetime ) -> int :
123- return d .hour
122+ def hour (d : datetime ) -> int32 :
123+ return int32 ( d .hour )
124124
125125
126- def minute (d : datetime ) -> int :
127- return d .minute
126+ def minute (d : datetime ) -> int32 :
127+ return int32 ( d .minute )
128128
129129
130- def second (d : datetime ) -> int :
131- return d .second
130+ def second (d : datetime ) -> int32 :
131+ return int32 ( d .second )
132132
133133
134- def millisecond (d : datetime ) -> int :
135- return d .microsecond // 1_000
134+ def millisecond (d : datetime ) -> int32 :
135+ return int32 ( d .microsecond // 1_000 )
136136
137137
138- def microsecond (d : datetime ) -> int :
139- return d .microsecond
138+ def microsecond (d : datetime ) -> int32 :
139+ return int32 ( d .microsecond )
140140
141141
142142def to_universal_time (d : datetime ) -> datetime :
@@ -365,15 +365,15 @@ def date_to_string_with_custom_format(date: datetime, format: str, utc: bool) ->
365365
366366 match kind (date ):
367367 case DateKind .UTC :
368- utc_offet_text = localized_date .strftime ("%z" )
368+ utc_offset_text = localized_date .strftime ("%z" )
369369 case DateKind .Local :
370- utc_offet_text = localized_date .strftime ("%z" )
371- case DateKind .Unspecified :
372- utc_offet_text = to_local_time (date ).strftime ("%z" )
370+ utc_offset_text = localized_date .strftime ("%z" )
371+ case _: # DateKind.Unspecified
372+ utc_offset_text = to_local_time (date ).strftime ("%z" )
373373
374- sign = utc_offet_text [:1 ]
375- hours = int (utc_offet_text [1 :3 ])
376- minutes = int (utc_offet_text [3 :5 ])
374+ sign = utc_offset_text [:1 ]
375+ hours = int (utc_offset_text [1 :3 ])
376+ minutes = int (utc_offset_text [3 :5 ])
377377
378378 match token_length :
379379 case 1 :
@@ -600,16 +600,16 @@ def days_in_month(year: int, month: int) -> int:
600600 return 31
601601
602602
603- def add_years (d : datetime , v : int ) -> datetime :
603+ def add_years (d : datetime , v : SupportsInt ) -> datetime :
604604 new_month = month (d )
605- new_year = year (d ) + v
605+ new_year = year (d ) + int ( v )
606606 _days_in_month = days_in_month (new_year , new_month )
607607 new_day = min (_days_in_month , day (d ))
608608 return create (new_year , new_month , new_day , hour (d ), minute (d ), second (d ), millisecond (d ), microsecond (d ), kind (d ))
609609
610610
611- def add_months (d : datetime , v : int ) -> datetime :
612- new_month = month (d ) + v
611+ def add_months (d : datetime , v : SupportsInt ) -> datetime :
612+ new_month = int ( month (d )) + int ( v )
613613 new_month_ = 0
614614 year_offset = 0
615615 if new_month > 12 :
@@ -620,37 +620,37 @@ def add_months(d: datetime, v: int) -> datetime:
620620 new_month_ = 12 + int (fmod (new_month , 12 ))
621621 year_offset = new_month // 12 + (- 1 if new_month_ == 12 else 0 )
622622 new_month = new_month_
623- new_year = year (d ) + year_offset
623+ new_year = int ( year (d ) ) + year_offset
624624 _days_in_month = days_in_month (new_year , new_month )
625- new_day = min (_days_in_month , day (d ))
625+ new_day = min (_days_in_month , int ( day (d ) ))
626626 return create (new_year , new_month , new_day , hour (d ), minute (d ), second (d ), millisecond (d ), microsecond (d ), kind (d ))
627627
628628
629- def add_days (d : datetime , v : int ) -> datetime :
629+ def add_days (d : datetime , v : SupportsInt ) -> datetime :
630630 return d + timedelta (days = int (v ))
631631
632632
633- def add_hours (d : datetime , v : int ) -> datetime :
633+ def add_hours (d : datetime , v : SupportsInt ) -> datetime :
634634 return d + timedelta (hours = int (v ))
635635
636636
637- def add_minutes (d : datetime , v : int ) -> datetime :
637+ def add_minutes (d : datetime , v : SupportsInt ) -> datetime :
638638 return d + timedelta (minutes = int (v ))
639639
640640
641- def add_seconds (d : datetime , v : int ) -> datetime :
641+ def add_seconds (d : datetime , v : SupportsInt ) -> datetime :
642642 return d + timedelta (seconds = int (v ))
643643
644644
645- def add_milliseconds (d : datetime , v : int ) -> datetime :
645+ def add_milliseconds (d : datetime , v : SupportsInt ) -> datetime :
646646 return d + timedelta (milliseconds = int (v ))
647647
648648
649- def add_microseconds (d : datetime , v : int ) -> datetime :
649+ def add_microseconds (d : datetime , v : SupportsInt ) -> datetime :
650650 return d + timedelta (microseconds = int (v ))
651651
652652
653- def kind (d : datetime ) -> DateKind :
653+ def kind (d : datetime ) -> int32 :
654654 if d .tzinfo == UTC :
655655 return DateKind .UTC
656656
@@ -661,11 +661,11 @@ def kind(d: datetime) -> DateKind:
661661 return DateKind .Local
662662
663663
664- def specify_kind (d : datetime , kind : DateKind ) -> datetime :
664+ def specify_kind (d : datetime , kind : int32 ) -> datetime :
665665 return create (year (d ), month (d ), day (d ), hour (d ), minute (d ), second (d ), millisecond (d ), microsecond (d ), kind )
666666
667667
668- def ticks (d : datetime ) -> int :
668+ def ticks (d : datetime ) -> int64 :
669669 # Note: It can happens that Ticks differs a little bit from the .NET implementation
670670 # because of some rounding/precision issues in Python
671671 # DateTime(1, 1, 1, 0, 0, 0, 0, 99, DateTimeKind.Utc).Ticks should be 990
@@ -674,20 +674,20 @@ def ticks(d: datetime) -> int:
674674 # - returns -62135596799.9999
675675 # - instead of -62135596800000
676676 # compute timestamp in microseconds
677- return unix_epoch_microseconds_to_ticks (int (d .timestamp () * 1_000_000 ), date_offset (d ) * 1_000 )
677+ return unix_epoch_microseconds_to_ticks (int64 (d .timestamp () * 1_000_000 ), date_offset (d ) * 1_000 )
678678
679679
680- def unix_epoch_microseconds_to_ticks (us : int , offset : int ) -> int :
681- return int ((( us + 62135596800000000 ) + offset ) * 10 )
680+ def unix_epoch_microseconds_to_ticks (us : int64 , offset : int64 ) -> int64 :
681+ return (( us + 62135596800000000 ) + offset ) * 10
682682
683683
684- def ticks_to_unix_epoch_microseconds (ticks : int ) -> int :
685- return int (( ticks - 621355968000000000 ) // 10 )
684+ def ticks_to_unix_epoch_microseconds (ticks : SupportsInt ) -> int :
685+ return ( int (ticks ) - 621355968000000000 ) // 10
686686
687687
688- def date_offset (d : datetime ) -> int :
688+ def date_offset (d : datetime ) -> int64 :
689689 if d .tzinfo == UTC :
690- return 0
690+ return int64 . ZERO
691691 else :
692692 utc_offset = d .utcoffset ()
693693
@@ -696,14 +696,14 @@ def date_offset(d: datetime) -> int:
696696 if utc_offset is None :
697697 forced_utc_offset = d .astimezone ().utcoffset ()
698698 assert forced_utc_offset is not None
699- return int (forced_utc_offset .total_seconds () * 1_000 )
699+ return int64 (forced_utc_offset .total_seconds () * 1_000 )
700700 else :
701- return int (utc_offset .total_seconds () * 1_000 )
701+ return int64 (utc_offset .total_seconds () * 1_000 )
702702
703703 # return 0 if d.tzinfo == timezone.utc else
704704
705705
706- def create_from_epoch_microseconds (us : int , kind : DateKind | None = None ) -> datetime :
706+ def create_from_epoch_microseconds (us : int , kind : int32 | None = None ) -> datetime :
707707 if kind == DateKind .UTC :
708708 date = datetime .fromtimestamp (us / 1_000_000 , UTC )
709709 else :
@@ -714,7 +714,7 @@ def create_from_epoch_microseconds(us: int, kind: DateKind | None = None) -> dat
714714 return date
715715
716716
717- def from_ticks (ticks : int , kind : DateKind | None = None ) -> datetime :
717+ def from_ticks (ticks : SupportsInt , kind : int32 | None = None ) -> datetime :
718718 # Better default than Unspecified
719719 kind = kind or DateKind .Local
720720 date = create_from_epoch_microseconds (ticks_to_unix_epoch_microseconds (ticks ), kind )
0 commit comments