Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pickling(self):
"Test reversibility of pickling."
import pickle
date = Datetime360Day(year=1, month=2, day=3, hour=4, minute=5, second=6, microsecond=7)
self.assertEqual(date, pickle.loads(pickle.dumps(date)))
def days_per_month_leap_year(date_type, month):
if date_type is Datetime360Day:
return [-1, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30][month]
if date_type in [DatetimeGregorian, DatetimeProlepticGregorian,
DatetimeJulian, DatetimeAllLeap]:
return [-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
else:
return [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
d1 = DatetimeProlepticGregorian(1582, 10, 4, 12)
d2 = real_datetime(1582,10,4,12)
assert (d1.dayofwk == d2.dayofwk == 0)
assert (d1.dayofyr == d2.dayofyr == 277)
# issue 71: negative reference years
# https://coastwatch.pfeg.noaa.gov/erddap/convert/time.html
# gives 2446433 (365 days more - is it counting year 0?)
# however http://aa.usno.navy.mil/data/docs/JulianDate.php gives
# 2446068, which agrees with us
units = "days since -4713-01-01T00:00:00Z"
t = date2num(datetime(1985,1,2), units, calendar="standard")
assert_almost_equal(t, 2446068)
# issue #68: allow months since for 360_day calendar
d = num2date(1, 'months since 0000-01-01 00:00:00', calendar='360_day')
self.assertEqual(d, Datetime360Day(0,2,1))
t = date2num(d, 'months since 0000-01-01 00:00:00', calendar='360_day')
self.assertEqual(t, 1)
# check that exception is raised if 'months since' used with
# anything but the 360_day calendar.
self.assertRaises(ValueError, num2date, \
1, 'months since 01-01-01',calendar='standard')
self.assertRaises(ValueError, utime, \
'months since 01-01-01', calendar='standard')
# issue #78 - extra digits due to roundoff
assert(cftime.date2num(cftime.datetime(1, 12, 1, 0, 0, 0, 0, -1, 1), units='days since 01-01-01',calendar='noleap') == 334.0)
assert(cftime.date2num(cftime.num2date(1.0,units='days since 01-01-01',calendar='noleap'),units='days since 01-01-01',calendar='noleap') == 1.0)
assert(cftime.date2num(cftime.DatetimeNoLeap(1980, 1, 1, 0, 0, 0, 0, 6, 1),'days since 1970-01-01','noleap') == 3650.0)
def test_pickling(self):
"Test reversibility of pickling."
import pickle
date = Datetime360Day(year=1, month=2, day=3, hour=4, minute=5, second=6, microsecond=7)
self.assertEqual(date, pickle.loads(pickle.dumps(date)))
def setUp(self):
self.date1_365_day = DatetimeNoLeap(-5000, 1, 2, 12)
self.date2_365_day = DatetimeNoLeap(-5000, 1, 3, 12)
self.date3_gregorian = DatetimeGregorian(1969, 7, 20, 12)
# last day of the Julian calendar in the mixed Julian/Gregorian calendar
self.date4_gregorian = DatetimeGregorian(1582, 10, 4)
# first day of the Gregorian calendar in the mixed Julian/Gregorian calendar
self.date5_gregorian = DatetimeGregorian(1582, 10, 15)
self.date6_proleptic_gregorian = DatetimeProlepticGregorian(1582, 10, 15)
self.date7_360_day = Datetime360Day(2000, 1, 1)
self.date8_julian = DatetimeJulian(1582, 10, 4)
# a datetime.datetime instance (proleptic Gregorian calendar)
self.datetime_date1 = datetime(1969, 7, 21, 12)
self.delta = timedelta(hours=25)
self.assertEqual(t, copy.deepcopy(t))
# issue 442
units = "days since 0000-01-01 00:00:00"
# this should fail (year zero not allowed with real-world calendars)
try:
date2num(datetime(1, 1, 1), units, calendar='standard')
except ValueError:
pass
# this should not fail (year zero allowed in 'fake' calendars)
t = date2num(datetime(1, 1, 1), units, calendar='360_day')
self.assertAlmostEqual(t, 360)
d = num2date(t, units, calendar='360_day')
self.assertEqual(d, Datetime360Day(1,1,1))
d = num2date(0, units, calendar='360_day')
self.assertEqual(d, Datetime360Day(0,1,1))
# list around missing dates in Gregorian calendar
# scalar
units = 'days since 0001-01-01 12:00:00'
t1 = date2num(datetime(1582, 10, 4), units, calendar='gregorian')
t2 = date2num(datetime(1582, 10, 15), units, calendar='gregorian')
self.assertEqual(t1+1, t2)
# list
t1, t2 = date2num([datetime(1582, 10, 4), datetime(1582, 10, 15)], units, calendar='gregorian')
self.assertEqual(t1+1, t2)
t1, t2 = date2num([datetime(1582, 10, 4), datetime(1582, 10, 15)], units, calendar='standard')
self.assertEqual(t1+1, t2)
# this should fail: days missing in Gregorian calendar
try:
t1, t2, t3 = date2num([datetime(1582, 10, 4), datetime(1582, 10, 10), datetime(1582, 10, 15)], units, calendar='standard')
except ValueError:
def test_num2date_integer_upcast_required():
numeric_times = np.array([30, 60, 90, 120], dtype=np.int32)
units = "minutes since 2000-01-01"
expected = np.array([
Datetime360Day(2000, 1, 1, 0, 30, 0),
Datetime360Day(2000, 1, 1, 1, 0, 0),
Datetime360Day(2000, 1, 1, 1, 30, 0),
Datetime360Day(2000, 1, 1, 2, 0, 0)
])
result = num2date(numeric_times, units=units, calendar="360_day")
np.testing.assert_equal(result, expected)
def test_valid_julian_gregorian_mixed_dates(date_type, date_args):
date_type(*date_args)
@pytest.mark.parametrize(
'date_args',
[(1, 2, 3, 4, 5, 6), (10, 2, 3, 4, 5, 6), (100, 2, 3, 4, 5, 6),
(1000, 2, 3, 4, 5, 6)],
ids=['1', '10', '100', '1000'])
def test_str_matches_datetime_str(date_type, date_args):
assert str(date_type(*date_args)) == str(datetime(*date_args))
_EXPECTED_DATE_TYPES = {'noleap': DatetimeNoLeap,
'365_day': DatetimeNoLeap,
'360_day': Datetime360Day,
'julian': DatetimeJulian,
'all_leap': DatetimeAllLeap,
'366_day': DatetimeAllLeap,
'gregorian': DatetimeGregorian,
'proleptic_gregorian': DatetimeProlepticGregorian,
'standard': DatetimeGregorian}
@pytest.mark.parametrize(
['calendar', 'expected_date_type'],
list(_EXPECTED_DATE_TYPES.items())
)
def test_num2date_only_use_cftime_datetimes_negative_years(
calendar, expected_date_type):
result = num2date(-1000., units='days since 0001-01-01', calendar=calendar,
only_use_cftime_datetimes=True)
def get_date_type(calendar):
"""Return the cftime date type for a given calendar name."""
try:
import cftime
except ImportError:
raise ImportError("cftime is required for dates with non-standard calendars")
else:
calendars = {
"noleap": cftime.DatetimeNoLeap,
"360_day": cftime.Datetime360Day,
"365_day": cftime.DatetimeNoLeap,
"366_day": cftime.DatetimeAllLeap,
"gregorian": cftime.DatetimeGregorian,
"proleptic_gregorian": cftime.DatetimeProlepticGregorian,
"julian": cftime.DatetimeJulian,
"all_leap": cftime.DatetimeAllLeap,
"standard": cftime.DatetimeGregorian,
}
return calendars[calendar]