gpt4 book ai didi

ruby - Ruby Time#dst 如何运行?

转载 作者:数据小太阳 更新时间:2023-10-29 07:06:32 29 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,表明我对夏令时的基础知识缺乏了解,但根据标题,Time.dst? 如何知道时间对象确实是真还是假?

大概这必须通过日期和时区的组合来辨别,但这没有意义,因为时区中的低纬度不使用夏令时?因此肯定它必须需要位置来识别#dst? ?

我错过了什么?

最佳答案

为了处理时区和夏令时,Ruby 和其他所有东西一样,是 calling localtime_r C function .这将时间放入名为 tm 的 C 结构中其中包括一个名为 isdst 的字段。 Ruby 正在读取该标志。

localtime_r 首先通过从全局 tzname 变量获取您的时区来计算 isdsttzname 通过调用 tzset 确定. tzset 的工作方式取决于系统。它可以来自 TZ 环境变量、读取文件或查询操作系统服务。

例如。

# 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 转换过来使用 "tz database" 到所需的时区(使用该日期适用的规则)又名“tzdata”,又名“zoneinfo”,又名“Olson 数据库”,以其创建者 Arthur David Olson 命名。以前是私有(private)努力,现在由 IANA 维护.它是一组安装在您的系统上或随 Ruby 一起提供的文件,其中包含 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 .只要我们有时区,时区记录就会一直保持下去。在我们有时区之前solar noon使用该位置。由于这些历史复杂性和时区变化,tz 数据库更喜欢 work with cities (such as "America/New York")并为您确定时区。

time zone data files包含广泛的评论和背景,如果您对日历的历史感兴趣,那将是一本引人入胜的读物。

关于ruby - Ruby Time#dst 如何运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32684906/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com