Skip to content

Commit ce4717e

Browse files
committed
remove microsecond handling
1 parent 2654f28 commit ce4717e

File tree

1 file changed

+2
-68
lines changed

1 file changed

+2
-68
lines changed

cf_units/__init__.py

+2-68
Original file line numberDiff line numberDiff line change
@@ -387,39 +387,6 @@ def date2num(date, unit, calendar):
387387
return unit_inst.date2num(date)
388388

389389

390-
def _discard_microsecond(date):
391-
"""
392-
Return a date with the microsecond component discarded.
393-
394-
Works for scalars, sequences and numpy arrays. Returns a scalar
395-
if input is a scalar, else returns a numpy array.
396-
397-
Args:
398-
399-
* date (datetime.datetime or cftime.datetime):
400-
Date value/s
401-
402-
Returns:
403-
datetime, or numpy.ndarray of datetime object.
404-
405-
"""
406-
dates = np.asarray(date)
407-
shape = dates.shape
408-
dates = dates.ravel()
409-
# Create date objects of the same type returned by cftime.num2date()
410-
# (either datetime.datetime or cftime.datetime), discarding the
411-
# microseconds
412-
dates = np.array(
413-
[
414-
d
415-
and d.__class__(d.year, d.month, d.day, d.hour, d.minute, d.second)
416-
for d in dates
417-
]
418-
)
419-
result = dates[0] if shape == () else dates.reshape(shape)
420-
return result
421-
422-
423390
def num2date(
424391
time_value,
425392
unit,
@@ -552,47 +519,16 @@ def _num2date_to_nearest_second(
552519
datetime, or numpy.ndarray of datetime object.
553520
554521
"""
555-
time_values = np.asanyarray(time_value)
556-
shape = time_values.shape
557-
time_values = time_values.ravel()
558522

559-
# We account for the edge case where the time is in seconds and has a
560-
# half second: cftime.num2date() may produce a date that would round
561-
# down.
562-
#
563-
# Note that this behaviour is different to the num2date function in version
564-
# 1.1 and earlier of cftime that didn't have microsecond precision. In
565-
# those versions, a half-second value would be rounded up or down
566-
# arbitrarily. It is probably not possible to replicate that behaviour with
567-
# later versions, if one wished to do so for the sake of consistency.
568523
cftime_unit = unit.cftime_unit
569-
time_units = cftime_unit.split(" ")[0]
570-
has_half_seconds = np.logical_and(
571-
time_units == "seconds", time_values % 1.0 == 0.5
572-
)
524+
573525
num2date_kwargs = dict(
574526
units=cftime_unit,
575527
calendar=unit.calendar,
576528
only_use_cftime_datetimes=only_use_cftime_datetimes,
577529
only_use_python_datetimes=only_use_python_datetimes,
578530
)
579-
dates = cftime.num2date(time_values, **num2date_kwargs)
580-
# try:
581-
# # We can assume all or none of the dates have a microsecond attribute
582-
# microseconds = np.array([d.microsecond if d else 0 for d in dates])
583-
# except AttributeError:
584-
# microseconds = 0
585-
# round_mask = np.logical_or(has_half_seconds, microseconds != 0)
586-
# ceil_mask = np.logical_or(has_half_seconds, microseconds >= 500000)
587-
# if time_values[ceil_mask].size > 0:
588-
# useconds = Unit("second")
589-
# second_frac = useconds.convert(0.75, time_units)
590-
# dates[ceil_mask] = cftime.num2date(
591-
# time_values[ceil_mask] + second_frac, **num2date_kwargs
592-
# )
593-
# dates[round_mask] = _discard_microsecond(dates[round_mask])
594-
result = dates[0] if shape == () else dates.reshape(shape)
595-
return result
531+
return cftime.num2date(time_value, **num2date_kwargs)
596532

597533

598534
_CACHE = {}
@@ -1971,8 +1907,6 @@ def date2num(self, date):
19711907
array([5.5, 6.5])
19721908
19731909
"""
1974-
1975-
# date = _discard_microsecond(date)
19761910
return cftime.date2num(date, self.cftime_unit, self.calendar)
19771911

19781912
def num2date(

0 commit comments

Comments
 (0)