[Python-ideas] millisecond and microsecond times without floats

M.-A. Lemburg mal at egenix.com
Wed Jun 24 09:50:16 CEST 2015

On 23.06.2015 01:15, Paul Sokolovsky wrote:
> Hello from MicroPython, a lean Python implementation
> scaling down to run even on microcontrollers 
> (https://github.com/micropython/micropython).
> Our target hardware base oftentimes lacks floating point support, and
> using software emulation is expensive. So, we would like to have
> versions of some timing functions, taking/returning millisecond and/or
> microsecond values as integers.
> The most functionality we're interested in:
> 1. Delays
> 2. Relative time (from an arbitrary starting point, expected to be
>    wrapped)
> 3. Calculating time differences, with immunity to wrap-around.
> The first presented assumption is to use "time.sleep()" for delays,
> "time.monotonic()" for relative time as the base. Would somebody gave
> alternative/better suggestions?
> Second question is how to modify their names for
> millisecond/microsecond versions. For sleep(), "msleep" and "usleep"
> would be concise possibilities, but that doesn't map well to
> monotonic(), leading to "mmonotonic". So, better idea is to use "_ms"
> and "_us" suffixes:
> sleep_ms()
> sleep_us()
> monotonic_ms()
> monotonic_us()
> Point 3 above isn't currently addressed by time module at all.
> https://www.python.org/dev/peps/pep-0418/ mentions some internal
> workaround for overflows/wrap-arounds on some systems. Due to
> lean-ness of our hardware base, we'd like to make this matter explicit
> to the applications and avoid internal workarounds. Proposed solution
> is to have time.elapsed(time1, time2) function, which can take values
> as returned by monotonic_ms(), monotonic_us(). Assuming that results of
> both functions are encoded and wrap consistently (this is reasonable
> assumption), there's no need for 2 separate elapsed_ms(), elapsed_us()
> function.
> So, the above are rough ideas we (well, I) have. We'd like to get wider
> Python community feedback on them, see if there're better/alternative
> ideas, how Pythonic it is, etc. To clarify, this should not be construed
> as proposal to add the above functions to CPython.

You may want to use a similar approach as I have used in
mxDateTime to express date/time values:


It uses an integer to represent days and a float to represent
seconds since midnight (i.e. time of day). The concept has worked
out really well and often makes date/time calculations a lot
easier than trying to stuff everything into a single number and
then having to deal things like leap seconds and rounding errors.

In your case you'd use integers for both and nanoseconds
as basis for the time of day integer.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Jun 24 2015)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
2015-06-16: Released eGenix pyOpenSSL 0.13.10 ... http://egenix.com/go78
2015-07-20: EuroPython 2015, Bilbao, Spain ...             26 days to go
2015-07-29: Python Meeting Duesseldorf ...                 35 days to go

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-ideas mailing list