Ruby 时间#dst 是怎样的?
How does Ruby Time#dst?
也许是一个愚蠢的问题,表明我对夏令时的基本原理缺乏了解,但根据标题,Time.dst?
如何知道时间 object 是真还是假?
大概这必须通过日期和时区的组合来辨别,但这没有意义,因为时区中的低纬度不使用夏令时?因此肯定它必须需要位置来识别#dst? ?
我错过了什么?
Ruby 2.2.0 Time.dst?
method refers to the time_isdst(...)
function time.c
:
// ...
return tobj->vtm.isdst ? Qtrue : Qfalse
这似乎对应于 UNIX struct tm tm_isdt
member,经过大量修改后:
int tm_isdst daylight savings flag
在 time.c
中搜索 tm_isdst
和 isdst
以了解其计算方式。
为了处理时区和夏令时,Ruby 与其他所有内容一样,是 calling the localtime_r
C function. This puts the time in a C structure called tm
,其中包含一个名为 isdst
的字段。 Ruby 正在读取该标志。
localtime_r
首先通过从全局 tzname
变量获取您的时区来计算 isdst
。 tzname
是通过调用tzset
来确定的。 tzset
的工作方式取决于系统。它可以来自 TZ 环境变量、读取文件或查询 OS 服务。
例如。
# Time zone from the system.
$ ruby -e 'puts Time.now.zone; puts Time.now.dst?'
PDT
true
# Time zone from the TZ environment variable.
$ TZ='Australia/Brisbane' ruby -e 'puts Time.now.zone; puts Time.now.dst?'
AEST
false
有了时区后,localtime_r
可以从 GMT to the desired time zone (using the rules which applied on that date) using the "tz database" aka "tzdata" aka "zoneinfo" aka "the Olson database" after its creator Arthur David Olson. Previously a private effort, this is now maintained by IANA. It is a set of files installed on your system, or shipped with Ruby, which contains Far More Than Everything You Ever Want To Know About Time Zones and Daylight Savings.
转换
tz 数据库treats daylight savings (and other weird things like War and Peace time) as just another time zone. Time zone records are kept going all the way back as far as we've had time zones. Before we had time zones solar noon for that location is used. Because of these historical complications and shifting time zones, the tz database prefers to work with cities (such as "America/New York") 并为您确定时区。
time zone data files 包含广泛的评论和背景,如果您对日历的历史感兴趣,那将是一本引人入胜的读物。
也许是一个愚蠢的问题,表明我对夏令时的基本原理缺乏了解,但根据标题,Time.dst?
如何知道时间 object 是真还是假?
大概这必须通过日期和时区的组合来辨别,但这没有意义,因为时区中的低纬度不使用夏令时?因此肯定它必须需要位置来识别#dst? ?
我错过了什么?
Ruby 2.2.0 Time.dst?
method refers to the time_isdst(...)
function time.c
:
// ...
return tobj->vtm.isdst ? Qtrue : Qfalse
这似乎对应于 UNIX struct tm tm_isdt
member,经过大量修改后:
int tm_isdst daylight savings flag
在 time.c
中搜索 tm_isdst
和 isdst
以了解其计算方式。
为了处理时区和夏令时,Ruby 与其他所有内容一样,是 calling the localtime_r
C function. This puts the time in a C structure called tm
,其中包含一个名为 isdst
的字段。 Ruby 正在读取该标志。
localtime_r
首先通过从全局 tzname
变量获取您的时区来计算 isdst
。 tzname
是通过调用tzset
来确定的。 tzset
的工作方式取决于系统。它可以来自 TZ 环境变量、读取文件或查询 OS 服务。
例如。
# Time zone from the system.
$ ruby -e 'puts Time.now.zone; puts Time.now.dst?'
PDT
true
# Time zone from the TZ environment variable.
$ TZ='Australia/Brisbane' ruby -e 'puts Time.now.zone; puts Time.now.dst?'
AEST
false
有了时区后,localtime_r
可以从 GMT to the desired time zone (using the rules which applied on that date) using the "tz database" aka "tzdata" aka "zoneinfo" aka "the Olson database" after its creator Arthur David Olson. Previously a private effort, this is now maintained by IANA. It is a set of files installed on your system, or shipped with Ruby, which contains Far More Than Everything You Ever Want To Know About Time Zones and Daylight Savings.
tz 数据库treats daylight savings (and other weird things like War and Peace time) as just another time zone. Time zone records are kept going all the way back as far as we've had time zones. Before we had time zones solar noon for that location is used. Because of these historical complications and shifting time zones, the tz database prefers to work with cities (such as "America/New York") 并为您确定时区。
time zone data files 包含广泛的评论和背景,如果您对日历的历史感兴趣,那将是一本引人入胜的读物。