Ruby TimeZone毫秒自Midoch以来的午夜

时间:2017-09-29 15:40:33

标签: ruby datetime timezone

我需要从1970年开始以毫秒为单位,以今天的BST(英国夏令时间)偏移

a = DateTime.now
=> Fri, 29 Sep 2017 16:30:29 +0100

b = DateTime.new(a.year, a.month, a.day, 0, 0, 0, 0)
=> Fri, 29 Sep 2017 00:00:00 +0000   # this is an hour out  

b.strftime('%Q').to_i
1506643200000                        # therefore this is an hour out

我该如何纠正?

b = b.in_time_zone
=> Fri, 29 Sep 2017 01:00:00 BST +01:00  # looks promising but...

b.strftime('%Q').to_i
=> 0                                     # grrr

欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案:

a = DateTime.now
=> Fri, 29 Sep 2017 16:30:29 +0100

a = a.beginning_of_day
=> Fri, 29 Sep 2017 00:00:00 +0100

milliseconds_since_1970 = a.to_i * 1000
=> 1506639600000                     <-- correct!
相关问题