我正在尝试以这样的方式应用一个cron作业,我需要获取3个国家的时间并检查是否是上午6点如果它是根据该国家所以特定服务应该运行。我正在使用任务调度程序为此每次只打印中国
my_task_schedular.rb
s = Rufus::Scheduler.new
s.every '1m' do
Tzone.each do |zone|
time = TZInfo::Timezone.get(zone)
time.now.strftime("%I:%M")
end
application.rb中
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Shivam
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Eastern Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.middleware.use ActionDispatch::Flash
end
end
答案 0 :(得分:0)
使用in_time_zone
方法获取适当的时间。在这一刻,我们有:
Time.now.in_time_zone
=> Wed, 30 Dec 2015 10:19:41 CET +01:00
另一个区域:
Time.now.in_time_zone('Asia/Kolkata')
=> Wed, 30 Dec 2015 14:50:03 IST +05:30
Time.now.in_time_zone('Eastern Time (US & Canada)')
=> Wed, 30 Dec 2015 04:20:31 EST -05:00
答案 1 :(得分:0)
因此,如果你需要检查是否在选定的时区上午6点,你可以使用它:
Time.now.in_time_zone('Asia/Kolkata').hour == 6
如documentation所述,小时将返回当天的小时(0..23)
请记住,Time.zone
on会显示您的计算机所在的当前时区,您也可以将Rails应用配置为具有时区,并且您的计算机具有完全不同的时区,但您可能遇到问题。数据源。