Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{time}} |
| 2 | |
| 3 | \bimodindex{time} |
| 4 | This module provides various time-related functions. |
| 5 | It is always available. (On some systems, not all functions may |
| 6 | exist; e.g. the ``milli'' variants can't always be implemented.) |
| 7 | |
| 8 | An explanation of some terminology and conventions is in order. |
| 9 | |
| 10 | \begin{itemize} |
| 11 | |
| 12 | \item |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 13 | The ``epoch'' is the point where the time starts. On January 1st of that |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 14 | year, at 0 hours, the ``time since the epoch'' is zero. For UNIX, the |
| 15 | epoch is 1970. To find out what the epoch is, look at the first |
| 16 | element of \code{gmtime(0)}. |
| 17 | |
| 18 | \item |
| 19 | UTC is Coordinated Universal Time (formerly known as Greenwich Mean |
| 20 | Time). The acronym UTC is not a mistake but a compromise between |
| 21 | English and French. |
| 22 | |
| 23 | \item |
| 24 | DST is Daylight Saving Time, an adjustment of the timezone by |
| 25 | (usually) one hour during part of the year. DST rules are magic |
| 26 | (determined by local law) and can change from year to year. The C |
| 27 | library has a table containing the local rules (often it is read from |
| 28 | a system file for flexibility) and is the only source of True Wisdom |
| 29 | in this respect. |
| 30 | |
| 31 | \item |
| 32 | The precision of the various real-time functions may be less than |
| 33 | suggested by the units in which their value or argument is expressed. |
| 34 | E.g. on most UNIX systems, the clock ``ticks'' only every 1/50th or |
| 35 | 1/100th of a second, and on the Mac, it ticks 60 times a second. |
| 36 | |
| 37 | \end{itemize} |
| 38 | |
| 39 | Functions and data items are: |
| 40 | |
| 41 | \renewcommand{\indexsubitem}{(in module time)} |
| 42 | |
| 43 | \begin{datadesc}{altzone} |
| 44 | The offset of the local DST timezone, in seconds west of the 0th |
| 45 | meridian, if one is defined. Only use this if \code{daylight} is |
| 46 | nonzero. |
| 47 | \end{datadesc} |
| 48 | |
| 49 | |
| 50 | \begin{funcdesc}{asctime}{tuple} |
| 51 | Convert a tuple representing a time as returned by \code{gmtime()} or |
| 52 | \code{localtime()} to a 24-character string of the following form: |
| 53 | \code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of |
| 54 | the same name, there is no trailing newline. |
| 55 | \end{funcdesc} |
| 56 | |
| 57 | |
| 58 | \begin{funcdesc}{ctime}{secs} |
| 59 | Convert a time expressed in seconds since the epoch to a string |
| 60 | representing local time. \code{ctime(t)} is equivalent to |
| 61 | \code{asctime(localtime(t))}. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{datadesc}{daylight} |
| 65 | Nonzero if a DST timezone is defined. |
| 66 | \end{datadesc} |
| 67 | |
| 68 | \begin{funcdesc}{gmtime}{secs} |
| 69 | Convert a time expressed in seconds since the epoch to a tuple of 9 |
| 70 | integers, in UTC: year (e.g. 1993), month (1-12), day (1-31), hour |
| 71 | (0-23), minute (0-59), second (0-59), weekday (0-6, monday is 0), |
| 72 | julian day (1-366), dst flag (always zero). Fractions of a second are |
| 73 | ignored. Note subtle differences with the C function of this name. |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | \begin{funcdesc}{localtime}{secs} |
| 77 | Like \code{gmtime} but converts to local time. The dst flag is set |
| 78 | to 1 when DST applies to the given time. |
| 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{millisleep}{msecs} |
| 82 | Suspend execution for the given number of milliseconds. (Obsolete, |
| 83 | you can now use use \code{sleep} with a floating point argument.) |
| 84 | \end{funcdesc} |
| 85 | |
| 86 | \begin{funcdesc}{millitimer}{} |
| 87 | Return the number of milliseconds of real time elapsed since some |
| 88 | point in the past that is fixed per execution of the python |
| 89 | interpreter (but may change in each following run). The return value |
| 90 | may be negative, and it may wrap around. |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{funcdesc}{mktime}{tuple} |
| 94 | This is the inverse function of \code{localtime}. Its argument is the |
| 95 | full 9-tuple (since the dst flag is needed). It returns an integer. |
| 96 | \end{funcdesc} |
| 97 | |
| 98 | \begin{funcdesc}{sleep}{secs} |
| 99 | Suspend execution for the given number of seconds. The argument may |
| 100 | be a floating point number to indicate a more precise sleep time. |
| 101 | \end{funcdesc} |
| 102 | |
| 103 | \begin{funcdesc}{time}{} |
| 104 | Return the time as a floating point number expressed in seconds since |
| 105 | the epoch, in UTC. Note that even though the time is always returned |
| 106 | as a floating point number, not all systems provide time with a better |
| 107 | precision than 1 second. An alternative for measuring precise |
| 108 | intervals is \code{millitimer}. |
| 109 | \end{funcdesc} |
| 110 | |
| 111 | \begin{datadesc}{timezone} |
| 112 | The offset of the local (non-DST) timezone, in seconds west of the 0th |
| 113 | meridian (i.e. negative in most of Western Europe, positive in the US, |
| 114 | zero in the UK). |
| 115 | \end{datadesc} |
| 116 | |
| 117 | \begin{datadesc}{tzname} |
| 118 | A tuple of two strings: the first is the name of the local non-DST |
| 119 | timezone, the second is the name of the local DST timezone. If no DST |
| 120 | timezone is defined, the second string should not be used. |
| 121 | \end{datadesc} |