Skip to content

Commit

Permalink
handle datetime.datetime vs datetime.date correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Feb 27, 2018
1 parent 490fadc commit e8bef75
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 7 additions & 0 deletions sciencedates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,14 @@ def datetime2yeardec(t):
time distances should be preserved: If bdate-adate=ddate-cdate then
dt2t(bdate)-dt2t(adate) = dt2t(ddate)-dt2t(cdate)
"""

if isinstance(t,str):
t = parse(t)
elif isinstance(t, datetime.datetime):
pass
elif isinstance(t, datetime.date):
t = datetime.datetime.combine(t, datetime.time.min)


t = forceutc(t)

Expand All @@ -179,6 +185,7 @@ def datetime2yeardec(t):
year = t.year
boy = datetime.datetime(year, 1, 1,tzinfo=UTC)
eoy = datetime.datetime(year + 1, 1, 1, tzinfo=UTC)

return year + ((t - boy).total_seconds() / ((eoy - boy).total_seconds()))


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(name='sciencedates',
packages=find_packages(),
version = '1.3.1',
version = '1.3.2',
description='Date conversions used in the sciences.',
long_description=open('README.rst').read(),
author = 'Michael Hirsch, Ph.D.',
Expand Down
12 changes: 6 additions & 6 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def test_yearint(self):
assert utsec==utsec2

def test_yeardec(self):
adatetime = datetime.datetime(2013,7,2,12,0,0,tzinfo=UTC)
yeardec = sd.datetime2yeardec(adatetime)
t0 = datetime.datetime(2013,7,2,12,0,0,tzinfo=UTC)
yeardec = sd.datetime2yeardec(t0)

assert_allclose(yeardec,2013.5)
assert sd.yeardec2datetime(yeardec) == adatetime
assert_allclose(yeardec, 2013.5)
assert sd.yeardec2datetime(yeardec) == t0

def test_utc(self):
t0 = datetime.datetime(2013,7,2,12,0,0)
Expand All @@ -36,8 +36,8 @@ def test_utc(self):
assert sd.forceutc(d0) == d0

def test_gtd(self):
adatetime = datetime.datetime(2013,7,2,12,0,0)
iyd,utsec,stl= sd.datetime2gtd(adatetime,glon=42)
t0 = datetime.datetime(2013,7,2,12,0,0)
iyd,utsec,stl= sd.datetime2gtd(t0,glon=42)

assert iyd[0]==183
assert_allclose(utsec[0],43200)
Expand Down

0 comments on commit e8bef75

Please sign in to comment.