Python Tips
Time , Timezone
The time module provides various time-related functions. For related functionality, see also the datetime and calendar modules.
An explanation of some terminology :
- epoch the point where the time starts, and is platform dependent. For Unix, the epoch is January 1, 1970, 00:00:00 (UTC). Use time.gmtime(0) to get the epoch on a platform.
- seconds since the epoch the total number of elapsed seconds since the epoch
- UTC ( Coordinated Universal Time ) Greenwich Mean Time, or GMT.
- DST ( Daylight Saving Time ) DST rules are magic (determined by local law)
- the precision of time() and sleep() time() returns the most accurate time available (using Unix gettimeofday() where available), and sleep() will accept a time with a nonzero fraction (Unix select() is used to implement this, where available).
- Use the following functions to convert between time representations:
- time.gmtime([secs]) 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.
- time.localtime([secs] 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.
From | To | Use |
---|---|---|
seconds since the epoch | struct_time in UTC | gmtime() |
seconds since the epoch | struct_time in local time | localtime() |
struct_time in UTC | seconds since the epoch | calendar.timegm() |
struct_time in local time | seconds since the epoch | mktime() |
>>> time.gmtime()
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=22, tm_hour=6, tm_min=24, tm_sec=20, tm_wday=5, tm_yday=53, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=22, tm_hour=14, tm_min=24, tm_sec=20, tm_wday=5, tm_yday=53, tm_isdst=0)
platform — Access to underlying platform’s identifying data
import platform | Ubuntu 18.04 | Raspbian Buster Lite |
---|---|---|
platform.platform() | 'Linux-5.3.0-40-generic-x86_64-with-Ubuntu-18.04-bionic' | 'Linux-4.19.97+-armv6l-with-debian-10.3' |
platform.system() | 'Linux' | 'Linux' |
platform.release() | '5.3.0-40-generic' | '4.19.97+' |
platform.machine() | 'x86_64' | 'armv6l' |
platform.version() | '#32~18.04.1-Ubuntu SMP Mon Feb 3 14:05:59 UTC 2020' | '#1294 Thu Jan 30 13:10:54 GMT 2020' |
syslog — Unix syslog library routines
This module provides an interface to the Unix syslog library routines.
syslog(priority, format) generates a log message, which will be distributed by syslogd(8).
The priority argument is formed by ORing together a facility(a default of LOG_USER) value and a level(a default of LOG_INFO) value.
- facility The facility argument is used to specify what type of program is logging the message.
- level
LOG_EMERG
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
留言