pyppin.text.now_and_then

[View Source]

Produce human-readable strings expressing relative time for status pages and debugging.

Functions

now_and_then(now, then[, julian, format])

Output a string '{then}, {duration} ago' or '{then}, {duration} from now', or the like, expressing "then" in terms of "now." This tends to be useful in debugging and status pages.

relative_time_string(delta[, julian])

Express a time delta in words, e.g.

time_delta_string(delta[, julian, sign_mode])

Like relative_time_string, but using a sign_mode (-, +, etc) instead of "ago" and "from now."

Classes

Formats()

This class just wraps some handy constants to pass as 'format' to now_and_then.

pyppin.text.now_and_then.now_and_then(now: Optional[datetime], then: datetime, julian: bool = False, format: Optional[str] = None) str[source]

Output a string ‘{then}, {duration} ago’ or ‘{then}, {duration} from now’, or the like, expressing “then” in terms of “now.” This tends to be useful in debugging and status pages.

For hopefully obvious reasons, don’t use this in outside-facing UI’s; this code is 100% English-based, and would be virtually impossible to localize, as different cultures express temporal relationships differently.

Parameters
  • now – The current time, which we use as a baseline. If not given, we use right now.

  • then – The time we want to express

  • julian – If True, use Julian (astronomical) years for intervals longer than a year; otherwise, use Gregorian years.

  • format – If given, the format in which to print out ‘now’. The default is to use ISO format.

The argument order may seem a bit counterintuitive at first (why is the time we want to express the second argument??) but experiment shows it works well, because it matches the name of the function, which is also a common English idiom, and you usually have a variable named ‘now’ anyway!

class pyppin.text.now_and_then.Formats[source]

Bases: object

This class just wraps some handy constants to pass as ‘format’ to now_and_then.

LONG_FORMAT = '%A, %B %d, %Y %X'[source]

A standard “long” format, ‘Monday, July 18, 2022 15﹕20﹕23’

LOCAL_DATETIME = '%c'[source]

The localized datetime format, e.g. ‘Wed Jul 18 15﹕20﹕23 1988’

LOCAL_DATE_ONLY = '%x'[source]

The localized date-only format, e.g. ‘Wed Jul 18 1988’

LOCAL_TIME_ONLY = '%X'[source]

The localized time-only format, e.g. ‘15﹕20﹕23’

pyppin.text.now_and_then.relative_time_string(delta: timedelta, julian: bool = False) str[source]

Express a time delta in words, e.g. “15 seconds ago” or “3 years from now.”

Parameters
  • delta – The time to be expressed.

  • julian – If True, use Julian (astronomical) years; otherwise, use Gregorian years.

pyppin.text.now_and_then.time_delta_string(delta: timedelta, julian: bool = False, sign_mode: Sign = Sign.NEGATIVE_ONLY) str[source]

Like relative_time_string, but using a sign_mode (-, +, etc) instead of “ago” and “from now.”

So for example, it may yield “+3 years” or “-15 seconds” rather than “3 years from now” and “15 seconds ago.”