@@ -1487,6 +1487,43 @@ def iso_calendar(self) -> tuple[int, int, int]: ...
1487
1487
time_base_class = object
1488
1488
1489
1489
1490
+ def _dst (
1491
+ tz : _tzinfo | None = None , dt : DateTime | None = None
1492
+ ) -> timedelta | None :
1493
+ if tz is None :
1494
+ return None
1495
+ try :
1496
+ value = tz .dst (dt )
1497
+ except TypeError :
1498
+ if dt is None :
1499
+ raise
1500
+ # For timezone implementations not compatible with the custom
1501
+ # datetime implementations, we can't do better than this.
1502
+ value = tz .dst (dt .to_native ()) # type: ignore
1503
+ if value is None :
1504
+ return None
1505
+ if isinstance (value , timedelta ):
1506
+ if value .days != 0 :
1507
+ raise ValueError ("dst must be less than a day" )
1508
+ if value .seconds % 60 != 0 or value .microseconds != 0 :
1509
+ raise ValueError ("dst must be a whole number of minutes" )
1510
+ return value
1511
+ raise TypeError ("dst must be a timedelta" )
1512
+
1513
+
1514
+ def _tz_name (tz : _tzinfo | None , dt : DateTime | None ) -> str | None :
1515
+ if tz is None :
1516
+ return None
1517
+ try :
1518
+ return tz .tzname (dt )
1519
+ except TypeError :
1520
+ if dt is None :
1521
+ raise
1522
+ # For timezone implementations not compatible with the custom
1523
+ # datetime implementations, we can't do better than this.
1524
+ return tz .tzname (dt .to_native ())
1525
+
1526
+
1490
1527
class Time (time_base_class , metaclass = TimeType ):
1491
1528
"""
1492
1529
Time of day.
@@ -1996,23 +2033,7 @@ def dst(self) -> timedelta | None:
1996
2033
:raises TypeError: if `self.tzinfo.dst(self)` does return anything but
1997
2034
None or a :class:`datetime.timedelta`.
1998
2035
"""
1999
- if self .tzinfo is None :
2000
- return None
2001
- try :
2002
- value = self .tzinfo .dst (self ) # type: ignore
2003
- except TypeError :
2004
- # For timezone implementations not compatible with the custom
2005
- # datetime implementations, we can't do better than this.
2006
- value = self .tzinfo .dst (self .to_native ()) # type: ignore
2007
- if value is None :
2008
- return None
2009
- if isinstance (value , timedelta ):
2010
- if value .days != 0 :
2011
- raise ValueError ("dst must be less than a day" )
2012
- if value .seconds % 60 != 0 or value .microseconds != 0 :
2013
- raise ValueError ("dst must be a whole number of minutes" )
2014
- return value
2015
- raise TypeError ("dst must be a timedelta" )
2036
+ return _dst (self .tzinfo , None )
2016
2037
2017
2038
def tzname (self ) -> str | None :
2018
2039
"""
@@ -2021,14 +2042,7 @@ def tzname(self) -> str | None:
2021
2042
:returns: None if the time is local (i.e., has no timezone), else
2022
2043
return `self.tzinfo.tzname(self)`
2023
2044
"""
2024
- if self .tzinfo is None :
2025
- return None
2026
- try :
2027
- return self .tzinfo .tzname (self ) # type: ignore
2028
- except TypeError :
2029
- # For timezone implementations not compatible with the custom
2030
- # datetime implementations, we can't do better than this.
2031
- return self .tzinfo .tzname (self .to_native ()) # type: ignore
2045
+ return _tz_name (self .tzinfo , None )
2032
2046
2033
2047
def to_clock_time (self ) -> ClockTime :
2034
2048
"""Convert to :class:`.ClockTime`."""
@@ -2202,16 +2216,14 @@ def now(cls, tz: _tzinfo | None = None) -> DateTime:
2202
2216
if tz is None :
2203
2217
return cls .from_clock_time (Clock ().local_time (), UnixEpoch )
2204
2218
else :
2219
+ utc_now = cls .from_clock_time (
2220
+ Clock ().utc_time (), UnixEpoch
2221
+ ).replace (tzinfo = tz )
2205
2222
try :
2206
- return tz .fromutc ( # type: ignore
2207
- cls .from_clock_time ( # type: ignore
2208
- Clock ().utc_time (), UnixEpoch
2209
- ).replace (tzinfo = tz )
2210
- )
2223
+ return tz .fromutc (utc_now ) # type: ignore
2211
2224
except TypeError :
2212
2225
# For timezone implementations not compatible with the custom
2213
2226
# datetime implementations, we can't do better than this.
2214
- utc_now = cls .from_clock_time (Clock ().utc_time (), UnixEpoch )
2215
2227
utc_now_native = utc_now .to_native ()
2216
2228
now_native = tz .fromutc (utc_now_native )
2217
2229
now = cls .from_native (now_native )
@@ -2809,15 +2821,15 @@ def dst(self) -> timedelta | None:
2809
2821
2810
2822
See :meth:`.Time.dst`.
2811
2823
"""
2812
- return self .__time . dst ( )
2824
+ return _dst ( self .tzinfo , self )
2813
2825
2814
2826
def tzname (self ) -> str | None :
2815
2827
"""
2816
2828
Get the timezone name.
2817
2829
2818
2830
See :meth:`.Time.tzname`.
2819
2831
"""
2820
- return self .__time . tzname ( )
2832
+ return _tz_name ( self .tzinfo , self )
2821
2833
2822
2834
def time_tuple (self ):
2823
2835
raise NotImplementedError
0 commit comments