Ruby on rails 4 在美国午夜,在rails中安排“每当”任务的最佳方式是什么?

Ruby on rails 4 在美国午夜,在rails中安排“每当”任务的最佳方式是什么?,ruby-on-rails-4,cron,rake-task,whenever,Ruby On Rails 4,Cron,Rake Task,Whenever,我们有一个Rails 4应用程序。 在美国午夜使用夏令时,在rails中安排任务的最佳方式是什么 我们需要在一天报告的晚上11:58发送电子邮件 我们正在使用tzinfogem TZInfo::Timezone.get('America/Denver').local_to_utc(Time.parse('11:58pm')).strftime('%H:%M%p') 此时不发送电子邮件。这解决了时区问题,服务器在UTC,用户在另一个时区(夏令时)。定义本地操作并在cronjob中使用 附表1.r

我们有一个Rails 4应用程序。 在美国午夜使用夏令时,在rails中安排任务的最佳方式是什么

我们需要在一天报告的晚上11:58发送电子邮件

我们正在使用
tzinfo
gem

TZInfo::Timezone.get('America/Denver').local_to_utc(Time.parse('11:58pm')).strftime('%H:%M%p')

此时不发送电子邮件。

这解决了时区问题,服务器在UTC,用户在另一个时区(夏令时)。定义本地操作并在cronjob中使用

附表1.rb
希望能对你有所帮助。

热汉的回答太棒了!在我的用例中,我遇到了一个问题,时区转换也改变了任务计划的星期几

也许有一个更简单的方法,但这就是我所做的。 我们需要的时区转换只会使工作日提前。 如果您的用例要求撤销工作日,那么您需要编辑它,但这应该是一个简单的解决方案

def local(time, est_weekday = nil)
  days = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
  local_time = Time.parse(time)
  utc_time = TZInfo::Timezone.get('America/New_York').local_to_utc(local_time)
  utc_time_formatted = utc_time.strftime("%I:%M %p")
  if est_weekday && days.include?(est_weekday.downcase.to_sym)
    #extract intended wday for ruby datetime and assign
    weekday_index = days.index(est_weekday.downcase.to_sym)
    #get placeholder wday from desired EST day/time
    temp_est_weekday_index = local_time.wday
    #get placeholder wday from adjusted UTC day/time
    temp_utc_weekday_index = utc_time.wday
    #has the conversion to UTC advanced the wday?
    weekday_advances = temp_utc_weekday_index != temp_est_weekday_index
    #adjust wday index if timezone conversion has advanced weekday
    weekday_index += 1 if weekday_advances
    weekday = days[weekday_index]
    return {time: utc_time_formatted, day: weekday || nil }
  else
    return utc_time_formatted
  end
end

这类任务的良好全球工作环境。已添加书签!!:)
def local(time, est_weekday = nil)
  days = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
  local_time = Time.parse(time)
  utc_time = TZInfo::Timezone.get('America/New_York').local_to_utc(local_time)
  utc_time_formatted = utc_time.strftime("%I:%M %p")
  if est_weekday && days.include?(est_weekday.downcase.to_sym)
    #extract intended wday for ruby datetime and assign
    weekday_index = days.index(est_weekday.downcase.to_sym)
    #get placeholder wday from desired EST day/time
    temp_est_weekday_index = local_time.wday
    #get placeholder wday from adjusted UTC day/time
    temp_utc_weekday_index = utc_time.wday
    #has the conversion to UTC advanced the wday?
    weekday_advances = temp_utc_weekday_index != temp_est_weekday_index
    #adjust wday index if timezone conversion has advanced weekday
    weekday_index += 1 if weekday_advances
    weekday = days[weekday_index]
    return {time: utc_time_formatted, day: weekday || nil }
  else
    return utc_time_formatted
  end
end