如何将转换为string的active support time对象转换回object,换句话说,如何从字符串中找到active support对象?
示例:
a = ActiveSupport::TimeZone.all.first =
#<ActiveSupport::TimeZone:0x007f8c45bc1848 @name="American Samoa",
@tzinfo=#<TZInfo::TimezoneProxy: Pacific/Pago_Pago>, @utc_offset=nil>如果我使用to_s将这个对象转换为一个字符串,我会得到"(GMT-11:00)美国萨摩亚“。
如果我有"(GMT-11:00)美属萨摩亚“,我如何找到物体?
发布于 2017-09-20 19:55:52
这将消除第一对括号之间的所有内容,并获取剩余的字符串:
a = ActiveSupport::TimeZone.all.first.to_s.match(/\(.*?\) (.*)/)[1]使用...and,您可以找到ActiveSupport::Timezone对象:
ActiveSupport::Timezone[a]
发布于 2017-09-20 19:44:15
# let
timezone_string = '(GMT-11:00) American Samoa'
# let's capture the "American Samoa" substring from above (as an example)
matches = timezone_string.match /\(GMT.*?\) (.*)/
timezone_name = matches[1]
# then we look up the corresponding Timezone object using the "American Samoa" timezone_name
timezone = ActiveSupport::TimeZone[timezone_name]发布于 2017-09-22 17:23:16
谢谢你的回答,我确实试过了,而且很管用。我也试过这个
tz_value = business_timezone.split(')').second.strip 它为我提供了名称,并且我正在使用
ActiveSupport::TimeZone[tz_value].https://stackoverflow.com/questions/46320188
复制相似问题