我有一个cronjob正在运行,每次运行任务时我想从我的数据库加载联系人,其中该人的当地小时是我的cron系统中的指定小时。
例如:在当前下午4点(第16小时)的时区给我所有联系人。
为了实现这一点,我想创建一个简单的函数,返回当前小时为X的UTC timezone offset。
这是我提出的最佳解决方案,但我认为有更高效的方法。
def timezone_offset_for_hour hour
["-12:00","-11:00","-10:00", "-09:30","-09:00","-08:00","-07:00","-06:00","-05:00","-04:00","-03:30",
"-03:00","-02:00","-01:00","+00:00","+01:00","+02:00","+03:00","+03:30","+04:00","+04:30","+05:00","+05:30",
"+05:45","+06:00","+06:30","+07:00","+08:00","+08:30","+08:45","+09:00","+09:30","+10:00","+10:30",
"+11:00","+12:00","+12:45","+13:00","+14:00"].select{ |o| Time.now.getlocal(o).hour == hour }
end
解决方案可以是纯红宝石,也可以包含Rails方法。
答案 0 :(得分:1)
如果您需要数字偏移:
offsets = ActiveSupport::TimeZone.all.map{ |t| t.utc_offset / 3600.0 }.uniq
如果您需要字符串表示:
offset_strs = ActiveSupport::TimeZone.all.map(&:formatted_offset).uniq