Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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)
# issue #126
d = cftime.DatetimeProlepticGregorian(1, 1, 1)
assert(cftime.date2num(d, 'days since 0001-01-01',\
'proleptic_gregorian') == 0.0)
# issue #140 (fractional seconds in reference date)
d = datetime.strptime('2018-01-23 09:27:10.950000',"%Y-%m-%d %H:%M:%S.%f")
units = 'seconds since 2018-01-23 09:31:42.94'
assert(cftime.date2num(d, units) == -271.99)
# issue 143 - same answer for arrays vs scalars.
units = 'seconds since 1970-01-01 00:00:00'
times_in = [1261440000.0, 1261440001.0, 1261440002.0, 1261440003.0,
1261440004.0, 1261440005.0]
times_out1 = cftime.num2date(times_in, units)
times_out2 = []
for time_in in times_in:
times_out2.append(cftime.num2date(time_in, units))
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)
def test_roundtrip(self):
"Test roundtrip conversion (num2date <-> date2num) using 360_day and 365_day calendars."
for datetime_class in [Datetime360Day, DatetimeNoLeap]:
# Pick a date and time outside of the range of the Julian calendar.
date = datetime_class(-5000, 1, 1, 12)
converter = self.converters[date.calendar]
self.assertEqual(date, converter.num2date(converter.date2num(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)
[(1582, 10, 4), (1582, 10, 15)], ids=['lower-bound', 'upper-bound'])
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),
(2000, 1, 1, 12, 34, 56, 123456)],
ids=['1', '10', '100', '1000', '2000'])
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):
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)
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)
def to_datetime(d):
if isinstance(d, dt.datetime):
return d
if isinstance(d, cftime.DatetimeNoLeap):
return dt.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
elif isinstance(d, cftime.DatetimeGregorian):
return dt.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
elif isinstance(d, str):
errors = []
for fmt in (
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M:%SZ"):
try:
return dt.datetime.strptime(d, fmt)
except ValueError as e:
errors.append(e)
continue
raise Exception(errors)
elif isinstance(d, np.datetime64):