Skip to content

Commit

Permalink
Fix oppia#9565: Pass .timetuple() to time.mktime() in utils.get_time_…
Browse files Browse the repository at this point in the history
…in_millisecs (oppia#9560)

* fix get_time_in_milliseconds

* fix lint

* simplify logic

* simplify fix

* consistent literals
  • Loading branch information
brianrodri authored Jun 20, 2020
1 parent 0b9dafb commit 0565ec6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ def get_time_in_millisecs(datetime_obj):
Returns:
float. The time in milliseconds since the Epoch.
"""
seconds = time.mktime(datetime_obj.utctimetuple()) * 1000
return seconds + python_utils.divide(datetime_obj.microsecond, 1000.0)
msecs = time.mktime(datetime_obj.timetuple()) * 1000.0
return msecs + python_utils.divide(datetime_obj.microsecond, 1000.0)


def get_current_time_in_millisecs():
Expand Down
14 changes: 14 additions & 0 deletions utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,17 @@ def test_require_valid_thumbnail_filename(self):
'Expected a filename ending in svg, received name.jpg', 'name.jpg')
filename = 'filename.svg'
utils.require_valid_thumbnail_filename(filename)

def test_get_time_in_millisecs(self):
dt = datetime.datetime(2020, 6, 15)
msecs = utils.get_time_in_millisecs(dt)
self.assertEqual(
dt,
datetime.datetime.fromtimestamp(python_utils.divide(msecs, 1000.0)))

def test_get_time_in_millisecs_with_complicated_time(self):
dt = datetime.datetime(2020, 6, 15, 5, 18, 23, microsecond=123456)
msecs = utils.get_time_in_millisecs(dt)
self.assertEqual(
dt,
datetime.datetime.fromtimestamp(python_utils.divide(msecs, 1000.0)))

0 comments on commit 0565ec6

Please sign in to comment.