Teks sumber — English
Terjemahan — Bahasa Indonesia
Although this module is always available, not all functions are available on all platforms. Most of the functions defined in this module call platform C library functions with the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.
On the other hand, the precision of :func:`.time` and :func:`sleep` is better than their Unix equivalents: times are expressed as floating-point numbers, :func:`.time` returns the most accurate time available (using Unix :c:func:`!gettimeofday` where available), and :func:`sleep` will accept a time with a nonzero fraction (Unix :c:func:`!select` is used to implement this, where available).
Use the following functions to convert between time representations:
Use
Gunakan
If *time_tuple* is not provided, the current time as returned by :func:`localtime` is used. Locale information is not used by :func:`asctime`.
Use :func:`threading.get_ident` or the :attr:`~threading.Thread.ident` attribute of :class:`threading.Thread` objects to get a suitable value for *thread_id*.
Use :func:`clock_gettime_ns` to avoid the precision loss caused by the :class:`float` type.
Use :func:`clock_settime_ns` to avoid the precision loss caused by the :class:`float` type.
If *seconds* is not provided or :const:`None`, the current time as returned by :func:`.time` is used. ``ctime(seconds)`` is equivalent to ``asctime(localtime(seconds))``. Locale information is not used by :func:`ctime`.
*implementation*: The name of the underlying C function used to get the clock value. Refer to :ref:`time-clock-id-constants` for possible values.
Convert a time expressed in seconds since the epoch_ to a :class:`struct_time` in UTC in which the dst flag is always zero. If *seconds* is not provided or :const:`None`, the current time as returned by :func:`.time` is used. Fractions of a second are ignored. See above for a description of the :class:`struct_time` object. See :func:`calendar.timegm` for the inverse of this function.
Like :func:`gmtime` but converts to local time. If *seconds* is not provided or :const:`None`, the current time as returned by :func:`.time` is used. The dst flag is set to ``1`` when DST applies to the given time.
This is the inverse function of :func:`localtime`. Its argument is the :class:`struct_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, not UTC. It returns a floating-point number, for compatibility with :func:`.time`. If the input value cannot be represented as a valid time, either :exc:`OverflowError` or :exc:`ValueError` will be raised (which depends on whether 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.
Use :func:`monotonic_ns` to avoid the precision loss caused by the :class:`float` type.
On CPython, use the same clock as :func:`time.monotonic` and is a monotonic clock, i.e. a clock that cannot go backwards.
Use :func:`perf_counter_ns` to avoid the precision loss caused by the :class:`float` type.
Use the same clock as :func:`time.monotonic`.
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.
Use :func:`process_time_ns` to avoid the precision loss caused by the :class:`float` type.
The suspension time may be longer than requested by an arbitrary amount, because of the scheduling of other activity in the system.
On Windows, if *seconds* is zero, the thread relinquishes the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution. On Windows 10 and newer the implementation uses a `high-resolution timer <https://learn.microsoft.com/windows/win32/api/synchapi/nf-synchapi-createwaitabletimerexw>`_ which provides resolution of 100 nanoseconds. If *seconds* is zero, ``Sleep(0)`` is used.
Use ``clock_nanosleep()`` if available (resolution: 1 nanosecond);
Or use ``nanosleep()`` if available (resolution: 1 nanosecond);
Or use ``select()`` (resolution: 1 microsecond).
To emulate a "no-op", use :keyword:`pass` instead of ``time.sleep(0)``.
To voluntarily relinquish the CPU, specify a real-time :ref:`scheduling policy <os-scheduling-policy>` and use :func:`os.sched_yield` instead.
On Unix, the ``clock_nanosleep()`` and ``nanosleep()`` functions are now used if available. On Windows, a waitable timer is now used.
Convert a tuple or :class:`struct_time` representing a time as returned by :func:`gmtime` or :func:`localtime` to a string as specified by the *format* argument. If *time_tuple* is not provided, the current time as returned by :func:`localtime` is used. *format* must be a string. :exc:`ValueError` is raised if any field in *time_tuple* is outside of the allowed range.
When used with the :func:`strptime` function, the ``%p`` directive only affects the output hour field if the ``%I`` directive is used to parse the hour.
When used with the :func:`strptime` function, ``%U`` and ``%W`` are only used in calculations when the day of the week and the year are specified.
The *format* parameter uses the same directives as those used by :func:`strftime`; it defaults to ``"%a %b %d %H:%M:%S %Y"`` which matches the formatting returned by :func:`ctime`. If *string* cannot be parsed according to *format*, or if it has excess data after parsing, :exc:`ValueError` is raised. The default values used to fill in any missing data when more accurate values cannot be inferred are ``(1900, 1, 1, 0, 0, 0, 0, 1, -1)``. Both *string* and *format* must be strings.
Support for the ``%Z`` directive is based on the values contained in ``tzname`` and whether ``daylight`` is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).
Only the directives specified in the documentation are supported. Because ``strftime()`` is implemented per platform it can sometimes offer more directives than those listed. But ``strptime()`` is independent of any platform and thus does not necessarily support all directives available that are not documented as supported.
Use :func:`time_ns` to avoid the precision loss caused by the :class:`float` type.
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current thread. It does not include time elapsed during sleep. It is thread-specific by definition. The reference point of the returned value is undefined, so that only the difference between the results of two calls in the same thread is valid.
Use :func:`thread_time_ns` to avoid the precision loss caused by the :class:`float` type.
Reset the time conversion rules used by the library routines. The environment variable :envvar:`TZ` specifies how this is done. It will also set the variables ``tzname`` (from the :envvar:`TZ` environment variable), ``timezone`` (non-DST seconds 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 to nonzero if there is a time, past, present or future when daylight saving time applies).
On many Unix systems (including \*BSD, Linux, Solaris, and Darwin), it is more convenient to use the system's zoneinfo (:manpage:`tzfile(5)`) database to specify the timezone rules. To do this, set the :envvar:`TZ` environment variable to the path of the required timezone datafile, relative to the root of the systems 'zoneinfo' timezone database, usually located at :file:`/usr/share/zoneinfo`. For example, ``'US/Eastern'``, ``'Australia/Melbourne'``, ``'Egypt'`` or ``'Europe/Amsterdam'``. ::
These constants are used as parameters for :func:`clock_getres` and :func:`clock_gettime`.
The Solaris OS has a ``CLOCK_HIGHRES`` timer that attempts to use an optimal hardware source, and may give close to nanosecond resolution. ``CLOCK_HIGHRES`` is the nonadjustable, high-resolution clock.
The offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC (as in Western Europe, including the UK). Only use this if ``daylight`` is nonzero. See note below.
A tuple of two strings: the first is the name of the local non-DST timezone, the second is the name of the local DST timezone. If no DST timezone is defined, the second string should not be used. See note below.
For the above Timezone constants (:data:`altzone`, :data:`daylight`, :data:`timezone`, and :data:`tzname`), the value is determined by the timezone rules in effect at module load time or the last time :func:`tzset` is called and may be incorrect for times in the past. It is recommended to use the :attr:`~struct_time.tm_gmtoff` and :attr:`~struct_time.tm_zone` results from :func:`localtime` to obtain timezone information.
The use of ``%Z`` is now deprecated, but the ``%z`` escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries. Also, a strict reading of the original 1982 :rfc:`822` standard calls for a two-digit year (``%y`` rather than ``%Y``), but practice moved to 4-digit years long before the year 2000. After that, :rfc:`822` became obsolete and the 4-digit year has been first recommended by :rfc:`1123` and then mandated by :rfc:`2822`, with :rfc:`5322` continuing this requirement.