Fix #409 ts.utc() would die if given a date object

The routine would die because `combine()` is a class method, not a
module method; and because it needed a timezone.  A test is here added
to keep the feature fixed.  Also, the timelib module used `dt` for the
`datetime` module but also sometimes as a convenient local name for a
`datetime`, so let’s rename the module to `dt_module` for readability
and consistency.  Inspired by the attempted fix in #410.
This commit is contained in:
Brandon Rhodes 2020-07-22 08:46:49 -04:00
parent 0af8e01b4a
commit a8e66742b4
2 changed files with 13 additions and 7 deletions

View File

@ -1,3 +1,4 @@
import datetime as dt_module
import numpy as np
from assay import assert_raises
from pytz import timezone
@ -102,6 +103,11 @@ def test_building_time_from_list_of_utc_datetimes(ts):
2442046.5, 2442047.5, 2442048.5, 2442049.5, 2442050.5, 2442051.5,
]
def test_building_time_from_python_date(ts):
d = dt_module.date(2020, 7, 22)
t = ts.utc(d)
assert t.utc == (2020, 7, 22, 0, 0, 0.0)
def test_converting_ut1_to_tt(ts):
ten_thousand_years = 365 * 10000

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import datetime as dt
import datetime as dt_module
import re
from collections import namedtuple
from datetime import date, datetime
@ -33,8 +33,8 @@ class CalendarArray(ndarray):
@property
def second(self): return self[5]
if hasattr(dt, 'timezone'):
utc = dt.timezone.utc
if hasattr(dt_module, 'timezone'):
utc = dt_module.timezone.utc
else:
try:
from pytz import utc
@ -42,9 +42,9 @@ else:
# Lacking a full suite of timezones from pytz, we at least need a
# time zone object for UTC.
class UTC(dt.tzinfo):
class UTC(dt_module.tzinfo):
'UTC'
zero = dt.timedelta(0)
zero = dt_module.timedelta(0)
def utcoffset(self, dt):
return self.zero
def tzname(self, dt):
@ -56,7 +56,7 @@ else:
# Much of the following code is adapted from the USNO's "novas.c".
_time_zero = dt.time()
_time_zero = dt_module.time(tzinfo=utc)
_half_minute = 30.0 / DAY_S
_half_second = 0.5 / DAY_S
_half_microsecond = 0.5e-6 / DAY_S
@ -150,7 +150,7 @@ class Timescale(object):
if isinstance(year, datetime):
return self.from_datetime(year)
if isinstance(year, date):
return self.from_datetime(dt.combine(year, _time_zero))
return self.from_datetime(datetime.combine(year, _time_zero))
if hasattr(year, '__len__') and isinstance(year[0], datetime):
return self.from_datetimes(year)