add milliseconds into timestamps, fix #721

main
Samuel Colvin 7 years ago
parent 19bc288378
commit 260fd84f51

@ -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):

@ -1,9 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from datetime import timedelta
def strip_microseconds(date):
return date - timedelta(microseconds=date.microsecond)

@ -14,7 +14,6 @@ else:
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
@ -170,7 +169,7 @@ class TestJob(RQTestCase):
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')
@ -179,7 +178,7 @@ class TestJob(RQTestCase):
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."""
@ -192,11 +191,8 @@ class TestJob(RQTestCase):
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(

@ -51,11 +51,13 @@ class TestUtils(RQTestCase):
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"""

@ -22,7 +22,6 @@ from tests.fixtures import (create_file, create_file_after_timeout,
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)
@ -212,7 +211,7 @@ class TestWorker(RQTestCase):
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
@ -228,7 +227,7 @@ class TestWorker(RQTestCase):
# 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):

Loading…
Cancel
Save