Make Rails ignore daylight saving time when displaying a date
Create a helper that utilizes the dst?
method on TimeZone
to check whether the passed timezone is currently in DST. If it is, then subtract an hour from the supplied DateTime
instance:
# helper function
module TimeConversion
def no_dst(datetime, timezone)
Time.zone = timezone
if Time.zone.now.dst?
return datetime - 1.hour
end
return datetime
end
end
Then, render the adjusted (or non-adjusted) time in your view:
# in your view
<%= no_dst(DateTime.parse("2013-08-26T00:00:00Z"), 'Eastern Time (US & Canada)') %>
#=> Sun, 25 Aug 2013 19:00:00 EDT -04:00
Rails ignore daylight savings time
I don't mean to sound pedantic, but...
I want to completely ignore daylight savings time.
Well, you will be on your own then. Despite our best hopes and wishes, much of the real world uses daylight saving time. You can get a quick primer here.
If an event starts at 5:30pm, it always starts at 5:30pm whether daylight savings time is in effect or not.
5:30 for who? If you're saying 5:30 UTC, then sure. But if you're saying 5:30 in US Central Time, then you have to take DST into account. Otherwise, half of the year people will show up at your event at what they think is 5:30 and you think is 6:30.
Is it possible, considering all times are stored uniformly, to retrieve database times and display them locally so that daylight savings is completely ignored?
You're storing the times in UTC, which is good. When you display them locally, you should not ignore DST.
Are there any problems I am going to run into ignoring daylight savings?
Yes, people don't commonly understand this. It's generally expected that if you refer to a local time, that you mean a time that is local for them. If you don't include DST in that calculation, then you will have a disagreement about what time you are talking about.
Another word of advice, you might want to consider using the TZInfo gem instead of ActiveSupport::TimeZone
. Then you would store time zone selection using the IANA identifiers such as America/Chicago
. These are recognizable outside of Rails.
For some unexplained reason, the ActiveSupport folks thought they should limit time zones to the 146 values they felt were "meaningful". But they didn't explain their process, and they don't seem to be on top of maintenance. I've asked why, but haven't gotten much of a detailed response.
You may also wish to review the timezone tag wiki.
Ignore DST on Rails Timzone
ActiveSupport::TimeWithZone
has a useful dst?
method to check if the time is within Daylight Savings Time for the specified time zone:
Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
Time.zone.parse("2012-5-30").dst? # => true
Time.zone.parse("2012-11-30").dst? # => false
You can read more at http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-dst-3F
So you can just check if time is within DST, or extend TimeWithZone
, as explained here Make Rails ignore daylight saving time when displaying a date by @zeantsoi:
class ActiveSupport::TimeWithZone
def no_dst
self.dst? ? self - 1.hour : self
end
end
and use it this way always getting the time without DST:
time.in_time_zone("Eastern Time (US & Canada)").no_dst
Daylight Savings Time ignored using in_time_zone Rails 4
It is showing CST simply because the time is read from the database including the stored date, i.e. it's read as 09:00 of Jan 1st 2000.
I guess you'd have to parse the time upon reading the attribute back. You can use a helper method in your model, for example:
# schedule model
def start_in_zone(zone)
self.start.strftime("%H:%M").in_time_zone(zone)
end
This will take only the hours and minutes part of the stored time and parse it in the given time zone with the date set to today. See this example:
"Sat, 01 Jan 2000 08:00:00".to_time.
strftime("%H:%M").
in_time_zone("Central Time (US & Canada)")
# => Tue, 12 Apr 2016 08:00:00 CDT -05:00
Rails timezone and Daylight saving time
7 months after you asked, but perhaps skip_time_zone_conversion_for_attributes=
will help - it tells AcitveRecord not to convert timezones on storage or retrieval. See ActiveRecord Timestamp which shows the example:
class Topic < ActiveRecord::Base
self.skip_time_zone_conversion_for_attributes = [:written_on]
end
Correcting time for daylight savings in rails DateTime.parse
Here's my solution so far. I'm hoping someone has a better one.
start_time = DateTime.parse "#{date} #{(form_start_time || start_time)} #{Time.zone}"
start_time = start_time - 1.hour if start_time.dst? && !Time.now.dst?
start_time = start_time + 1.hour if Time.now.dst? && start_time.dst?
It seems to work but I haven't rigorously tested it. I suspect it could be prettied up and shortened but I think this is readable and understandable. Any improvements?
Related Topics
Get Chromes Console Log via Ruby Webdriver
Error Installing SQLite3 Gem via Bundler
Errno::Eaccess: Permission Denied @ Dir_S_Mkdir
Rails 3.1 - Changing Default Scaffold Views and Template
Time Gt Query Not Working with Mongoid and Ruby on Rails
Working with Decimals in Ruby on Rails 3
Openshift and Net-Ssh Incompatibility? (2.9.3-Beta1 VS 2.9.2)
How to Use Views and Layouts with Ruby and Erb (Not Rails)
Ruby Dot Parenthesis Call Syntax
Ruby on Rails: Confirmation Page for Activerecord Object Creation
How to Specify the Location of the Chromedriver Binary
Difference Between or and || When Setting Variables
Ruby String Split with Terminal Strings Empty
Sass Variables Not Parsing Correctly - Undefined Variable: "$Ct-White"
Generate Nested Hashes from Strings and Deep Merging in Ruby