据我了解,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
Run Code Online (Sandbox Code Playgroud)
恕我直言,a.utc?应返回true。有什么解释吗?
另外:来自Time#utc 的Ruby文档?
如果时间表示以UTC(GMT)表示的时间,则返回true 。
“在UTC / GMT中表示时间”到底是什么意思?显然,偏移量0是不够的。
在实现方面,Ruby的(即MRI)内部时间结构具有一个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;
});
Run Code Online (Sandbox Code Playgroud)
该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
Run Code Online (Sandbox Code Playgroud)
与通过utc:创建的时间实例相反(请注意,偏移量显示为UTC)
Time.utc(2018) #=> 2018-01-01 00:00:00 UTC
Time.utc(2018).utc? #=> true
Run Code Online (Sandbox Code Playgroud)
您可以utc_offset改为查看:
t = Time.new(2018) #=> 2018-01-01 00:00:00 +0000
t.utc_offset #=> 0
t.utc_offset.zero? #=> true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |