了解时间#utc?
Understanding Time#utc?
据我了解,UTC 表示时间在时区 +00:00 中给出。但是 Ruby 在 Time#utc?
中的想法不同。我在 Ruby 2.5.1:
中观察到了这一点
a = Time.new(2018,6,13, 9,0,0, '+00:00')
# => 2018-06-13 09:00:00 +0000
b = Time.utc(2018,6,13, 9,0,0)
# => 2018-06-13 09:00:00 UTC
a == b
# => true
a.utc?
# => false (WHY???)
b.utc?
# => true
恕我直言,a.utc?
应该 return 正确。有什么解释吗?
添加: 来自Ruby docs for Time#utc?
Returns true if time represents a time in UTC (GMT).
"representing a time in UTC/GMT" 究竟是什么意思?显然,偏移量为 0 是不够的。
在实现方面,Ruby 的(即 MRI)内部 time structure 有一个 gmt
字段指定时间的类型:
PACKED_STRUCT_UNALIGNED(struct time_object {
wideval_t timew; /* time_t value * TIME_SCALE. possibly Rational. */
struct vtm vtm;
uint8_t gmt:3; /* 0:localtime 1:utc 2:fixoff 3:init */
uint8_t tm_got:1;
});
utc?
方法仅检查 gmt
是否为 1
。
因此,本地时间的时间实例或具有显式偏移的时间实例永远不会是 utc?
,即使您系统的时区偏移是 UTC+0:
Time.local(2018) #=> 2018-01-01 00:00:00 +0000
Time.local(2018).utc? #=> false
Time.new(2018) #=> 2018-01-01 00:00:00 +0000
Time.new(2018).utc? #=> false
与通过 utc
创建的时间实例相反:(请注意偏移显示为 UTC
)
Time.utc(2018) #=> 2018-01-01 00:00:00 UTC
Time.utc(2018).utc? #=> true
您可以改为检查 utc_offset
:
t = Time.new(2018) #=> 2018-01-01 00:00:00 +0000
t.utc_offset #=> 0
t.utc_offset.zero? #=> true
据我了解,UTC 表示时间在时区 +00:00 中给出。但是 Ruby 在 Time#utc?
中的想法不同。我在 Ruby 2.5.1:
a = Time.new(2018,6,13, 9,0,0, '+00:00')
# => 2018-06-13 09:00:00 +0000
b = Time.utc(2018,6,13, 9,0,0)
# => 2018-06-13 09:00:00 UTC
a == b
# => true
a.utc?
# => false (WHY???)
b.utc?
# => true
恕我直言,a.utc?
应该 return 正确。有什么解释吗?
添加: 来自Ruby docs for Time#utc?
Returns true if time represents a time in UTC (GMT).
"representing a time in UTC/GMT" 究竟是什么意思?显然,偏移量为 0 是不够的。
在实现方面,Ruby 的(即 MRI)内部 time structure 有一个 gmt
字段指定时间的类型:
PACKED_STRUCT_UNALIGNED(struct time_object {
wideval_t timew; /* time_t value * TIME_SCALE. possibly Rational. */
struct vtm vtm;
uint8_t gmt:3; /* 0:localtime 1:utc 2:fixoff 3:init */
uint8_t tm_got:1;
});
utc?
方法仅检查 gmt
是否为 1
。
因此,本地时间的时间实例或具有显式偏移的时间实例永远不会是 utc?
,即使您系统的时区偏移是 UTC+0:
Time.local(2018) #=> 2018-01-01 00:00:00 +0000
Time.local(2018).utc? #=> false
Time.new(2018) #=> 2018-01-01 00:00:00 +0000
Time.new(2018).utc? #=> false
与通过 utc
创建的时间实例相反:(请注意偏移显示为 UTC
)
Time.utc(2018) #=> 2018-01-01 00:00:00 UTC
Time.utc(2018).utc? #=> true
您可以改为检查 utc_offset
:
t = Time.new(2018) #=> 2018-01-01 00:00:00 +0000
t.utc_offset #=> 0
t.utc_offset.zero? #=> true