Ruby Time.parse gives me out of range error
My guess would be that its expecting the second part of the string (the 14) to be the month.
This link may help you parse it.
Random, intermittent argument out of range error while parsing CSV and parsing time
Consider this code:
Time.parse('31/1/2001') # => 2001-01-31 00:00:00 -0700
Time.parse('1/31/2001') # => argument out of range (ArgumentError)
Why is it out of range? Because Ruby, by default tries to break a date string that matches that format into a day, month, year format, AKA '%d/%m/%Y'
.
It's not possible to have a month number 31, at least not in this reality, so Ruby raises an exception.
Instead, using your time value and strptime
:
Time.parse('18/10/2011') # => 2011-10-18 00:00:00 -0700
Time.parse('10/18/2011') # => argument out of range (ArgumentError)
Time.strptime('10/18/2011', '%m/%d/%Y' ) # => 2011-10-18 00:00:00 -0700
This is a situation you can't fix by trying to trap the exception. parse
isn't smart enough to deal with this either, nor can it be made smart enough. You HAVE to know the LOCALE or the date format being sent to you.
Trying to parse without knowing means that the date you get back could very well be wrong. For instance, which format is this:
Time.parse('10/12/2011') # => 2011-12-10 00:00:00 -0700
Is it really October 12 or is it December 10? It kind of depends on where your data originated, which is typically set by the LOCALE OS setting of the machine that sent it. parse
can't tell, so it does the easy thing it was told to do, and it assumes it's '%d/%m/%Y'
format and returns that to the code, and, unless you looked carefully, you probably let the database store that.
Go time.Parse() getting month out of range error
You're using the wrong reference time in the layout
parameter of time.Parse
which should be Jan 2, 2006 at 3:04pm (MST)
Change your begin
line to the following and it will work:
begin, err := time.Parse("2006-01-02 15:04:05", "2016-12-25 "+string(start)+":00")
func Parse
Getting argument out of range when trying to turn a duration into milliseconds in Rails 4
If you know you're always going to be using a hours:minutes:seconds format, but the number in each field isn't guaranteed to be inside the 'normal' range (e.g. 0-23 for hours, 0-59 for minutes, etc), then you're probably best off doing it 'manually' using something like this:
def duration_in_milliseconds(input)
h, m, s = input.split(':').map(&:to_i)
(h.hours + m.minutes + s.seconds) * 1000
end
puts duration_in_milliseconds('34:13:00') #=> 123180000
Note that this only works with ActiveSupport, but you have that, since you've specified Rails. Also, this assumes you're always getting all three terms (e.g. 5 seconds is 00:00:05). The full setup that accepts shorter strings as well would want to also use your convert_to_hrs method.
Note also that this works even if formatting isn't strictly 'time-like', as long as you have consistent colons as seperators:
puts duration_in_milliseconds('1:1:5') #=> 3665000
The Numeric#hours
, Numeric#minutes
and Numeric#seconds
methods are provided by ActiveSupport, as part of active_support/core-ext/time.rb. They aren't particularly documented, but they return ActiveSupport::Duration objects, which have fancy methods for interacting with Time and Date issues like 5.days.ago
, but when treated as an integer are effectively a number of seconds.
Time.parse 11-21-2017 3:00PM -0500 doesn't work - Ruby/Rails
I can reproduce your out of range issue.
You need to provide format of your date string.
Following code works:
DateTime.strptime("11-21-2017 3:00PM -0500", "%m-%d-%Y %I:%M%p %z")
Why is this Time.parse failing
Time#parse
creates a Time
object out of a String
, which it takes as its first argument. You already have a Time
object, so Time.parse
doesn't know what to do with it.
In order to format the date like you want it, take a look at Time#strftime
. You can format it like you want with the format string:
events.first.datetime.strftime("%A %B %d, %Y at %I:%M %p")
Take a look at the manual entry for strftime
for other format specifiers.
Why does Time.parse fail in Rails 4 and 5 but not in Rails 3 for date strings with a space between the date and time?
Apparently, in Rails 4 and later, that string is interpreted as dd/mm/yyyy
(as it should, if you ask me. To hell with mm/dd/yyyy
).
But if you want that, the proper solution would be to specify the format, not gsub
random spaces!
Time.strptime "12/13/2016 3:11 PM", '%m/%d/%Y %H:%M %p'
=> 2016-12-13 15:11:00 -0500
Related Topics
Instance Variables Inheritance
Application.CSS Not Being Served as an Asset
Why Does Ruby Use Nil to Name the Null Object
Rails 3/ Git/ Bundler Fatal Could Not Parse Object
Rails - Link_To Helper with Data-* Attribute
Why Does Using '-Webkit-Appearance: None' on a Select Option in Osx Make The Text Disappear
Why Array.Reverse_Each Is Faster Than Array.Reverse.Each
"Cannot Load Such File - Rubygems/Defaults/Operating_System" When Installing Ruby 2.0.0
When Do You Need to Pass Arguments to 'Thread.New'
Using CSS to Style Ruby Objects in Erb
Jekyll Templates Using Django-Like Liquid Blocks/Inheritance
How to Declare a Rake Task That Depends on a Parameterized Task
When Trying to Generate a Model with Rails and Postgresql, the Command Hangs Without Error
Which Ruby Rest API Client for Neo4J