Skip to main content

implementation of PEP0418 with the "Choosing the clock from a list of constraints" get_clock() and get_clocks() functions

Project description

A framework to present system clocks by feature, intended to avoid the library-as-policy pitfalls of the discussion around PEP 418.

My 2c:

Class BaseClock

A BaseClock is the private base class of clock objects.

A clock has the following mandatory attributes:

  • .flags: Feature flags describing the clock.

A clock may have the following optional attributes:

  • .epoch: If present, the offset from time.time()'s epoch of this clock's epoch(). Not all clocks have epochs; some measure elapsed time from an unknown point and only the difference in two measurements is useful.
  • .resolution: The resolution of the underlying clock facility's reporting units. The clock can never be more precise than this value. The actual accuracy of the reported time may vary with adjustments and the real accuracy of the underlying OS clock facility (which in turn may be dependent on the precision of some hardware clock).

A clock must also supply the following methods:

  • .now(): Report the current time in seconds, a float.

Class ClockEntry

MRO: builtins.tuple
ClockEntry(flags, factory)

Class ClockFlags

MRO: builtins.int
An int with human friendly str() and repr() for clock flags.

The flag names are:

  • HIGHRES: clock with the highest resolution.
  • MONOTONIC: clock does not go backwards.
  • STEADY: clock with high stability and relatively high accuracy and precision.
  • ADJUSTED: clock whose value may be changed to the correct time.
  • WALLCLOCK: what the clock on the wall shows.
  • RUNTIME: clock based on the process running time.
  • SYNTHETIC: clock computed from other clocks, such as a monotonic clock computed from other nonmonotonic clocks.

Function get_clock(flags=0, clocklist=None)

Return a clock based on the supplied flags. The returned clock shall have all the requested flags. If no clock matches, return None.

Function get_clocks(flags=0, clocklist=None)

Yield all clocks matching the supplied flags. The returned clocks shall have all the requested flags.

Function highres()

Return the current time according to the default high resolution clock.

Function highres_clock(other_flags=0)

Return a high resolution clock, preferably steady.

Function monotonic()

Return the current time according to the default monotonic clock.

Function monotonic_clock(other_flags=0)

Return a monotonic clock, preferably high resolution.

Function steady()

Return the current time according to the default steady clock.

Function steady_clock(other_flags=0)

Return a steady clock, preferably high resolution.

Class SyntheticMonotonic

MRO: BaseClock
An example synthetic clock.

This class comes after time.time() because I think synthetic clocks should be less desired - they tend to have side effects; but perhaps offered anyway because they can offer flag combinations not always presented by the system clocks.

A simple synthetic monotonic clock may skew with respect to other instances. Steven D'Aprano wrote a better one.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.clockutils-20190101.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file cs.clockutils-20190101.tar.gz.

File metadata

  • Download URL: cs.clockutils-20190101.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.6

File hashes

Hashes for cs.clockutils-20190101.tar.gz
Algorithm Hash digest
SHA256 df414bb2fad45446b28981cac5033b5c294539b4925246e87c603eca5c6f0393
MD5 a2f0f486a82ff8764c7fd6b57a9fb889
BLAKE2b-256 38d6191f87f18cc22c88e7c2fa1bd2b48428d6ad5e6b9ae6d68411f698ae829c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page