程式扎記: [Python Std Library] Generic Operating System Services : time — Time access and conversions

標籤

2012年6月25日 星期一

[Python Std Library] Generic Operating System Services : time — Time access and conversions


來源自 這裡
Preface :
This module provides various time-related functions. For related functionality, see also the datetime and calendar modules.

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.

Supported APIs :
The module defines the following functions and data items :
time.accept2dyear
Boolean value indicating whether two-digit year values will be accepted. This is true by default, but will be set to false if the environment variable PYTHONY2K has been set to a non-empty string. It may also be modified at run time.

time.altzone
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.

time.asctime([t])
Changed in version 2.1: Allowed t to be omitted.
Convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a 24-character string of the following form: 'Sun Jun 20 23:21:05 1993'. If t is not provided, the current time as returned by localtime() is used. Locale information is not used by asctime() :
>>> time.localtime()
time.struct_time(tm_year=2012, tm_mon=6, tm_mday=24, tm_hour=19, tm_min=15, tm_sec=8, tm_wday=6, tm_yday=176, tm_isdst=1)
>>> time.asctime()
'Sun Jun 24 19:15:10 2012'

time.clock()
On Unix, return the current processor time as a floating point number expressed in seconds.

time.ctime([secs])
Changed in version 2.1: Allowed secs to be omitted.
Changed in version 2.4: If secs is None, the current time is used.

Convert a time expressed in seconds since the epoch to a string representing local time. If secs is not provided or None, the current time as returned by time() is used. ctime(secs) is equivalent to asctime(localtime(secs)). Locale information is not used here.

time.daylight
Nonzero if a DST timezone is defined.

time.gmtime([secs])
Changed in version 2.1: Allowed secs to be omitted.
Changed in version 2.4: If secs is None, the current time is used.

Convert a time expressed in seconds since the epoch to a struct_time in UTC in which the dst flag is always zero. If secs is not provided or None, the current time as returned by time() is used. Fractions of a second are ignored. See above for a description of the struct_time object. See calendar.timegm() for the inverse of this function. Basic usage :
>>> time.gmtime(0) # 檢視 epoch time
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

time.localtime([secs])
Changed in version 2.1: Allowed secs to be omitted.
Changed in version 2.4: If secs is None, the current time is used.

Like gmtime() but converts to local time. If secs is not provided or None, the current time as returned by time() is used. The dst flag is set to 1 when DST applies to the given time. Basic usage :
>>> time.gmtime()
time.struct_time(tm_year=2012, tm_mon=6, tm_mday=25, tm_hour=5, tm_min=44, tm_sec=40, tm_wday=0, tm_yday=177, tm_isdst=0)
>>> time.localtime() # 因為我的時區是 GMT +8, 故 tm_hour = 5+8=13
time.struct_time(tm_year=2012, tm_mon=6, tm_mday=25, tm_hour=13, tm_min=44, tm_sec=45, tm_wday=0, tm_yday=177, tm_isdst=0)

time.mktime(t)
This is the inverse function of localtime(). Its argument is the struct_time or full 9-tuple (since the dst flag is needed; use -1 as the dst flag if it is unknown) whichexpresses the time in local time, not UTC. It returns a floating point number, for compatibility with time(). If the input value cannot be represented as a valid time, either OverflowError or ValueError will be raised (which depends on whether the invalid value is caught by Python or the underlying C libraries).

time.sleep(secs)
Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

time.strftime(format[, t])
Changed in version 2.1: Allowed t to be omitted.
Changed in version 2.4ValueError raised if a field in t is out of range.
Changed in version 2.5: 0 is now a legal argument for any position in the time tuple; if it is normally illegal the value is forced to a correct one.

Convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. If t is not provided, the current time as returned by localtime() is used. format must be a string. ValueError is raised if any field in t is outside of the allowed range.

The following directives can be embedded in the format string. They are shown without the optional field width and precision specification, and are replaced by the indicated characters in the strftime() result :


Here is an example, a format for dates compatible with that specified in the RFC 2822 Internet email standard :
>>> from time import gmtime, strftime, localtime
>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime()) # RFC 2822
'Mon, 25 Jun 2012 09:20:47'
>>> strftime("%Y/%m/%d %H:%M:%S", localtime()) # Customized format
'2012/06/25 17:22:59'

- class time.struct_time
New in version 2.2.
The type of the time value sequence returned by gmtime()localtime(), and strptime(). It is an object with a named tuple interface: values can be accessed by index and by attribute name. The following values are present :

time.time()
Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

time.timezone
The offset of the local (non-DST) timezone, in seconds west of UTC (negative in most of Western Europe, positive in the US, zero in the UK).
>>> time.mktime(time.gmtime())
1340588559.0
>>> time.mktime(time.localtime())+time.timezone # 會與 time.mktime(time.gmtime()) 相等!
1340588560.0

time.tzname
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.

Supplement :
Google 地圖時間及經緯度查詢
查詢世界各地的時間,請在 Google 地圖上移動滑鼠游標到您想要查詢的地點。城市的中英文名稱及當地時間、夏令時間就自動顯示在 Google 地圖上面,經緯度顯示在 Google 地圖的左下角...

[Linux 小學堂] 如何線上對 Server 時間進行校時 (netdate)

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!