Skip to main content

time

Functions related to time and dates. Includes wall-clock and monotonic clocks, sleeping, time formatting, and the struct_time named tuple.

Source-of-record: Modules/timemodule.c, time docs.

Clocks

FunctionReturns
time()Wall clock seconds (float).
time_ns()Wall clock nanoseconds (int).
monotonic() / monotonic_ns()Monotonic clock; cannot go backwards.
perf_counter() / perf_counter_ns()Highest available resolution clock.
process_time() / process_time_ns()CPU time of the process (no sleep).
thread_time() / thread_time_ns()CPU time of the current thread.
get_clock_info(name)Resolution / monotonic / adjustable info.

Sleep

FunctionBehaviour
sleep(secs)Yields the GIL; signals interrupt.
clock_settime(clock_id, time)POSIX clock adjustment (privileged).
clock_gettime(clock_id)Read POSIX clock.
CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_MONOTONIC_RAW, CLOCK_BOOTTIME, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_IDConstants.

struct_time

time.struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec,
tm_wday, tm_yday, tm_isdst, tm_zone, tm_gmtoff)

tm_wday Monday = 0; tm_yday 1-based; tm_isdst 1, 0, or -1.

Formatting

FunctionReturns
gmtime(seconds=None)struct_time in UTC.
localtime(seconds=None)struct_time in local tz.
mktime(struct_time)Local-tz time_t from struct.
asctime(struct_time=None)'Sun Jun 20 23:21:05 1993'.
ctime(seconds=None)asctime(localtime(seconds)).
strftime(format, t=None)Format struct_time.
strptime(string, format)Parse string.
tzset()Re-read TZ (POSIX).
tzname / daylight / timezone / altzoneLocal-tz info attributes.

strftime / strptime directives

%Y %m %d %H %M %S %f %A %a %B %b %p %j %U %W %w %Z %z %s %%.

Gopy status

AreaState
Clock functionsComplete; monotonic backed by Go's time.Time.
struct_time and formattingComplete.
strftime / strptime directivesComplete.
POSIX clock_gettime/settimeComplete on POSIX.

Reference

  • CPython 3.14: time.
  • Modules/timemodule.c.
  • module/_time/. gopy port.