timeutil.py

Routines for dealing with time and time deltas. I’m a fetishist about printing time, so that’s what’s emphasized.

timeutil.report_timer(start, label, count=None, unit='unit', logger=None, stream=sys.stderr)[source]

Return a message containing the time it took to run a function. if stream supports the .write() method, invoke the message to print to the stream if logger is given, log the message

timeutil.time_method(func, count=None, unit='unit', logger=None, stream=sys.stderr)[source]

Return a function which can be invoked to report the time it takes to execute the function func.

timeutil.DAYS_PER_WEEK = 7.0
timeutil.HOURS_PER_DAY = 24.0
timeutil.MICROSECONDS_PER_MILLISECOND = 1000.0
timeutil.MICROSECONDS_PER_SECOND = 1000000.0
timeutil.MILLISECONDS_PER_SECOND = 1000.0
timeutil.SECONDS_PER_HOUR = 3600.0
timeutil.SECONDS_PER_MINUTE = 60.0
timeutil.convert_delta(timeunit)[source]

Turn a timeunit as an int or float into a datetime.delta.

Not permitting strings right now because there’s a method below that would end up recurring infinitely through this method. TODO: A default parameter could fix that.

timeutil.parse_colon_delta(deltastring)[source]

Assume no more than hours specified, ie, 10:00

timeutil.parse_comparable_delta(comparable)[source]

Parse a comparable_delta string to return a timedelta.

timeutil.pretty_delta(delta, weeksonly=False, nofractions=False)[source]

Format a timedelta reasonably nicely, so that it doesn’t print with absurd precision. i.e., no ‘1 month, 2 seconds’ or ‘15.000010 seconds’

It only approximates months, so times as long as a year will be off by .16 months. The message will say ‘About x months.’

weeksonly = True will print precise weeks instead. nofractions = True will not print fractional seconds.

delta can be either a datetime.timedelta or an interval in seconds

timeutil.printable_elapsed_stamp(timezero=False)[source]

Return a string showing elapsed time in standard sortable form. Although this is “exposed” as a “public” method, it is chiefly a utility function for stampit()

The first invocation will return 0 elapsed time.

timezero resets elapsed time to zero if true.

timeutil.report_timed_loop(func, unit='iteration', logger=None, stream=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)[source]

Return a report of how long an iterative function takes to run

timeutil.rounded_seconds_in_delta(delta)[source]

Return the seconds in datetime.timedelta, delta, rounded by the microseconds. Note, this differs from timedelta.total_seconds(), which should truncate, but doesn’t.

delta can be either a datetime.timedelta or an interval in seconds

timeutil.stampit(message, stream=None, logfile=None, timezero=False)[source]

Print message with a sortable leading timestamp of elapsed time.

If timezero is True, restart the timer (and thus print 00 seconds elapsed)

timeutil.start_timer()[source]

Return the current time, to be used as the first parameter to report_timer().

timeutil.timestamp(when=None, seconds=False, microseconds=False, printed=False)[source]

Return the string representation of the date controlled by a string format.

timeutil.walltime(delta)[source]

Format a timedelta to H:MM:SS