Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 5 Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
minutes = int(z[3:5])
seconds = int(z[5:7] or 0)
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
gmtoff_fraction = int(z[8:] or 0)
gmtoff_remainder = z[8:]
# Pad to always return microseconds.
gmtoff_remainder_padding = "0" * (6 - len(gmtoff_remainder))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a microbenchmark for this vs. multiplying by 10**(6-len(gmtoff_remainder)) and it seems like your version is either faster or the same speed. Maybe that's why you chose this method, but I thought I'd chime in since I went through the trouble to check it.

gmtoff_fraction = int(gmtoff_remainder + gmtoff_remainder_padding)
if z.startswith("-"):
gmtoff = -gmtoff
gmtoff_fraction = -gmtoff_fraction
Expand Down
3 changes: 3 additions & 0 deletions 3 Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ def test_offset(self):
(*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30.000001", "%z")
self.assertEqual(offset, -(one_hour + half_hour + half_minute))
self.assertEqual(offset_fraction, -1)
(*_, offset), _, offset_fraction = _strptime._strptime("+01:30:30.001", "%z")
self.assertEqual(offset, one_hour + half_hour + half_minute)
self.assertEqual(offset_fraction, 1000)
(*_, offset), _, offset_fraction = _strptime._strptime("Z", "%z")
self.assertEqual(offset, 0)
self.assertEqual(offset_fraction, 0)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.