Return this SystemCallError’s error number.
Return the fractional part of the number, as a BigDecimal
.
Returns the numerator.
Rational(7).numerator #=> 7 Rational(7, 1).numerator #=> 7 Rational(9, -4).numerator #=> -9 Rational(-2, -10).numerator #=> 1
Returns a simpler approximation of the value if the optional argument eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
Parse an HTTP query string into a hash of key=>value pairs.
params = CGI.parse("query_string") # {"name1" => ["value1", "value2", ...], # "name2" => ["value1", "value2", ...], ... }
Returns a new Date object constructed from the present date:
Date.today.to_s # => "2022-07-06"
See argument start.
Note: This method recognizes many forms in string
, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times
If string
does not specify a valid date, the result is unpredictable; consider using Date._strptime
instead.
Returns a hash of values parsed from string
:
Date._parse('2001-02-03') # => {:year=>2001, :mon=>2, :mday=>3}
If comp
is true
and the given year is in the range (0..99)
, the current century is supplied; otherwise, the year is taken as given:
Date._parse('01-02-03', true) # => {:year=>2001, :mon=>2, :mday=>3} Date._parse('01-02-03', false) # => {:year=>1, :mon=>2, :mday=>3}
See argument limit.
Related: Date.parse
(returns a Date object).
Note: This method recognizes many forms in string
, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times If string
does not specify a valid date, the result is unpredictable; consider using Date._strptime
instead.
Returns a new Date object with values parsed from string
:
Date.parse('2001-02-03') # => #<Date: 2001-02-03> Date.parse('20010203') # => #<Date: 2001-02-03> Date.parse('3rd Feb 2001') # => #<Date: 2001-02-03>
If comp
is true
and the given year is in the range (0..99)
, the current century is supplied; otherwise, the year is taken as given:
Date.parse('01-02-03', true) # => #<Date: 2001-02-03> Date.parse('01-02-03', false) # => #<Date: 0001-02-03>
See:
Argument start.
Argument limit.
Related: Date._parse
(returns a hash).
Returns the day of the month in range (1..31):
Date.new(2001, 2, 3).mday # => 3
Returns the day of the month in range (1..31):
Date.new(2001, 2, 3).mday # => 3
Returns commercial-date year for self
(see Date.commercial
):
Date.new(2001, 2, 3).cwyear # => 2001 Date.new(2000, 1, 1).cwyear # => 1999
Returns the commercial-date weekday index for self
(see Date.commercial
); 1 is Monday:
Date.new(2001, 2, 3).cwday # => 6
Returns true
if self
is a Sunday, false
otherwise.
Returns true
if self
is a Monday, false
otherwise.
Returns true
if self
is a Tuesday, false
otherwise.
Returns true
if self
is a Wednesday, false
otherwise.
Returns true
if self
is a Thursday, false
otherwise.
Returns true
if self
is a Friday, false
otherwise.
Returns true
if self
is a Saturday, false
otherwise.
Returns the Julian start date for calendar reform; if not an infinity, the returned value is suitable for passing to Date#jd
:
d = Date.new(2001, 2, 3, Date::ITALY) s = d.start # => 2299161.0 Date.jd(s).to_s # => "1582-10-15" d = Date.new(2001, 2, 3, Date::ENGLAND) s = d.start # => 2361222.0 Date.jd(s).to_s # => "1752-09-14" Date.new(2001, 2, 3, Date::GREGORIAN).start # => -Infinity Date.new(2001, 2, 3, Date::JULIAN).start # => Infinity
See argument start.
Parses the given representation of date and time, and creates a DateTime
object.
This method does not function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use DateTime.strptime
instead of this method as possible.
If the optional second argument is true and the detected year is in the range “00” to “99”, makes it full.
DateTime.parse('2001-02-03T04:05:06+07:00') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.parse('20010203T040506+0700') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.parse('3rd Feb 2001 04:05:06 PM') #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
Takes a string representation of a Time
and attempts to parse it using a heuristic.
This method **does not** function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use ‘Time.strptime` instead of this method as possible.
require 'time' Time.parse("2010-10-31") #=> 2010-10-31 00:00:00 -0500
Any missing pieces of the date are inferred based on the current date.
require 'time' # assuming the current date is "2011-10-31" Time.parse("12:00") #=> 2011-10-31 12:00:00 -0500
We can change the date used to infer our missing elements by passing a second object that responds to mon
, day
and year
, such as Date
, Time
or DateTime
. We can also use our own object.
require 'time' class MyDate attr_reader :mon, :day, :year def initialize(mon, day, year) @mon, @day, @year = mon, day, year end end d = Date.parse("2010-10-28") t = Time.parse("2010-10-29") dt = DateTime.parse("2010-10-30") md = MyDate.new(10,31,2010) Time.parse("12:00", d) #=> 2010-10-28 12:00:00 -0500 Time.parse("12:00", t) #=> 2010-10-29 12:00:00 -0500 Time.parse("12:00", dt) #=> 2010-10-30 12:00:00 -0500 Time.parse("12:00", md) #=> 2010-10-31 12:00:00 -0500
If a block is given, the year described in date
is converted by the block. This is specifically designed for handling two digit years. For example, if you wanted to treat all two digit years prior to 70 as the year 2000+ you could write this:
require 'time' Time.parse("01-10-31") {|year| year + (year < 70 ? 2000 : 1900)} #=> 2001-10-31 00:00:00 -0500 Time.parse("70-10-31") {|year| year + (year < 70 ? 2000 : 1900)} #=> 1970-10-31 00:00:00 -0500
If the upper components of the given time are broken or missing, they are supplied with those of now
. For the lower components, the minimum values (1 or 0) are assumed if broken or missing. For example:
require 'time' # Suppose it is "Thu Nov 29 14:33:20 2001" now and # your time zone is EST which is GMT-5. now = Time.parse("Thu Nov 29 14:33:20 2001") Time.parse("16:30", now) #=> 2001-11-29 16:30:00 -0500 Time.parse("7/23", now) #=> 2001-07-23 00:00:00 -0500 Time.parse("Aug 31", now) #=> 2001-08-31 00:00:00 -0500 Time.parse("Aug 2000", now) #=> 2000-08-01 00:00:00 -0500
Since there are numerous conflicts among locally defined time zone abbreviations all over the world, this method is not intended to understand all of them. For example, the abbreviation “CST” is used variously as:
-06:00 in America/Chicago, -05:00 in America/Havana, +08:00 in Asia/Harbin, +09:30 in Australia/Darwin, +10:30 in Australia/Adelaide, etc.
Based on this fact, this method only understands the time zone abbreviations described in RFC 822 and the system time zone, in the order named. (i.e. a definition in RFC 822 overrides the system time zone definition.) The system time zone is taken from Time.local(year, 1, 1).zone
and Time.local(year, 7, 1).zone
. If the extracted time zone abbreviation does not match any of them, it is ignored and the given time is regarded as a local time.
ArgumentError
is raised if Date._parse
cannot extract information from date
or if the Time
class cannot represent specified date.
This method can be used as a fail-safe for other parsing methods as:
Time.rfc2822(date) rescue Time.parse(date) Time.httpdate(date) rescue Time.parse(date) Time.xmlschema(date) rescue Time.parse(date)
A failure of Time.parse
should be checked, though.
You must require ‘time’ to use this method.