MCPcopy Index your code
hub / github.com/pydata/xarray / exact_cftime_datetime_difference

Function exact_cftime_datetime_difference

xarray/core/resample_cftime.py:454–491  ·  view source on GitHub ↗

Exact computation of b - a Assumes: a = a_0 + a_m b = b_0 + b_m Here a_0, and b_0 represent the input dates rounded down to the nearest second, and a_m, and b_m represent the remaining microseconds associated with date a and date b. We can then express the

(a: CFTimeDatetime, b: CFTimeDatetime)

Source from the content-addressed store, hash-verified

452
453
454def exact_cftime_datetime_difference(a: CFTimeDatetime, b: CFTimeDatetime):
455 """Exact computation of b - a
456
457 Assumes:
458
459 a = a_0 + a_m
460 b = b_0 + b_m
461
462 Here a_0, and b_0 represent the input dates rounded
463 down to the nearest second, and a_m, and b_m represent
464 the remaining microseconds associated with date a and
465 date b.
466
467 We can then express the value of b - a as:
468
469 b - a = (b_0 + b_m) - (a_0 + a_m) = b_0 - a_0 + b_m - a_m
470
471 By construction, we know that b_0 - a_0 must be a round number
472 of seconds. Therefore we can take the result of b_0 - a_0 using
473 ordinary cftime.datetime arithmetic and round to the nearest
474 second. b_m - a_m is the remainder, in microseconds, and we
475 can simply add this to the rounded timedelta.
476
477 Parameters
478 ----------
479 a : cftime.datetime
480 Input datetime
481 b : cftime.datetime
482 Input datetime
483
484 Returns
485 -------
486 datetime.timedelta
487 """
488 seconds = b.replace(microsecond=0) - a.replace(microsecond=0)
489 seconds = round(seconds.total_seconds())
490 microseconds = b.microsecond - a.microsecond
491 return datetime.timedelta(seconds=seconds, microseconds=microseconds)
492
493
494def _convert_offset_to_timedelta(

Callers 1

asi8Method · 0.90

Calls 3

roundFunction · 0.85
replaceMethod · 0.80
total_secondsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…