Skip to content

Commit ae2fe3b

Browse files
committed
remove microsecond handling
1 parent 3753a83 commit ae2fe3b

File tree

1 file changed

+2
-62
lines changed

1 file changed

+2
-62
lines changed

cf_units/__init__.py

+2-62
Original file line numberDiff line numberDiff line change
@@ -378,33 +378,6 @@ def date2num(date, unit, calendar):
378378
return unit_inst.date2num(date)
379379

380380

381-
def _discard_microsecond(date):
382-
"""
383-
Return a date with the microsecond component discarded.
384-
385-
Works for scalars, sequences and numpy arrays. Returns a scalar
386-
if input is a scalar, else returns a numpy array.
387-
388-
Args:
389-
390-
* date (datetime.datetime or cftime.datetime):
391-
Date value/s
392-
393-
Returns:
394-
datetime, or numpy.ndarray of datetime object.
395-
396-
"""
397-
dates = np.asanyarray(date)
398-
shape = dates.shape
399-
dates = dates.ravel()
400-
401-
# using the "and" pattern to support masked arrays of datetimes
402-
dates = np.array([dt and dt.replace(microsecond=0) for dt in dates])
403-
result = dates[0] if shape == () else dates.reshape(shape)
404-
405-
return result
406-
407-
408381
def num2date(
409382
time_value,
410383
unit,
@@ -537,47 +510,16 @@ def _num2date_to_nearest_second(
537510
datetime, or numpy.ndarray of datetime object.
538511
539512
"""
540-
time_values = np.asanyarray(time_value)
541-
shape = time_values.shape
542-
time_values = time_values.ravel()
543513

544-
# We account for the edge case where the time is in seconds and has a
545-
# half second: cftime.num2date() may produce a date that would round
546-
# down.
547-
#
548-
# Note that this behaviour is different to the num2date function in version
549-
# 1.1 and earlier of cftime that didn't have microsecond precision. In
550-
# those versions, a half-second value would be rounded up or down
551-
# arbitrarily. It is probably not possible to replicate that behaviour with
552-
# later versions, if one wished to do so for the sake of consistency.
553514
cftime_unit = unit.cftime_unit
554-
time_units = cftime_unit.split(" ")[0]
555-
has_half_seconds = np.logical_and(
556-
time_units == "seconds", time_values % 1.0 == 0.5
557-
)
515+
558516
num2date_kwargs = dict(
559517
units=cftime_unit,
560518
calendar=unit.calendar,
561519
only_use_cftime_datetimes=only_use_cftime_datetimes,
562520
only_use_python_datetimes=only_use_python_datetimes,
563521
)
564-
dates = cftime.num2date(time_values, **num2date_kwargs)
565-
# try:
566-
# # We can assume all or none of the dates have a microsecond attribute
567-
# microseconds = np.array([d.microsecond if d else 0 for d in dates])
568-
# except AttributeError:
569-
# microseconds = 0
570-
# round_mask = np.logical_or(has_half_seconds, microseconds != 0)
571-
# ceil_mask = np.logical_or(has_half_seconds, microseconds >= 500000)
572-
# if time_values[ceil_mask].size > 0:
573-
# useconds = Unit("second")
574-
# second_frac = useconds.convert(0.75, time_units)
575-
# dates[ceil_mask] = cftime.num2date(
576-
# time_values[ceil_mask] + second_frac, **num2date_kwargs
577-
# )
578-
# dates[round_mask] = _discard_microsecond(dates[round_mask])
579-
result = dates[0] if shape == () else dates.reshape(shape)
580-
return result
522+
return cftime.num2date(time_value, **num2date_kwargs)
581523

582524

583525
_CACHE = {}
@@ -1978,8 +1920,6 @@ def date2num(self, date):
19781920
array([5.5, 6.5])
19791921
19801922
"""
1981-
1982-
# date = _discard_microsecond(date)
19831923
return cftime.date2num(date, self.cftime_unit, self.calendar)
19841924

19851925
def num2date(

0 commit comments

Comments
 (0)