Skip to content

Commit

Permalink
Bugfix: calendar -> JD broken for arrays of JD
Browse files Browse the repository at this point in the history
If a Julian calendar cutoff was specified, arrays of dates were failing
to turn into JDs but raising an exception instead.  See #450.
  • Loading branch information
brandon-rhodes committed Sep 25, 2020
1 parent ae1a052 commit 4c91a52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 12 additions & 0 deletions skyfield/tests/test_timelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,23 @@ def test_raw_julian_gregorian_cutover():
assert compute_calendar_date(gregory + 0, gregory) == (1582, 10, 15)
assert compute_calendar_date(gregory + 1, gregory) == (1582, 10, 16)

jd = np.arange(gregory - 2, gregory + 2)
assert [list(a) for a in compute_calendar_date(jd, gregory)] == [
[1582, 1582, 1582, 1582],
[10, 10, 10, 10],
[3, 4, 15, 16],
]

assert julian_day(1582, 10, 3, gregory) == (gregory - 2)
assert julian_day(1582, 10, 4, gregory) == (gregory - 1)
assert julian_day(1582, 10, 15, gregory) == (gregory + 0)
assert julian_day(1582, 10, 16, gregory) == (gregory + 1)

days = [3, 4, 15, 16]
assert list(julian_day(1582, 10, np.array(days), gregory)) == [
2299159, 2299160, 2299161, 2299162,
]

def test_constructor_julian_gregorian_cutover(ts, time_scale_name):
if sys.version_info <= (3,):
return # Python 2 time.strftime() complains about the year 1582
Expand Down
6 changes: 3 additions & 3 deletions skyfield/timelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,9 @@ def julian_day(year, month=1, day=1, julian_before=None):
f = (month + 9) % 12
e = 1461 * g // 4 + day - 1402
J = e + (153 * f + 2) // 5
if (julian_before is None) or (J >= julian_before):
J += 38
J -= (g + 184) // 100 * 3 // 4

mask = 1 if (julian_before is None) else (J >= julian_before)
J += (38 - (g + 184) // 100 * 3 // 4) * mask
return J

def julian_date(year, month=1, day=1, hour=0, minute=0, second=0.0):
Expand Down

0 comments on commit 4c91a52

Please sign in to comment.