@@ -197,7 +197,13 @@ def _get_rc_timezone():
197197Time-related constants.
198198"""
199199EPOCH_OFFSET = float (datetime .datetime (1970 , 1 , 1 ).toordinal ())
200- JULIAN_OFFSET = 1721424.5 # Julian date at 0001-01-01
200+ # EPOCH_OFFSET is not used by matplotlib
201+ JULIAN_OFFSET = 1721424.5 # Julian date at 0000-12-31
202+ # note that the Julian day epoch is achievable w/
203+ # np.datetime64('-4713-11-24T12:00:00'); datetime64 is proleptic
204+ # Gregorian and BC has a one-year offset. So
205+ # np.datetime64('0000-12-31') - np.datetime64('-4713-11-24T12:00') = 1721424.5
206+ # Ref: https://en.wikipedia.org/wiki/Julian_day
201207MICROSECONDLY = SECONDLY + 1
202208HOURS_PER_DAY = 24.
203209MIN_PER_HOUR = 60.
@@ -445,14 +451,20 @@ def julian2num(j):
445451 Parameters
446452 ----------
447453 j : float or sequence of floats
448- Julian date(s)
454+ Julian dates (days relative to 4713 BC Jan 1, 12:00:00 Julian
455+ calendar or 4714 BC Nov 24, 12:00:00, proleptic Gregorian calendar).
449456
450457 Returns
451458 -------
452459 float or sequence of floats
453- Matplotlib date(s)
460+ Matplotlib dates (days relative to `.get_epoch`).
454461 """
455- return np .subtract (j , JULIAN_OFFSET ) # Handles both scalar & nonscalar j.
462+ ep = np .datetime64 (get_epoch (), 'h' ).astype (float ) / 24.
463+ ep0 = np .datetime64 ('0000-12-31T00:00:00' , 'h' ).astype (float ) / 24.
464+ # Julian offset defined above is relative to 0000-12-31, but we need
465+ # relative to our current epoch:
466+ dt = JULIAN_OFFSET - ep0 + ep
467+ return np .subtract (j , dt ) # Handles both scalar & nonscalar j.
456468
457469
458470def num2julian (n ):
@@ -462,14 +474,19 @@ def num2julian(n):
462474 Parameters
463475 ----------
464476 n : float or sequence of floats
465- Matplotlib date(s)
477+ Matplotlib dates (days relative to `.get_epoch`).
466478
467479 Returns
468480 -------
469481 float or sequence of floats
470- Julian date(s)
482+ Julian dates (days relative to 4713 BC Jan 1, 12:00:00).
471483 """
472- return np .add (n , JULIAN_OFFSET ) # Handles both scalar & nonscalar j.
484+ ep = np .datetime64 (get_epoch (), 'h' ).astype (float ) / 24.
485+ ep0 = np .datetime64 ('0000-12-31T00:00:00' , 'h' ).astype (float ) / 24.
486+ # Julian offset defined above is relative to 0000-12-31, but we need
487+ # relative to our current epoch:
488+ dt = JULIAN_OFFSET - ep0 + ep
489+ return np .add (n , dt ) # Handles both scalar & nonscalar j.
473490
474491
475492def num2date (x , tz = None ):
0 commit comments