在Rails中,如何以一种可与ActiveSupport::TimeZone[zone].parse()一起使用的格式获取当前时区(time .zone) ?

时间:2021-03-03 15:22:19

How can a method take the current Time.zone and put it into a format that is usable by ActiveSupport::TimeZone[some_zone].parse()?

一个方法如何利用当前时间。使用ActiveSupport::时区[some_zone].parse()?

It seems very strange that Time.zone.to_s returns a string that can not be used with ActiveSupport::TimeZone[zone].parse()

那时候看起来很奇怪。to_s返回不能与ActiveSupport::TimeZone[zone].parse()一起使用的字符串

Time.zone.to_s returns "(GMT-08:00) Pacific Time (US & Canada)"

Time.zone。to_s return (GMT-08:00)太平洋时间(美国和加拿大)

But ActiveSupport::TimeZone["(GMT-08:00) Pacific Time (US & Canada)"] is nil.

太平洋时间(美国和加拿大)是零。

ActiveSupport::TimeZone["(GMT-08:00) Pacific Time (US & Canada)"]
=> nil

ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
=> (GMT-08:00) Pacific Time (US & Canada)

1 个解决方案

#1


38  

Use Time.zone.name, not Time.zone.to_s

使用Time.zone.name,不是Time.zone.to_s

[1] pry(main)> Time.zone.to_s
=> "(GMT-05:00) Eastern Time (US & Canada)"
[2] pry(main)> Time.zone.name
=> "Eastern Time (US & Canada)"
[3] pry(main)> ActiveSupport::TimeZone[Time.zone.name]
=> (GMT-05:00) Eastern Time (US & Canada)

As for how I got this (as requested), I just know the name method exists on Time.zone. If I didn't know this by heart though, I will check the docs. If it's not in there as you say (and it is, here), I typically inspect the class/module/object with Pry. Pry is an alternative to irb that lets me do something like

至于我如何得到这个(按要求),我只知道名称方法存在于时间。zone。如果我不知道,我会去检查医生。如果它不像您说的那样(在这里),我通常使用Pry检查类/模块/对象。Pry是irb的另一种选择它可以让我做类似的事情

[1] pry(main)> cd Time.zone
[2] pry(#<ActiveSupport::TimeZone>):1> ls -m
Comparable#methods: <  <=  ==  >  >=  between?
ActiveSupport::TimeZone#methods: <=>  =~  at  formatted_offset  local  local_to_utc  name  now  parse  period_for_local  period_for_utc  to_s  today  tzinfo  utc_offset  utc_to_local
self.methods: __pry__
[3] pry(#<ActiveSupport::TimeZone>):1> name
=> "Eastern Time (US & Canada)"

ls -m on line [2] above prints methods on the object (if you scroll right you'll see name listed there). You can see in [3] I can call name directly on the Time.zone object I'm inside of and get the output you're looking for.

ls -m在[2]上,在对象上打印方法(如果您右滚动,您将看到在那里列出的名称)。您可以在[3]中看到,我可以直接在时间上调用名称。我所在的区域对象并获取您要查找的输出。

#1


38  

Use Time.zone.name, not Time.zone.to_s

使用Time.zone.name,不是Time.zone.to_s

[1] pry(main)> Time.zone.to_s
=> "(GMT-05:00) Eastern Time (US & Canada)"
[2] pry(main)> Time.zone.name
=> "Eastern Time (US & Canada)"
[3] pry(main)> ActiveSupport::TimeZone[Time.zone.name]
=> (GMT-05:00) Eastern Time (US & Canada)

As for how I got this (as requested), I just know the name method exists on Time.zone. If I didn't know this by heart though, I will check the docs. If it's not in there as you say (and it is, here), I typically inspect the class/module/object with Pry. Pry is an alternative to irb that lets me do something like

至于我如何得到这个(按要求),我只知道名称方法存在于时间。zone。如果我不知道,我会去检查医生。如果它不像您说的那样(在这里),我通常使用Pry检查类/模块/对象。Pry是irb的另一种选择它可以让我做类似的事情

[1] pry(main)> cd Time.zone
[2] pry(#<ActiveSupport::TimeZone>):1> ls -m
Comparable#methods: <  <=  ==  >  >=  between?
ActiveSupport::TimeZone#methods: <=>  =~  at  formatted_offset  local  local_to_utc  name  now  parse  period_for_local  period_for_utc  to_s  today  tzinfo  utc_offset  utc_to_local
self.methods: __pry__
[3] pry(#<ActiveSupport::TimeZone>):1> name
=> "Eastern Time (US & Canada)"

ls -m on line [2] above prints methods on the object (if you scroll right you'll see name listed there). You can see in [3] I can call name directly on the Time.zone object I'm inside of and get the output you're looking for.

ls -m在[2]上,在对象上打印方法(如果您右滚动,您将看到在那里列出的名称)。您可以在[3]中看到,我可以直接在时间上调用名称。我所在的区域对象并获取您要查找的输出。