---
title: "Rails 6 adds support to persist timezones of Active Job"
description: "Rails 6 now records current timezone of Active Job"
canonical_url: "https://www.bigbinary.com/blog/rails-6-add-timezone-support-in-active-job"
markdown_url: "https://www.bigbinary.com/blog/rails-6-add-timezone-support-in-active-job.md"
---

# Rails 6 adds support to persist timezones of Active Job

Rails 6 now records current timezone of Active Job

- Author: Chetan Gawai
- Published: September 1, 2020
- Categories: Rails 6, Rails

When a job is enqueued in Rails 6 using Active Job, the current timezone of a
job is [preserved](https://github.com/rails/rails/pull/32085/) and then this
preserved timezone is restored when the job is finished executing.

Let's take an example of sale at Amazon.

Amazon would like to remind users across different timezones about its upcoming
sale by sending an email. This task of sending a reminder would be processed as
a background job.

<b>Before:</b>

Before Rails 6, we had to pass timezone explicitly to the `perform` method of
the job as shown below.

```ruby
timezone = "Eastern Time (US & Canada)"

AmazonSaleJob.perform_later(Time.now, timezone)

class AmazonSaleJob < ApplicationJob
  queue_as :default

  def perform(time, timezone)

    time = time.in_time_zone(timezone)
    sale_start_time = localtime(2020, 12, 24)

    if time >= sale_start_time
      puts "Sale has started!"
      #Send an email stating Sale has started
    else
      sale_starts_in = (sale_start_time - time).div(3600)
      puts "Hang on! Sale will start in #{sale_starts_in} hours"
      #Send an email stating sales starts in sale_starts_in hours
     end
  end

  private

    def localtime(*args)
      Time.zone ? Time.zone.local(*args) : Time.utc(*args)
    end
end

```

<b>After:</b>

After the changes in Rails 6, passing timezone to Job is now taken care of by
Rails.

```ruby
timezone = "Eastern Time (US & Canada)"

Time.use_zone(timezone) do
  AmazonSaleJob.perform_later(Time.zone.now)
end

class AmazonSaleJob < ApplicationJob
  queue_as :default

  def perform(time)
    sale_start_time = localtime(2020, 12, 24)

    if time >= sale_start_time
      puts "Sale has started!"
      #Send an email stating Sale has started
    else
      sale_starts_in = (sale_start_time - time).div(3600)
      puts "Hang on! Sale will start in #{sale_starts_in} hours"
      #Send an email stating sales starts in sale_starts_in hours
     end
   end

  private

    def localtime(*args)
      Time.zone ? Time.zone.local(*args) : Time.utc(*args)
    end
end

```

Rails 6 also propagates timezone to all the subsequent nested jobs.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-add-timezone-support-in-active-job)
