- Our machine's time zone.
# Check our system time zone.
Time.now.getlocal.zone
- Default: UTC
# Check our Rails time zone.
Time.zone.name
# List all the timezone names that Rails recognizes.
$ rake time:zones:all
# List all the timezone names that Rails recognizes.
ActiveSupport::TimeZone.all.map(&:name).sort
# List all the US timezone names that Rails recognizes.
ActiveSupport::TimeZone.us_zones.map(&:name).sort
class Application < Rails::Application
# We could override the Rails default time zone but in general it is not a good idea.
config.time_zone = 'Eastern Time (US & Canada)' # bad
end
# We can temporarily apply a given time zone to the Rails time zone.
Time.use_zone "UTC" do
# Do stuff with the specified time zone for the duration of this block.
end
Sets the time zone for displaying and interpreting time stamps. The built-in default is GMT, but that is typically overridden in postgresql.conf; initdb will install a setting there corresponding to its system environment. See Section 8.5.3 for more information.
- https://www.postgresql.org/docs/9.2/static/datatype-datetime.html#DATATYPE-TIMEZONES
- https://www.postgresql.org/docs/9.2/static/runtime-config-client.html#GUC-TIMEZONE
- In order to ensure consistency:
- always work with UTC
- always use the methods that use Rails application's time zone, and let them translate correct times
* Time.now
* Date.today
* Date.today.to_time
* Time.parse("2015-07-04 17:05:37")
* Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z")
* Time.current
* 2.hours.ago
* Time.zone.today
* Date.current
* 1.day.from_now
* Time.zone.parse("2015-07-04 17:05:37")
* Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone
time_zone = "Eastern Time (US & Canada)"
Time.use_zone(time_zone) do
create(:property_container, legacy_id: "1355")
instance = described_class.new(data_array)
expect(instance.created_at.iso8601).to eq("2018-09-25T08:18:24-04:00")
end
-
- By default, we always use UTC.
-
- Use the jstimezonedetect library to detect the user's time zone on the front end.
-
- Store the time zone name to a temporary cookie store.
-
- Apply that time zone to Rails time zone for the duration of each request.
= javascript_include_tag 'https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js'
javascript:
setTimezoneCookie()
function setTimezoneCookie() {
setCookie('tz', getTimezoneName())
function getTimezoneName() {
// https://medium.com/@jonathanabrams/be-aware-of-browsers-internationalization-api-db94bb32f9a8#.hjgg1453j
// Temporarily remove Intl object before calling jstz in order to ensure that jstz uses older timezone format.
// If the Intl object exists, jstz will detect a newer timezone format.
// We want to use older timezone format so that Rails can understand.
var oldIntl = window.Intl
window.Intl = undefined
// Get the usee's timezone name.
var tzName = jstz.determine().name()
// Restore the Intl object.
window.Intl = oldIntl
return tzName
}
function setCookie(key, value) {
// http://nithinbekal.com/posts/rails-user-timezones
var expires = new Date()
var currentTime = expires.getTime()
var duration = 24 * 60 * 60 * 1000
expires.setTime(currentTime + duration)
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString()
}
}
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :set_time_zone
private
def set_notifications
@notifications = Notification.where(recipient: current_user).recent
end
# Sets the time zone for the duration of a request.
# When the request completes, the original time zone is set back.
# https://robots.thoughtbot.com/its-about-time-zones
def set_time_zone(&block)
if cookies[:tz]
puts "*" * 60
puts "Time zone: #{cookies[:tz]}"
puts "*" * 60
Time.use_zone(cookies[:tz], &block)
else
# The around method must yield to execute the action.
# http://guides.rubyonrails.org/action_controller_overview.html#after-filters-and-around-filters
yield
end
end
end
- CDNJS - jstimezonedetect
- It's About Time (Zones) by Elle Meredith
- Rails: Automatically Set User Timezone During Signup by Nithin Bekal
- Be Aware of Browsers’ Internationalization API by Jon Abrams
- http://qiita.com/joker1007/items/2c277cca5bd50e4cce5e
- http://qiita.com/jnchito/items/cae89ee43c30f5d6fa2c
- http://qiita.com/hashakin/items/145aba9bda1b56507197
- http://guides.rubyonrails.org/action_controller_overview.html#after-filters-and-around-filters
The Problem with Time & Timezones - Computerphile
https://www.youtube.com/watch?v=-5wpm-gesOY