Time.zone = 'Asia/Kolkata'
Time.zone
=> (GMT+05:30) Asia/Kolkata
Time.zone has been set properly.
Time.zone.parse('0000-01-01 03:00:00 UTC').strftime('%l:%M %p')
=> " 8:53 AM"
Incorrect offset being applied (+05:33 vs +05:30)
Why does this happen? Is there a better way?
The time zone in Calcutta before 1941 seems to have been 5 hours, 53 minutes ahead of UTC.
If you use a year later than that (for example year 2000) instead of year 0, you should get the result you expect.
Due to daylight savings time, you can't really convert a UTC time to a local time, 8:00UTC can convert to different local times in summer and winter. For automatic conversion to give the correct result, you need a full date, not just a time.
Related
My timezone is -4 or -5 from GMT depending on time of year. Currently it is -4.
If I use this code below, will it automatically switch when we change to -5?
select convert_timezone('America/New_York', CAST(json_extract_path_text('{"timestamp":"2019-04-16T03:30:00.704Z"}', 'timestamp') as TIMESTAMP));
Currently the line above returns a date of 2019-04-15.
But if I make this call in December (note the time has +1 hour),
will it still return a date of 2019-04-15.
select convert_timezone('America/New_York', CAST(json_extract_path_text('{"timestamp":"2019-04-16T04:30:00.704Z"}', 'timestamp') as TIMESTAMP));
In other words, will it switch between EDT and EST without intervention? Or is there a better way?
Thanks
Consider:
2019-04-16T03:30:00.704Z (UTC) in America/New_York is 2019-04-15T23:30:00.704-04:00
2019-04-16T04:30:00.704Z (UTC) in America/New_York is 2019-04-16T00:30:00.704-04:00
2019-12-16T04:30:00.704Z (UTC) in America/New_York is 2019-12-15T23:30:00.704-05:00
For US Eastern Time, a date in April will be in -4 while a date in December will be in -5. In other words, the timestamp being converted is what determines which offset to apply.
The date and time that the code executes has no relevance.
I'm having a frustrating issue that I can't seem to narrow down. I have searched many similar articles but they are not close enough to my issue to resolve. I am trying to pull a time from the database and display it in more than one time zone. My Rails app is using UTC as default. Here is what I'm doing:
On the create action I take the string of time which will be saved in the time column in my DB:
params[:schedule][:start] = "09:00"
Time.zone = "Central Time (US & Canada)"
#schedule.start = Time.zone.parse(params[:schedule][:start])
The above formats the time as it is supposed to:
2016-04-12 09:00:00 -0500
This is saved in the DB as:
2000-01-01 14:00:00
This has no time offset which is fine since I know it's in UTC. The problem happens when I go to display the time:
#schedule.start.in_time_zone("Central Time (US & Canada)")
This returns:
Sat, 01 Jan 2000 08:00:00 CST -06:00
Now, since this is a time column, I don't care about the date. I plan on formatting the value to only show the time. However, it is showing CST when it is currently CDT.
I can't figure out why this is happening. As I said I am not setting the Time Zone anywhere in my application.rb or anywhere else and I only set the Time zone on the create action which should be fine when moving to a new action.
Any help on clarifying this would be awesome!
This seems to be because when the time is stored it is stored with the date in the year 2000-01-01 which seems to be why it is using CST. How can I ignore the date when converting it to a particular timezone or will I need to change the column type to DateTime to get this to work properly?
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
The fact that it matters whether it's CST or CDT means you do, on some level, care about the date. While I'm not familiar with the exact rules of Daylight Savings in that region, I do know that Jan 1 is the middle of winter and will definitely not be on Daylight Savings time.
Add the relevant date into your #schedule before putting it into a time zone, and it should fix the problem.
Time.use_zone('Pacific Time (US & Canada)') do
p Time.zone.now
end
I get the following: => Sun, 14 Apr 2013 20:30:53 PDT -07:00
Yet when I do Rails Time Zone Select.... it says -8:00 quite clearly. Why is it -7 in one area and -8 in another?
Other times, time zones like Hawaii which are -10:00 don't get offset by an hour.
I assume this has something to do with DST, but I'm more curious whether it means it's working properly or improperly and there is something else I need to do.
Ultimately I'm using this in a datepicker, and I find it very odd that when I use Time.zone.parse (along with my time zone around filter), its offsetting everything by 1 hour.
THanks
Edit
Heres a similar problem I also just experienced with another piece of code
2.0.0-p0 :006 >
2.0.0-p0 :006 > u.meetups.in_future.first.meetup_time
Meetup Load (0.4ms) SELECT `meetups`.* FROM `meetups` WHERE `meetups`.`user_id` = 1 AND (meetup_time >= '2013-04-23 04:46:48') ORDER BY meetup_time ASC LIMIT 1
=> Tue, 23 Apr 2013 05:43:00 UTC 00:00
2.0.0-p0 :007 >
Notice the discrepancy in the result compared to the where clause.
Edit
It appears to work for CST properly, but PST is off by ~1 hours?? I feel this is all the same problem, I am just missing a piece of the puzzle.
The output is correct. Pacific Daylight Time has an offset of -7, while Pacific Standard Time has an offset of -8. My guess is that "Rails Time Zone Select" (whatever that is) is only is showing you the "standard" offsets, rather than the current ones. This is common in time zone pickers.
Hawaii does not implement Daylight Savings Time of any kind, so that addresses your second point.
On your third point, I would have to know more about your database platform to answer why the values are converted to UTC. Given that these are event times, I would say that they should be in UTC. They could also be in the time zone of the location of the "meetup", but only if the offset from UTC was also stored. But never should they be in the time zone of the server.
On your fourth point, it's difficult to tell what you mean without more details. Expand if you feel necessary.
Running this code...
Time.use_zone('Pacific Time (US & Canada)') do
p Time.zone.now
end
I get the following: => Sun, 14 Apr 2013 20:30:53 PDT -07:00
Yet when I do Rails Time Zone Select.... it says -8:00 quite clearly. Why is it -7 in one area and -8 in another?
Other times, time zones like Hawaii which are -10:00 don't get offset by an hour.
I assume this has something to do with DST, but I'm more curious whether it means it's working properly or improperly and there is something else I need to do.
Ultimately I'm using this in a datepicker, and I find it very odd that when I use Time.zone.parse (along with my time zone around filter), its offsetting everything by 1 hour.
THanks
In a scheduling method, I need to take a (local_date, local_time, and local_time_zone), and return the corresponding utc time accounting for whether Daylight Saving time is in effect on that date.
I do not want to change Time.zone since while threadsafe, it has the entirely unexpected result of persisting across multiple requests - a Bad Thing.
The following (0800 Pacific time) does NOT work because they return the same time in UTC (16:00) even though the first day (Oct 1) IS in daylight saving time and the second day (Dec 1) is NOT in daylight savings time, so the UTC should be different
Time.new(2012, 10, 1, 8, 0, 0, ActiveSupport::TimeZone['Pacific Time (US & Canada)'].utc_offset).utc
# 2012-10-01 16:00:00 UTC <<<< should be 15:00
Time.new(2012, 12, 1, 8, 0, 0, ActiveSupport::TimeZone['Pacific Time (US & Canada)'].utc_offset).utc
# 2012-12-01 16:00:00 UTC
I had hoped that the .utc method would take into account whether the daylight saving is in effect, but it doesn't.
I'm not sure if I should be using a Time object, a DateTime object, and TimeWith Zone, etc.
===
Note: There IS a way to do it using the Chronic gem (below). But I would like to use the built-in Ruby Date and Time methods to get the same (eg, correct) result:
Chronic.time_class =ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
Chronic.parse("10/1/2012 0800").utc
# 2012-10-01 15:00:00 UTC
Chronic.parse("12/1/2012 0800").utc
# 2012-12-01 16:00:00 UTC
Found it on another SO thread: the trick is to put the time (and zone) into a format usable by Time.parse:
Time.parse("2012-10-1 8:00:00 Pacific Time (US & Canada)").utc
# 2012-10-01 15:00:00 UTC
Time.parse("2012-12-1 8:00:00 Pacific Time (US & Canada)").utc
# 2012-12-01 16:00:00 UTC
ANOTHER METHOD (probably better):
ActiveSupport::TimeZone["Pacific Time (US & Canada)"].parse("2012-12-1 8am").utc
After some playing around it looks like we were right to be wary of changing Time.zone simply to create a time in a certain zone -- setting Time.zone can 'stick' over multiple requests (in the same thread). So if you have two different windows open to a rails app, and are viewing two different accounts, setting Time.zone for the account in one window can actually change the Time.zone in the other window for the other account.
I recently went through something similar and found Time.zone.local to work fine taking daylight savings into account. I'm not sure about US daylight savings, but here in Australia daylight savings kick in on the 6th October 2013.
The following code seems to work fine for me:
1.9.3-p194 :009 > Time.zone = 'Sydney'
=> "Sydney"
1.9.3-p194 :010 > Time.zone.local(2013, 10, 1, 0, 0, 0).utc
=> 2013-09-30 14:00:00 UTC
1.9.3-p194 :011 > Time.zone.local(2013, 10, 10, 0, 0, 0).utc
=> 2013-10-09 13:00:00 UTC
Are you sure your dates / time zones are correct? Seems that Pacific Time experiences daylight savings in November according to this page:
http://www.timeanddate.com/worldclock/timezone.html?n=137