Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3e62f78c
Commit
3e62f78c
authored
Sep 21, 2010
by
Alexander Belopolsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed microsecond rounding in python version of utcfromtimestamp
parent
b3bfc3d8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
9 deletions
+14
-9
Lib/datetime.py
Lib/datetime.py
+10
-5
Lib/test/datetimetester.py
Lib/test/datetimetester.py
+4
-4
No files found.
Lib/datetime.py
View file @
3e62f78c
...
...
@@ -1379,12 +1379,17 @@ class datetime(date):
@
classmethod
def
utcfromtimestamp
(
cls
,
t
):
"Construct a UTC datetime from a POSIX timestamp (like time.time())."
if
1
-
(
t
%
1.0
)
<
0.000001
:
t
=
float
(
int
(
t
))
+
1
if
t
<
0
:
t
-=
1
t
,
frac
=
divmod
(
t
,
1.0
)
us
=
round
(
frac
*
1e6
)
# If timestamp is less than one microsecond smaller than a
# full second, us can be rounded up to 1000000. In this case,
# roll over to seconds, otherwise, ValueError is raised
# by the constructor.
if
us
==
1000000
:
t
+=
1
us
=
0
y
,
m
,
d
,
hh
,
mm
,
ss
,
weekday
,
jday
,
dst
=
_time
.
gmtime
(
t
)
us
=
int
((
t
%
1.0
)
*
1000000
)
ss
=
min
(
ss
,
59
)
# clamp out leap seconds if the platform has them
return
cls
(
y
,
m
,
d
,
hh
,
mm
,
ss
,
us
)
...
...
Lib/test/datetimetester.py
View file @
3e62f78c
...
...
@@ -1729,10 +1729,10 @@ class TestDateTime(TestDate):
def
test_microsecond_rounding
(
self
):
# Test whether fromtimestamp "rounds up" floats that are less
# than 1/2 microsecond smaller than an integer.
self
.
assertEqual
(
self
.
theclass
.
fromtimestamp
(
0.9999999
)
,
self
.
theclass
.
fromtimestamp
(
1
))
self
.
assertEqual
(
self
.
theclass
.
fromtimestamp
(
0.99999949
).
microsecond
,
999999
)
for
fts
in
[
self
.
theclass
.
fromtimestamp
,
self
.
theclass
.
utcfromtimestamp
]:
self
.
assertEqual
(
fts
(
0.9999999
),
fts
(
1
))
self
.
assertEqual
(
fts
(
0.99999949
).
microsecond
,
999999
)
def
test_insane_fromtimestamp
(
self
):
# It's possible that some platform maps time_t to double,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment