Skip to content

Commit 85c6c7a

Browse files
committed
switch to_unix_ms, add to_unix_ms_tz
1 parent 5cc3bf5 commit 85c6c7a

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

HISTORY.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
History
44
-------
55

6-
v0.9.0 (2017-XX-XX)
6+
v0.9.0 (2017-06-23)
77
...................
88
* allow set encoding in msgpack for jobs #49
99
* cron tasks allowing scheduling of functions in the future #50
10+
* **Breaking change:** switch ``to_unix_ms`` to just return the timestamp int, add ``to_unix_ms_tz`` to
11+
return tz offset too
1012

1113
v0.8.1 (2017-06-05)
1214
...................

arq/jobs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import msgpack
1010

11-
from .utils import DEFAULT_CURTAIL, ellipsis, from_unix_ms, timestamp, to_unix_ms
11+
from .utils import DEFAULT_CURTAIL, ellipsis, from_unix_ms, timestamp, to_unix_ms_tz
1212

1313
__all__ = ['JobSerialisationError', 'Job', 'DatetimeJob']
1414

@@ -122,7 +122,7 @@ class DatetimeJob(Job):
122122
@classmethod
123123
def msgpack_encoder(cls, obj):
124124
if isinstance(obj, datetime):
125-
ts, tz = to_unix_ms(obj)
125+
ts, tz = to_unix_ms_tz(obj)
126126
result = {DEVICE_CONTROL_TWO: ts}
127127
if tz is not None:
128128
result[TIMEZONE] = tz

arq/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def run_cron(self):
158158
for cron_job, run_at in to_run:
159159
if cron_job.unique:
160160
sentinel_key = self.CRON_SENTINEL_PREFIX + f'{self.name}.{cron_job.__name__}'.encode()
161-
sentinel_value = str(to_unix_ms(run_at)[0]).encode()
161+
sentinel_value = str(to_unix_ms(run_at)).encode()
162162
v, _ = await asyncio.gather(
163163
redis.getset(sentinel_key, sentinel_value),
164164
redis.expire(sentinel_key, 3600),

arq/utils.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ def timestamp() -> float:
135135
return (datetime.utcnow() - EPOCH).total_seconds()
136136

137137

138-
def to_unix_ms(dt: datetime) -> Tuple[int, Union[int, None]]:
138+
def to_unix_ms_tz(dt: datetime) -> Tuple[int, Union[int, None]]:
139139
"""
140-
convert a datetime to number of milliseconds since 1970
140+
convert a datetime to number of milliseconds since 1970 and calculate timezone offset
141141
:param dt: datetime to evaluate
142142
:return: tuple - (unix time in milliseconds, utc offset in seconds)
143143
"""
@@ -150,6 +150,15 @@ def to_unix_ms(dt: datetime) -> Tuple[int, Union[int, None]]:
150150
return int((dt - EPOCH).total_seconds() * 1000), None
151151

152152

153+
def to_unix_ms(dt: datetime) -> int:
154+
"""
155+
convert a datetime to number of milliseconds since 1970
156+
:param dt: datetime to evaluate
157+
:return: unix time in milliseconds
158+
"""
159+
return to_unix_ms_tz(dt)[0]
160+
161+
153162
def from_unix_ms(ms: int, utcoffset: int=None) -> datetime:
154163
"""
155164
convert int to a datetime.

arq/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__all__ = ['VERSION']
44

5-
VERSION = StrictVersion('0.8.1')
5+
VERSION = StrictVersion('0.9.0')

docs/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ API Reference
106106
:members:
107107

108108
.. automodule:: arq.utils
109-
:members: RedisSettings, RedisMixin, create_tz, timestamp, timestamp, to_unix_ms, from_unix_ms, gen_random, ellipsis
109+
:members: RedisSettings, RedisMixin, create_tz, timestamp, timestamp, to_unix_ms_tz, to_unix_ms, from_unix_ms, gen_random, ellipsis
110110

111111
.. automodule:: arq.testing
112112
:members:

tests/test_cron.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def test_cron_time_match_sentinel_set(tmpworkdir, redis_conn, actor):
4545
with open('datatime.pkl', 'wb') as f:
4646
pickle.dump(datetimes, f)
4747

48-
v = str(to_unix_ms(datetime(2032, 1, 1, 3, 0, 0, 123456))[0]).encode()
48+
v = str(to_unix_ms(datetime(2032, 1, 1, 3, 0, 0, 123456))).encode()
4949
await redis_conn.set(b'arq:cron:CronActor.save_spam', v)
5050

5151
worker = CronWorker(burst=True, loop=actor.loop)
@@ -59,7 +59,7 @@ async def test_cron_time_match_not_unique(tmpworkdir, redis_conn, actor):
5959
with open('datatime.pkl', 'wb') as f:
6060
pickle.dump(datetimes, f)
6161

62-
v = str(to_unix_ms(datetime(2032, 1, 1, 3, 0, 0, 123456))[0]).encode()
62+
v = str(to_unix_ms(datetime(2032, 1, 1, 3, 0, 0, 123456))).encode()
6363
await redis_conn.set(b'arq:cron:CronActor.save_not_unique', v)
6464

6565
worker = CronWorker(burst=True, loop=actor.loop)

0 commit comments

Comments
 (0)