pedantic_timedelta package

Package Module Contents

A Human-friendly Pedantic timedelta formatter.

class pedantic_timedelta.PedanticTimedelta[source]

Bases: datetime.timedelta

Wrapper formats timedelta using least common whole number time unit.

  • A datetime.timedelta() formatter that uses progressive time unit labels to prepare a delta time value for output.

Note

Unlike datetime.timedelta(), which accepts weeks but not months, nor years, because not every month, nor every year has the same number of days, this class fudges the calculation, allowing one to specify imprecise time deltas.

Variables:DAYS_IN_YEAR – Mean tropical year (using Laskar’s expression) on January 1, 2000.
DAYS_IN_MONTH = 30.436849083333332

DAYS_IN_YEAR / 12.0

DAYS_IN_YEAR = 365.242189

Mean tropical year (using Laskar’s expression) on 1/1/2000. - https://en.wikipedia.org/wiki/Tropical_year

SECS_IN_DAY = 86400

1/86,400 is mean of solar day.

SECS_IN_MONTH = 2629743.7608

SECS_IN_YEAR / 12.0

SECS_IN_YEAR = 31556925.1296

DAYS_IN_YEAR * SECS_IN_DAY

UNIT_NAMES = {'day': ('day', 'd', 'dā', 'day', 'days', 'day', 'day'), 'hour': ('hour', 'H', 'hr', 'our', 'hrs.', 'hr', 'hour'), 'minute': ('minute', 'M', 'm.', 'min', 'mins', 'min', 'min'), 'month': ('month', 'm', 'mo', 'mon', 'mos.', 'mon', 'month'), 'second': ('second', 'S', 's.', 'sec', 'secs', 'sec', 'sec'), 'year': ('year', 'y', 'yr', 'yēr', 'yrs.', 'yr', 'year')}
UNIT_NAME_ABBREV = 6
UNIT_NAME_BRIEF = 5
UNIT_NAME_FOURWIDE = 4
UNIT_NAME_FULL = 0
UNIT_NAME_INDEX_0 = 0
UNIT_NAME_INDEX_N = 6
UNIT_NAME_ONECH = 1
UNIT_NAME_TREYWIDE = 3
UNIT_NAME_TWOCH = 2
static __new__(cls=None, days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, fortnights=0, months=0, seasons=0, years=0, bienniums=0, decades=0, jubilees=0, centuries=0, millenniums=0, ages=0, megaannums=0, epochs=0, eras=0, eons=0, gigaannums=0)[source]

Create new PedanticTimedelta instance.

A wrapper around datetime.timedelta.__new__ that recognizes additional time units, including ‘months’, ‘years’, and much, much more.

In addition to the parent class’s parameters – days, seconds, microseconds, minutes, hours, and weeks – the following constructor parameters are recognized.

(Note that all arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative. Only days, seconds and microseconds are stored internally. Other arguments are converted to those units and added together.)

Parameters:
  • fortnights (float) – 14 days each.
  • months (float) – Approximated as DAYS_IN_MONTH.
  • seasons (float) – ¼ year each.
  • years (float) – Approximated as DAYS_IN_YEAR.
  • bienniums (float) – 2 years each.
  • decades (float) – 10 years each.
  • jubilees (float) – 50 years each.
  • centuries (float) – 100 years each.
  • millenniums (float) – 1000 years each.
  • ages (float) – 1000000 years each.
  • megaannums (float) – 1000000 years each.
  • epochs (float) – 10000000 years each.
  • eras (float) – 100000000 years each.
  • eons (float) – 500000000 years each.
  • gigaannums (float) – 1000000000 years each.
static time_format_elapsed(secs_then, secs_now=None)[source]

Format elapsed time pedantically.

Parameters:
  • secs_then (float) – seconds at time of event (e.g., time.time()).
  • secs_now (float) – seconds from which to calculate elapsed time. Defaults to now if not specified (in which case secs_then should be represented as seconds since epoch).
Returns:

elapsed time formatted using single unit of time

Return type:

string

time_format_scaled(field_width=0, precision=2, abbreviate=None)[source]

Format time duration using appropriate precision and time unit.

Format the instance’s elapsed time using the largest single unit of time where value is 1 or more (unless the elapsed time is less than a single second, in which case the value will be expressed in seconds).

Parameters:
  • field_width (int) – Total field width, including decimal point. Defaults to 0, i.e., no minimum width.
  • precision (int) – Time delta float value precision. Default to 2, e.g., “3.14 days.”
  • abbreviate (int) – Integer to indicate whether to abbreviate: 0 to not abbreviate; 1 to use single character; 2-4 to use 2, 3, or 4 character abbreviation, respectively; 5 to use a 2 or 3 character abbreviation, not includings plural and/or period, e.g., “1.00 yr.”, “3.00 hrs.”, “2.45 mins.” 6 (Default) to use set: year, month, day, hour, min/min., sec/sec.
Returns:

tuple containing (formatted time, seconds in unit, time unit)

Return type:

tuple(string, seconds-per-unit, time-unit)

>>> PedanticTimedelta(days=0.33).time_format_scaled()
('7.92 hours', 3600.0, 'hour')