|website=
(help)Wikimedia Commons has media related to Time recorders. |
datetime
and calendar
modules.time.gmtime(0)
.strptime()
can parse 2-digit years when given %y
formatcode. When 2-digit years are parsed, they are converted according to the POSIXand ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68are mapped to 2000–2068.time()
and sleep()
is betterthan their Unix equivalents: times are expressed as floating point numbers,time()
returns the most accurate time available (using Unixgettimeofday()
where available), and sleep()
will accept a timewith a nonzero fraction (Unix select()
is used to implement this, whereavailable).gmtime()
, localtime()
, andstrptime()
, and accepted by asctime()
, mktime()
andstrftime()
, is a sequence of 9 integers. The return values ofgmtime()
, localtime()
, and strptime()
also offer attributenames for individual fields.struct_time
for a description of these objects.struct_time
type was extended to provide the tm_gmtoff
and tm_zone
attributes when platform supports correspondingstructtm
members.struct_time
attributes tm_gmtoff
and tm_zone
are now available on all platforms.From | To | Use |
---|---|---|
seconds since the epoch | struct_time inUTC | |
seconds since the epoch | struct_time inlocal time | |
struct_time inUTC | seconds since the epoch | |
struct_time inlocal time | seconds since the epoch |
time.
asctime
([t])¶struct_time
representing a time as returned bygmtime()
or localtime()
to a string of the followingform: 'SunJun2023:21:051993'
. The day field is two characters longand is space padded if the day is a single digit,e.g.: 'WedJun904:26:401993'
.localtime()
is used. Locale information is not used by asctime()
.asctime()
does not add atrailing newline.time.
pthread_getcpuclockid
(thread_id)¶threading.get_ident()
or the ident
attribute of threading.Thread
objects to get a suitable valuefor thread_id.time.
clock_getres
(clk_id)¶time.
clock_gettime
(clk_id) → float¶time.
clock_gettime_ns
(clk_id) → int¶clock_gettime()
but return time as nanoseconds.time.
clock_settime
(clk_id, time: float)¶CLOCK_REALTIME
is the only accepted value for clk_id.time.
clock_settime_ns
(clk_id, time: int)¶clock_settime()
but set time with nanoseconds.time.
ctime
([secs])¶'SunJun2023:21:051993'
representing local time. The day fieldis two characters long and is space padded if the day is a single digit,e.g.: 'WedJun904:26:401993'
.None
, the current time asreturned by time()
is used. ctime(secs)
is equivalent toasctime(localtime(secs))
. Locale information is not used byctime()
.time.
get_clock_info
(name)¶'monotonic'
: time.monotonic()
'perf_counter'
: time.perf_counter()
'process_time'
: time.process_time()
'thread_time'
: time.thread_time()
'time'
: time.time()
True
if the clock can be changed automatically (e.g. bya NTP daemon) or manually by the system administrator, False
otherwiseTrue
if the clock cannot go backward,False
otherwisefloat
)time.
gmtime
([secs])¶struct_time
inUTC in which the dst flag is always zero. If secs is not provided orNone
, the current time as returned by time()
is used. Fractionsof a second are ignored. See above for a description of thestruct_time
object. See calendar.timegm()
for the inverse of thisfunction.time.
localtime
([secs])¶gmtime()
but converts to local time. If secs is not provided orNone
, the current time as returned by time()
is used. The dstflag is set to 1
when DST applies to the given time.time.
mktime
(t)¶localtime()
. Its argument is thestruct_time
or full 9-tuple (since the dst flag is needed; use -1
as the dst flag if it is unknown) which expresses the time in local time, notUTC. It returns a floating point number, for compatibility with time()
.If the input value cannot be represented as a valid time, eitherOverflowError
or ValueError
will be raised (which depends onwhether the invalid value is caught by Python or the underlying C libraries).The earliest date for which it can generate a time is platform-dependent.time.
monotonic
() → float¶time.
monotonic_ns
() → int¶monotonic()
, but return time as nanoseconds.time.
perf_counter
() → float¶time.
perf_counter_ns
() → int¶perf_counter()
, but return time as nanoseconds.time.
process_time
() → float¶time.
process_time_ns
() → int¶process_time()
but return time as nanoseconds.time.
sleep
(secs)¶sleep()
following execution of thatsignal’s catching routine. Also, the suspension time may be longer thanrequested by an arbitrary amount because of the scheduling of other activityin the system.time.
strftime
(format[, t])¶struct_time
representing a time as returned bygmtime()
or localtime()
to a string as specified by the formatargument. If t is not provided, the current time as returned bylocaltime()
is used. format must be a string. ValueError
israised if any field in t is outside of the allowed range.strftime()
result:Directive | Meaning | Notes |
---|---|---|
%a | Locale’s abbreviated weekday name. | |
%A | Locale’s full weekday name. | |
%b | Locale’s abbreviated month name. | |
%B | Locale’s full month name. | |
%c | Locale’s appropriate date and timerepresentation. | |
%d | Day of the month as a decimal number [01,31]. | |
%H | Hour (24-hour clock) as a decimal number[00,23]. | |
%I | Hour (12-hour clock) as a decimal number[01,12]. | |
%j | Day of the year as a decimal number [001,366]. | |
%m | Month as a decimal number [01,12]. | |
%M | Minute as a decimal number [00,59]. | |
%p | Locale’s equivalent of either AM or PM. | (1) |
%S | Second as a decimal number [00,61]. | (2) |
%U | Week number of the year (Sunday as the firstday of the week) as a decimal number [00,53].All days in a new year preceding the firstSunday are considered to be in week 0. | (3) |
%w | Weekday as a decimal number [0(Sunday),6]. | |
%W | Week number of the year (Monday as the firstday of the week) as a decimal number [00,53].All days in a new year preceding the firstMonday are considered to be in week 0. | (3) |
%x | Locale’s appropriate date representation. | |
%X | Locale’s appropriate time representation. | |
%y | Year without century as a decimal number[00,99]. | |
%Y | Year with century as a decimal number. | |
%z | Time zone offset indicating a positive ornegative time difference from UTC/GMT of theform +HHMM or -HHMM, where H represents decimalhour digits and M represents decimal minutedigits [-23:59, +23:59]. | |
%Z | Time zone name (no characters if no time zoneexists). | |
%% | A literal '%' character. |
strptime()
function, the %p
directive only affectsthe output hour field if the %I
directive is used to parse the hour.0
to 61
; value 60
is valid intimestamps representing leap seconds and value 61
is supportedfor historical reasons.strptime()
function, %U
and %W
are only used incalculations when the day of the week and the year are specified.'%'
of a directive in the following order;this is also not portable. The field width is normally 2 except for %j
whereit is 3.time.
strptime
(string[, format])¶struct_time
as returned by gmtime()
orlocaltime()
.strftime()
; it defaults to '%a%b%d%H:%M:%S%Y'
which matches theformatting returned by ctime()
. If string cannot be parsed accordingto format, or if it has excess data after parsing, ValueError
israised. The default values used to fill in any missing data when moreaccurate values cannot be inferred are (1900,1,1,0,0,0,0,1,-1)
.Both string and format must be strings.%Z
directive is based on the values contained in tzname
and whether daylight
is true. Because of this, it is platform-specificexcept for recognizing UTC and GMT which are always known (and are considered tobe non-daylight savings timezones).strftime()
is implemented per platform it can sometimes offer moredirectives than those listed. But strptime()
is independent of any platformand thus does not necessarily support all directives available that are notdocumented as supported.time.
struct_time
¶gmtime()
,localtime()
, and strptime()
. It is an object with a namedtuple interface: values can be accessed by index and by attribute name. Thefollowing values are present:Index | Attribute | Values |
---|---|---|
0 | tm_year | (for example, 1993) |
1 | tm_mon | range [1, 12] |
2 | tm_mday | range [1, 31] |
3 | tm_hour | range [0, 23] |
4 | tm_min | range [0, 59] |
5 | tm_sec | range [0, 61]; see (2) in strftime() description |
6 | tm_wday | range [0, 6], Monday is 0 |
7 | tm_yday | range [1, 366] |
8 | tm_isdst | 0, 1 or -1; see below |
N/A | tm_zone | abbreviation of timezone name |
N/A | tm_gmtoff | offset east of UTC in seconds |
mktime()
, tm_isdst
may be set to 1 when daylightsavings time is in effect, and 0 when it is not. A value of -1 indicates thatthis is not known, and will usually result in the correct state being filled in.struct_time
, or having elements of the wrong type, aTypeError
is raised.time.
time
() → float¶gmtime(0)
.time()
may be converted into a more commontime format (i.e. year, month, day, hour, etc…) in UTC by passing it togmtime()
function or in local time by passing it to thelocaltime()
function. In both cases astruct_time
object is returned, from which the componentsof the calendar date may be accessed as attributes.time.
thread_time
() → float¶CLOCK_THREAD_CPUTIME_ID
.time.
thread_time_ns
() → int¶thread_time()
but return time as nanoseconds.time.
time_ns
() → int¶time()
but returns time as an integer number of nanosecondssince the epoch.time.
tzset
()¶TZ
specifies how this is done. It will also set the variablestzname
(from the TZ
environment variable), timezone
(non-DSTseconds West of UTC), altzone
(DST seconds west of UTC) and daylight
(to 0 if this timezone does not have any daylight saving time rules, or tononzero if there is a time, past, present or future when daylight saving timeapplies).TZ
environment variable mayaffect the output of functions like localtime()
without callingtzset()
, this behavior should not be relied on.TZ
environment variable should contain no whitespace.TZ
environment variable is (whitespaceadded for clarity):std
and dst
offset
±hh[:mm[:ss]]
. This indicates the valueadded the local time to arrive at UTC. If preceded by a ‘-‘, the timezoneis east of the Prime Meridian; otherwise, it is west. If no offset followsdst, summer time is assumed to be one hour ahead of standard time.start[/time],end[/time]
Jn
n
Mm.n.d
time
has the same format as offset
except that no leading sign(‘-‘ or ‘+’) is allowed. The default, if time is not given, is 02:00:00.TZ
environmentvariable to the path of the required timezone datafile, relative to the root ofthe systems ‘zoneinfo’ timezone database, usually located at/usr/share/zoneinfo
. For example, 'US/Eastern'
,'Australia/Melbourne'
, 'Egypt'
or 'Europe/Amsterdam'
.clock_getres()
andclock_gettime()
.time.
CLOCK_BOOTTIME
¶CLOCK_MONOTONIC
, except it also includes any time thatthe system is suspended.CLOCK_REALTIME
, which mayhave discontinuities if the time is changed using settimeofday()
orsimilar.time.
CLOCK_HIGHRES
¶CLOCK_HIGHRES
timer that attempts to use an optimalhardware source, and may give close to nanosecond resolution.CLOCK_HIGHRES
is the nonadjustable, high-resolution clock.time.
CLOCK_MONOTONIC
¶time.
CLOCK_MONOTONIC_RAW
¶CLOCK_MONOTONIC
, but provides access to a rawhardware-based time that is not subject to NTP adjustments.time.
CLOCK_PROCESS_CPUTIME_ID
¶time.
CLOCK_PROF
¶time.
CLOCK_TAI
¶time.
CLOCK_THREAD_CPUTIME_ID
¶time.
CLOCK_UPTIME
¶time.
CLOCK_UPTIME_RAW
¶clock_settime()
.time.
CLOCK_REALTIME
¶time.
altzone
¶daylight
is nonzero. See note below.time.
daylight
¶time.
timezone
¶time.
tzname
¶altzone
, daylight
, timezone
,and tzname
), the value is determined by the timezone rules in effectat module load time or the last time tzset()
is called and may be incorrectfor times in the past. It is recommended to use the tm_gmtoff
andtm_zone
results from localtime()
to obtain timezone information.datetime
locale
strftime()
and strptime()
.calendar
timegm()
is theinverse of gmtime()
from this module.%Z
is now deprecated, but the %z
escape that expands to thepreferred hour/minute offset is not supported by all ANSI C libraries. Also, astrict reading of the original 1982 RFC 822 standard calls for a two-digityear (%y rather than %Y), but practice moved to 4-digit years long before theyear 2000. After that, RFC 822 became obsolete and the 4-digit year hasbeen first recommended by RFC 1123 and then mandated by RFC 2822.