Skip to content

Commit 990c01e

Browse files
Merge pull request pandas-dev#161 from mckelvin/tickstore
TickStore.max_date: convert to local timezone
2 parents 9ea03a7 + 378170c commit 990c01e

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/build
2+
*.eggs/
23
*.pyc
34
/src/_compress.c
45
*.egg
@@ -13,3 +14,5 @@ htmlcov
1314
coverage.xml
1415
junit.xml
1516
/tmp/
17+
18+
*.sw[op]

arctic/date/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from ._daterange import DateRange
22
from ._generalslice import OPEN_CLOSED, CLOSED_OPEN, OPEN_OPEN, CLOSED_CLOSED
33
from ._util import datetime_to_ms, ms_to_datetime
4-
from ._util import string_to_daterange, to_pandas_closed_closed, to_dt
4+
from ._util import string_to_daterange, to_pandas_closed_closed, to_dt, utc_dt_to_local_dt
55
from ._mktz import mktz, TimezoneError

arctic/date/_util.py

+15
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,18 @@ def datetime_to_ms(d):
166166
return long((calendar.timegm(_add_tzone(d).utctimetuple()) + d.microsecond / 1000000.0) * 1e3)
167167
except AttributeError:
168168
raise TypeError('expect Python datetime object, not %s' % type(d))
169+
170+
171+
def utc_dt_to_local_dt(dtm):
172+
"""Convert a UTC datetime to datetime in local timezone"""
173+
utc_zone = mktz("UTC")
174+
if dtm.tzinfo is not None and dtm.tzinfo != utc_zone:
175+
raise ValueError(
176+
"Expected dtm without tzinfo or with UTC, not %r" % (
177+
dtm.tzinfo
178+
)
179+
)
180+
181+
if dtm.tzinfo is None:
182+
dtm = dtm.replace(tzinfo=utc_zone)
183+
return dtm.astimezone(mktz())

arctic/tickstore/tickstore.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pymongo.errors import OperationFailure
1313
from six import iteritems, string_types
1414

15-
from ..date import DateRange, to_pandas_closed_closed, mktz, datetime_to_ms, ms_to_datetime, CLOSED_CLOSED, to_dt
15+
from ..date import DateRange, to_pandas_closed_closed, mktz, datetime_to_ms, ms_to_datetime, CLOSED_CLOSED, to_dt, utc_dt_to_local_dt
1616
from ..decorators import mongo_retry
1717
from ..exceptions import OverlappingDataException, NoDataFoundException, UnorderedDataException, UnhandledDtypeException, ArcticException
1818
from .._util import indent
@@ -718,4 +718,4 @@ def max_date(self, symbol):
718718
"""
719719
res = self._collection.find_one({SYMBOL: symbol}, projection={ID: 0, END: 1},
720720
sort=[(START, pymongo.DESCENDING)])
721-
return res[END]
721+
return utc_dt_to_local_dt(res[END])

tests/unit/date/test_util.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pytest
22
import pytz
3+
from mock import patch
34

45
from datetime import datetime as dt
56
from arctic.date import datetime_to_ms, ms_to_datetime, mktz, to_pandas_closed_closed, DateRange, OPEN_OPEN, CLOSED_CLOSED
6-
from arctic.date._util import to_dt
7+
from arctic.date._util import to_dt, utc_dt_to_local_dt
78

89

910
@pytest.mark.parametrize('pdt', [
@@ -105,3 +106,15 @@ def test_daterange_lt():
105106
assert(dr2 < dr)
106107
dr.start = None
107108
assert((dr2 < dr) == False)
109+
110+
111+
@patch("arctic.date._util.mktz", lambda zone="Asia/Shanghai": mktz(zone))
112+
def test_utc_dt_to_local_dt():
113+
with pytest.raises(ValueError):
114+
assert(utc_dt_to_local_dt(
115+
dt(2000, 1, 1, 0, 0, 0, tzinfo=mktz("Asia/Shanghai"))
116+
))
117+
118+
utc_time = dt(2000, 1, 1, 10, 0, 0)
119+
pek_time = utc_dt_to_local_dt(utc_time) # GMT +0800
120+
assert(pek_time.hour - utc_time.hour == 8)

0 commit comments

Comments
 (0)