python 从哪里得到本地时区?
Where does python get the local timezone from?
>>> import time
>>> time.tzname
('GMT', 'BST')
Python 从哪里获得此信息(本地时区)?是否有 returns 这个系统调用?我看到一些消息来源提到 /etc/timezone
文件,而其他消息来源提到 TZ
环境变量。
我看到 Python 支持 TZ
环境变量,如果设置:
TZ="Asia/Calcutta" python -c "import time; print(time.tzname)"
('IST', 'IST')
我的 shell 默认情况下没有这个设置,所以我很好奇当 TZ
环境变量不存在时如何读取本地时区。
这取决于您使用的平台。如果你在 windows,你的假设是正确的,它使 GetTimeZoneInformation
system call to create the time.tzname
tuple. Here is 成为 C
代码的相关部分。
#ifdef MS_WINDOWS
TIME_ZONE_INFORMATION tzinfo = {0};
GetTimeZoneInformation(&tzinfo);
otz0 = PyUnicode_FromWideChar(tzinfo.StandardName, -1);
if (otz0 == NULL) {
return -1;
}
otz1 = PyUnicode_FromWideChar(tzinfo.DaylightName, -1);
if (otz1 == NULL) {
Py_DECREF(otz0);
return -1;
}
#else
// Skipped for brevity
#endif // MS_WINDOWS
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
if (tzname_obj == NULL) {
return -1;
}
PyModule_AddObject(m, "tzname", tzname_obj);
但是与其他平台一样,它完全取决于 tzname
variable which is initialized by the tzset()
function in the C
standard library。
The tzset()
function initializes the tzname
variable from the TZ
environment variable. This function is automatically called by the
other time conversion functions that depend on the timezone. In a
System-V-like environment, it will also set the variables timezone
(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).
If the TZ
variable does not appear in the environment, the system
timezone is used. The system timezone is configured by copying, or
linking, a file in the tzfile(5) format to /etc/localtime
. A timezone
database of these files may be located in the system timezone
directory (see the FILES section below).
If the TZ
variable does appear in the environment, but its value is
empty, or its value cannot be interpreted using any of the formats
specified below, then Coordinated Universal Time (UTC) is used.
>>> import time
>>> time.tzname
('GMT', 'BST')
Python 从哪里获得此信息(本地时区)?是否有 returns 这个系统调用?我看到一些消息来源提到 /etc/timezone
文件,而其他消息来源提到 TZ
环境变量。
我看到 Python 支持 TZ
环境变量,如果设置:
TZ="Asia/Calcutta" python -c "import time; print(time.tzname)"
('IST', 'IST')
我的 shell 默认情况下没有这个设置,所以我很好奇当 TZ
环境变量不存在时如何读取本地时区。
这取决于您使用的平台。如果你在 windows,你的假设是正确的,它使 GetTimeZoneInformation
system call to create the time.tzname
tuple. Here is 成为 C
代码的相关部分。
#ifdef MS_WINDOWS TIME_ZONE_INFORMATION tzinfo = {0}; GetTimeZoneInformation(&tzinfo); otz0 = PyUnicode_FromWideChar(tzinfo.StandardName, -1); if (otz0 == NULL) { return -1; } otz1 = PyUnicode_FromWideChar(tzinfo.DaylightName, -1); if (otz1 == NULL) { Py_DECREF(otz0); return -1; } #else // Skipped for brevity #endif // MS_WINDOWS PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1); if (tzname_obj == NULL) { return -1; } PyModule_AddObject(m, "tzname", tzname_obj);
但是与其他平台一样,它完全取决于 tzname
variable which is initialized by the tzset()
function in the C
standard library。
The
tzset()
function initializes thetzname
variable from theTZ
environment variable. This function is automatically called by the other time conversion functions that depend on the timezone. In a System-V-like environment, it will also set the variables timezone (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).If the
TZ
variable does not appear in the environment, the system timezone is used. The system timezone is configured by copying, or linking, a file in the tzfile(5) format to/etc/localtime
. A timezone database of these files may be located in the system timezone directory (see the FILES section below).If the
TZ
variable does appear in the environment, but its value is empty, or its value cannot be interpreted using any of the formats specified below, then Coordinated Universal Time (UTC) is used.