Skip to content

Commit

Permalink
Merge pull request #880 from samuelcolvin/millisecs-in-timestamps
Browse files Browse the repository at this point in the history
add milliseconds into timestamps
  • Loading branch information
selwin committed Sep 21, 2017
2 parents 19bc288 + 260fd84 commit 47ee65e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
11 changes: 7 additions & 4 deletions rq/utils.py
Expand Up @@ -157,16 +157,19 @@ def utcnow():
return datetime.datetime.utcnow()


_TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'


def utcformat(dt):
return dt.strftime(as_text('%Y-%m-%dT%H:%M:%SZ'))
return dt.strftime(as_text(_TIMESTAMP_FORMAT))


def utcparse(string):
try:
return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ')
return datetime.datetime.strptime(string, _TIMESTAMP_FORMAT)
except ValueError:
# This catches RQ < 0.4 datetime format
return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%S.%f+00:00')
# This catches any jobs remain with old datetime format
return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ')


def first(iterable, default=None, key=None):
Expand Down
9 changes: 0 additions & 9 deletions tests/helpers.py

This file was deleted.

10 changes: 3 additions & 7 deletions tests/test_job.py
Expand Up @@ -14,7 +14,6 @@
import queue as queue

from tests import fixtures, RQTestCase
from tests.helpers import strip_microseconds

from rq.compat import PY2
from rq.exceptions import NoSuchJobError, UnpickleError
Expand Down Expand Up @@ -170,7 +169,7 @@ def test_fetch(self):
self.testconn.hset('rq:job:some_id', 'data',
"(S'tests.fixtures.some_calculation'\nN(I3\nI4\nt(dp1\nS'z'\nI2\nstp2\n.")
self.testconn.hset('rq:job:some_id', 'created_at',
'2012-02-07T22:13:24Z')
'2012-02-07T22:13:24.123456Z')

# Fetch returns a job
job = Job.fetch('some_id')
Expand All @@ -179,7 +178,7 @@ def test_fetch(self):
self.assertIsNone(job.instance)
self.assertEqual(job.args, (3, 4))
self.assertEqual(job.kwargs, dict(z=2))
self.assertEqual(job.created_at, datetime(2012, 2, 7, 22, 13, 24))
self.assertEqual(job.created_at, datetime(2012, 2, 7, 22, 13, 24, 123456))

def test_persistence_of_empty_jobs(self): # noqa
"""Storing empty jobs."""
Expand All @@ -192,11 +191,8 @@ def test_persistence_of_typical_jobs(self):
job = Job.create(func=fixtures.some_calculation, args=(3, 4), kwargs=dict(z=2))
job.save()

expected_date = strip_microseconds(job.created_at)
stored_date = self.testconn.hget(job.key, 'created_at').decode('utf-8')
self.assertEqual(
stored_date,
utcformat(expected_date))
self.assertEqual(stored_date, utcformat(job.created_at))

# ... and no other keys are stored
self.assertEqual(
Expand Down
8 changes: 5 additions & 3 deletions tests/test_utils.py
Expand Up @@ -51,11 +51,13 @@ def test_ensure_list(self):

def test_utcparse(self):
"""Ensure function utcparse works correctly"""
utc_formated_time = '2017-08-31T10:14:02Z'
utc_compat_formated_time = '2017-08-31T10:20:56.226733+00:00'
utc_formated_time = '2017-08-31T10:14:02.123456Z'
self.assertEqual(datetime.datetime(2017, 8, 31, 10, 14, 2, 123456), utcparse(utc_formated_time))

def test_utcparse_legacy(self):
"""Ensure function utcparse works correctly"""
utc_formated_time = '2017-08-31T10:14:02Z'
self.assertEqual(datetime.datetime(2017, 8, 31, 10, 14, 2), utcparse(utc_formated_time))
self.assertEqual(datetime.datetime(2017, 8, 31, 10, 20, 56, 226733), utcparse(utc_compat_formated_time))

def test_backend_class(self):
"""Ensure function backend_class works correctly"""
Expand Down
5 changes: 2 additions & 3 deletions tests/test_worker.py
Expand Up @@ -22,7 +22,6 @@
div_by_zero, do_nothing, say_hello, say_pid,
run_dummy_heroku_worker, access_self,
modify_self, modify_self_and_error)
from tests.helpers import strip_microseconds

from rq import (get_failed_queue, Queue, SimpleWorker, Worker,
get_current_connection)
Expand Down Expand Up @@ -212,7 +211,7 @@ def test_work_fails(self):
self.assertEqual(q.count, 1)

# keep for later
enqueued_at_date = strip_microseconds(job.enqueued_at)
enqueued_at_date = str(job.enqueued_at)

w = Worker([q])
w.work(burst=True) # should silently pass
Expand All @@ -228,7 +227,7 @@ def test_work_fails(self):

# Should be the original enqueued_at date, not the date of enqueueing
# to the failed queue
self.assertEqual(job.enqueued_at, enqueued_at_date)
self.assertEqual(str(job.enqueued_at), enqueued_at_date)
self.assertIsNotNone(job.exc_info) # should contain exc_info

def test_custom_exc_handling(self):
Expand Down

0 comments on commit 47ee65e

Please sign in to comment.