@@ -1374,48 +1374,42 @@ def tzinfo(self):
13741374 return self ._tzinfo
13751375
13761376 @classmethod
1377- def fromtimestamp (cls , t , tz = None ):
1377+ def _fromtimestamp (cls , t , utc , tz ):
13781378 """Construct a datetime from a POSIX timestamp (like time.time()).
13791379
13801380 A timezone info object may be passed in as well.
13811381 """
1382- _check_tzinfo_arg (tz )
1383-
1384- converter = _time .localtime if tz is None else _time .gmtime
1385-
1386- t , frac = divmod (t , 1.0 )
1382+ frac , t = _math .modf (t )
13871383 us = _round_half_up (frac * 1e6 )
1388-
1389- # If timestamp is less than one microsecond smaller than a
1390- # full second, us can be rounded up to 1000000. In this case,
1391- # roll over to seconds, otherwise, ValueError is raised
1392- # by the constructor.
1393- if us == 1000000 :
1384+ if us >= 1000000 :
13941385 t += 1
1395- us = 0
1386+ us -= 1000000
1387+ elif us < 0 :
1388+ t -= 1
1389+ us += 1000000
1390+
1391+ converter = _time .gmtime if utc else _time .localtime
13961392 y , m , d , hh , mm , ss , weekday , jday , dst = converter (t )
13971393 ss = min (ss , 59 ) # clamp out leap seconds if the platform has them
1398- result = cls (y , m , d , hh , mm , ss , us , tz )
1394+ return cls (y , m , d , hh , mm , ss , us , tz )
1395+
1396+ @classmethod
1397+ def fromtimestamp (cls , t , tz = None ):
1398+ """Construct a datetime from a POSIX timestamp (like time.time()).
1399+
1400+ A timezone info object may be passed in as well.
1401+ """
1402+ _check_tzinfo_arg (tz )
1403+
1404+ result = cls ._fromtimestamp (t , tz is not None , tz )
13991405 if tz is not None :
14001406 result = tz .fromutc (result )
14011407 return result
14021408
14031409 @classmethod
14041410 def utcfromtimestamp (cls , t ):
14051411 """Construct a naive UTC datetime from a POSIX timestamp."""
1406- t , frac = divmod (t , 1.0 )
1407- us = _round_half_up (frac * 1e6 )
1408-
1409- # If timestamp is less than one microsecond smaller than a
1410- # full second, us can be rounded up to 1000000. In this case,
1411- # roll over to seconds, otherwise, ValueError is raised
1412- # by the constructor.
1413- if us == 1000000 :
1414- t += 1
1415- us = 0
1416- y , m , d , hh , mm , ss , weekday , jday , dst = _time .gmtime (t )
1417- ss = min (ss , 59 ) # clamp out leap seconds if the platform has them
1418- return cls (y , m , d , hh , mm , ss , us )
1412+ return cls ._fromtimestamp (t , True , None )
14191413
14201414 @classmethod
14211415 def now (cls , tz = None ):
0 commit comments