Rails saves a Time instance to the wrong hour - ruby-on-rails

So I have the following Time as a strong:
0:00:00.000
and I use the following strptime:
Time.strptime(times[0], "%H:%M:%S.%L")
Expected Result
0:00:00.000
Actual Result
6:00:00.000
I am 6 hours behind UTC outside of daylight savings. So that might explain why it
s saving six hours ahead. I've tried to wrap it in a Time.use_zone and set the zone to UTC but that still doesn't do the trick.
For now I have it subtracting six hours before saving the record. But I'd like to figure out how to force it to save the timing to the correct hour.

Try with Time.zone
t = "0:00:00.000"
Time.zone.strptime(t, "%H:%M:%S.%L")
=> Thu, 23 Jun 2022 00:00:00.000000000 CDT -05:00

Related

Daylight saving issue Nov 04 - 05 in EST location

Date difference is zero today. When comparing Nov 04 and Nov 05, rare issue happening only for today.
Code I'm using
let dayDiff = Calendar.current.dateComponents([.day], from:businessDate , to: today).day!
Ipad date and time set to automatically.
Tried restarting iPad.
Tried setting date to tomorrow and I get date diff 1.
Is there any issues because of changes in day light savings yesterday?
If so, what settings I have to update in order to work it as expected?
Note: We can fix it with code changes, but like to know any iPad settings will fix this problem.
In any timezone that had a change in daylight saving time during that period (such as the in USA), the difference between 2018-11-04 00:00:00 UTC and 2018-11-05 00:00:00 UTC is only 23 hours which is less than 1 day so you get a difference of 0 days.
If you create those dates with midnight local time instead of UTC time, you will get the expected result of 1 day difference.
Or if you use a calendar set to the UTC timezone to calculate the difference, you will get 1 day difference.

Daylight Savings Time ignored using in_time_zone Rails 4

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.

Moment JS Show Local Time from DateTime.UTCNow

I have a JSON method which is returning a UTC DateTime I am storing on the server. It returns the following:
/Date(1394155885817)/
I'm trying to figure out how to get Moment JS to show me the local (browser) time. So my timezone is -5 (EST) I'd want to see 3:31 instead of 8:31. See the fiddle below.
http://jsfiddle.net/R9UbS/
What am I doing wrong here? How can I force Moment to return local?
I just ran the fiddle in Romania and got 3:31. Also, javascript's Date constructor handles UTC millis correctly. You're getting 8:31 which makes me think it's doing what it's supposed to do, showing the local time. Why is 8:31 incorrect? And is your device's timezone set correctly? (Btw if your timezone is GMT -5 then you would indeed get 8:31 PM the previous day)
Just to put this a bit in perspective, i put the number in a milliseconds to time converter and i saw that 1394155885817 actually means Fri Mar 07 2014 1:31:25 AM in UTC (or GMT for convenience) - subtracting 5 hours from this would result in Fri Mar 06 2014 8:31:25 PM

Why is my timezone one hour off?

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

How do I work out the difference between the current UTC time and a constant other UTC time?

How can I work out how many hours and minutes are between the current UTC time and the next time that it is 0021/12:21am in the UTC timezone?
For example, if the current UTC time is 2000/8:00pm, the answer would be 4 hours and 21 minutes. If the current UTC time is 0003/12:03am, the answer would be 0 hours and 18 minutes.
I'm using Ruby 1.9.2 and Rails 3.0.1
see
http://www.ruby-doc.org/core/classes/Time.src/M000358.html
http://www.ruby-doc.org/core/classes/Time.html#M000334
IF I understand this correctly the difference is represented as float which in turn contains how many seconds...

Resources