Convert Gregorian float of the date, preserving hours, minutes, seconds and microseconds. Return value is a `.datetime`. The input date *x* is a float in ordinal days at UTC, and the output will be the specified `.datetime` object corresponding to that time in timezone *tz*, o
(x, tz=None)
| 330 | |
| 331 | |
| 332 | def _from_ordinalf(x, tz=None): |
| 333 | """ |
| 334 | Convert Gregorian float of the date, preserving hours, minutes, |
| 335 | seconds and microseconds. Return value is a `.datetime`. |
| 336 | |
| 337 | The input date *x* is a float in ordinal days at UTC, and the output will |
| 338 | be the specified `.datetime` object corresponding to that time in |
| 339 | timezone *tz*, or if *tz* is ``None``, in the timezone specified in |
| 340 | :rc:`timezone`. |
| 341 | """ |
| 342 | |
| 343 | tz = _get_tzinfo(tz) |
| 344 | |
| 345 | dt = (np.datetime64(get_epoch()) + |
| 346 | np.timedelta64(int(np.round(x * MUSECONDS_PER_DAY)), 'us')) |
| 347 | if dt < np.datetime64('0001-01-01') or dt >= np.datetime64('10000-01-01'): |
| 348 | raise ValueError(f'Date ordinal {x} converts to {dt} (using ' |
| 349 | f'epoch {get_epoch()}), but Matplotlib dates must be ' |
| 350 | 'between year 0001 and 9999.') |
| 351 | # convert from datetime64 to datetime: |
| 352 | dt = dt.tolist() |
| 353 | |
| 354 | # datetime64 is always UTC: |
| 355 | dt = dt.replace(tzinfo=dateutil.tz.gettz('UTC')) |
| 356 | # but maybe we are working in a different timezone so move. |
| 357 | dt = dt.astimezone(tz) |
| 358 | # fix round off errors |
| 359 | if np.abs(x) > 70 * 365: |
| 360 | # if x is big, round off to nearest twenty microseconds. |
| 361 | # This avoids floating point roundoff error |
| 362 | ms = round(dt.microsecond / 20) * 20 |
| 363 | if ms == 1000000: |
| 364 | dt = dt.replace(microsecond=0) + datetime.timedelta(seconds=1) |
| 365 | else: |
| 366 | dt = dt.replace(microsecond=ms) |
| 367 | |
| 368 | return dt |
| 369 | |
| 370 | |
| 371 | # a version of _from_ordinalf that can operate on numpy arrays |
nothing calls this directly
no test coverage detected
searching dependent graphs…