# # ## DateTime # # A subclass of Date that easily handles date, hour, minute, second, and offset. # # DateTime class is considered deprecated. Use Time class. # # DateTime does not consider any leap seconds, does not track any summer time # rules. # # A DateTime object is created with DateTime::new, DateTime::jd, # DateTime::ordinal, DateTime::commercial, DateTime::parse, DateTime::strptime, # DateTime::now, Time#to_datetime, etc. # # require 'date' # # DateTime.new(2001,2,3,4,5,6) # #=> # # # The last element of day, hour, minute, or second can be a fractional number. # The fractional number's precision is assumed at most nanosecond. # # DateTime.new(2001,2,3.5) # #=> # # # An optional argument, the offset, indicates the difference between the local # time and UTC. For example, `Rational(3,24)` represents ahead of 3 hours of # UTC, `Rational(-5,24)` represents behind of 5 hours of UTC. The offset should # be -1 to +1, and its precision is assumed at most second. The default value is # zero (equals to UTC). # # DateTime.new(2001,2,3,4,5,6,Rational(3,24)) # #=> # # # The offset also accepts string form: # # DateTime.new(2001,2,3,4,5,6,'+03:00') # #=> # # # An optional argument, the day of calendar reform (`start`), denotes a Julian # day number, which should be 2298874 to 2426355 or negative/positive infinity. # The default value is `Date::ITALY` (2299161=1582-10-15). # # A DateTime object has various methods. See each reference. # # d = DateTime.parse('3rd Feb 2001 04:05:06+03:30') # #=> # # d.hour #=> 4 # d.min #=> 5 # d.sec #=> 6 # d.offset #=> (7/48) # d.zone #=> "+03:30" # d += Rational('1.5') # #=> # # d = d.new_offset('+09:00') # #=> # # d.strftime('%I:%M:%S %p') # #=> "09:35:06 PM" # d > DateTime.new(1999) # #=> true # # ### When should you use DateTime and when should you use Time? # # It's a common misconception that [William # Shakespeare](https://en.wikipedia.org/wiki/William_Shakespeare) and [Miguel de # Cervantes](https://en.wikipedia.org/wiki/Miguel_de_Cervantes) died on the same # day in history - so much so that UNESCO named April 23 as [World Book Day # because of this fact](https://en.wikipedia.org/wiki/World_Book_Day). However, # because England hadn't yet adopted the [Gregorian Calendar # Reform](https://en.wikipedia.org/wiki/Gregorian_calendar#Gregorian_reform) # (and wouldn't until # [1752](https://en.wikipedia.org/wiki/Calendar_(New_Style)_Act_1750)) their # deaths are actually 10 days apart. Since Ruby's Time class implements a # [proleptic Gregorian # calendar](https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar) and has # no concept of calendar reform there's no way to express this with Time # objects. This is where DateTime steps in: # # shakespeare = DateTime.iso8601('1616-04-23', Date::ENGLAND) # #=> Tue, 23 Apr 1616 00:00:00 +0000 # cervantes = DateTime.iso8601('1616-04-23', Date::ITALY) # #=> Sat, 23 Apr 1616 00:00:00 +0000 # # Already you can see something is weird - the days of the week are different. # Taking this further: # # cervantes == shakespeare # #=> false # (shakespeare - cervantes).to_i # #=> 10 # # This shows that in fact they died 10 days apart (in reality 11 days since # Cervantes died a day earlier but was buried on the 23rd). We can see the # actual date of Shakespeare's death by using the #gregorian method to convert # it: # # shakespeare.gregorian # #=> Tue, 03 May 1616 00:00:00 +0000 # # So there's an argument that all the celebrations that take place on the 23rd # April in Stratford-upon-Avon are actually the wrong date since England is now # using the Gregorian calendar. You can see why when we transition across the # reform date boundary: # # # start off with the anniversary of Shakespeare's birth in 1751 # shakespeare = DateTime.iso8601('1751-04-23', Date::ENGLAND) # #=> Tue, 23 Apr 1751 00:00:00 +0000 # # # add 366 days since 1752 is a leap year and April 23 is after February 29 # shakespeare + 366 # #=> Thu, 23 Apr 1752 00:00:00 +0000 # # # add another 365 days to take us to the anniversary in 1753 # shakespeare + 366 + 365 # #=> Fri, 04 May 1753 00:00:00 +0000 # # As you can see, if we're accurately tracking the number of [solar # years](https://en.wikipedia.org/wiki/Tropical_year) since Shakespeare's # birthday then the correct anniversary date would be the 4th May and not the # 23rd April. # # So when should you use DateTime in Ruby and when should you use Time? Almost # certainly you'll want to use Time since your app is probably dealing with # current dates and times. However, if you need to deal with dates and times in # a historical context you'll want to use DateTime to avoid making the same # mistakes as UNESCO. If you also have to deal with timezones then best of luck # - just bear in mind that you'll probably be dealing with [local solar # times](https://en.wikipedia.org/wiki/Solar_time), since it wasn't until the # 19th century that the introduction of the railways necessitated the need for # [Standard Time](https://en.wikipedia.org/wiki/Standard_time#Great_Britain) and # eventually timezones. # class DateTime < Date # # Same as DateTime.new. # def initialize: (?Integer year, ?Integer month, ?Integer mday, ?Integer hour, ?Integer minute, ?Integer second, ?Integer offset, ?Integer start) -> void # # Parses the given representation of date and time with the given template, and # returns a hash of parsed elements. _strptime does not support specification # of flags and width unlike strftime. # # See also strptime(3) and #strftime. # def self._strptime: (String str, ?String format) -> Hash[Symbol, Integer | String] # # Same as DateTime.new. # def self.civil: (?Integer year, ?Integer month, ?Integer mday, ?Integer hour, ?Integer minute, ?Integer second, ?Integer offset, ?Integer start) -> DateTime # # Creates a DateTime object denoting the given week date. # # DateTime.commercial(2001) #=> # # DateTime.commercial(2002) #=> # # DateTime.commercial(2001,5,6,4,5,6,'+7') # #=> # # def self.commercial: (?Integer cwyear, ?Integer cweek, ?Integer cwday, ?Integer hour, ?Integer minute, ?Integer second, ?Integer offset, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some RFC # 2616 format. # # DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT') # #=> # # # 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. # def self.httpdate: (String str, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some # typical ISO 8601 formats. # # DateTime.iso8601('2001-02-03T04:05:06+07:00') # #=> # # DateTime.iso8601('20010203T040506+0700') # #=> # # DateTime.iso8601('2001-W05-6T04:05:06+07: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. # def self.iso8601: (String str, ?Integer start) -> DateTime # # Creates a DateTime object denoting the given chronological Julian day number. # # DateTime.jd(2451944) #=> # # DateTime.jd(2451945) #=> # # DateTime.jd(Rational('0.5')) # #=> # # def self.jd: (?Integer jd, ?Integer hour, ?Integer minute, ?Integer second, ?Integer offset, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some # typical JIS X 0301 formats. # # DateTime.jisx0301('H13.02.03T04:05:06+07:00') # #=> # # # For no-era year, legacy format, Heisei is assumed. # # DateTime.jisx0301('13.02.03T04:05:06+07: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. # def self.jisx0301: (String str, ?Integer start) -> DateTime # # Creates a DateTime object denoting the present time. # # DateTime.now #=> # # def self.now: (?Integer start) -> DateTime # # Creates a DateTime object denoting the given ordinal date. # # DateTime.ordinal(2001,34) #=> # # DateTime.ordinal(2001,34,4,5,6,'+7') # #=> # # DateTime.ordinal(2001,-332,-20,-55,-54,'+7') # #=> # # def self.ordinal: (?Integer year, ?Integer yday, ?Integer hour, ?Integer minute, ?Integer second, ?Integer offset, ?Integer start) -> DateTime # # 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.parse('20010203T040506+0700') # #=> # # DateTime.parse('3rd Feb 2001 04:05:06 PM') # #=> # # # 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. # def self.parse: (String str, ?boolish complete, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some # typical RFC 2822 formats. # # DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700') # #=> # # # 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. # def self.rfc2822: (String str, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some # typical RFC 3339 formats. # # DateTime.rfc3339('2001-02-03T04:05:06+07: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. # def self.rfc3339: (String str, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some # typical RFC 2822 formats. # # DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700') # #=> # # # 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. # def self.rfc822: (String str, ?Integer start) -> DateTime # # Parses the given representation of date and time with the given template, and # creates a DateTime object. strptime does not support specification of flags # and width unlike strftime. # # DateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z') # #=> # # DateTime.strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p') # #=> # # DateTime.strptime('2001-W05-6T04:05:06+07:00', '%G-W%V-%uT%H:%M:%S%z') # #=> # # DateTime.strptime('2001 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z') # #=> # # DateTime.strptime('2001 05 6 04 05 06 +7', '%Y %W %u %H %M %S %z') # #=> # # DateTime.strptime('-1', '%s') # #=> # # DateTime.strptime('-1000', '%Q') # #=> # # DateTime.strptime('sat3feb014pm+7', '%a%d%b%y%H%p%z') # #=> # # # See also strptime(3) and #strftime. # def self.strptime: (String str, ?String format, ?Integer start) -> DateTime # # Creates a new DateTime object by parsing from a string according to some # typical XML Schema formats. # # DateTime.xmlschema('2001-02-03T04:05:06+07: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. # def self.xmlschema: (String str, ?Integer start) -> DateTime public # # Returns a hash of the name/value pairs, to use in pattern matching. Possible # keys are: `:year`, `:month`, `:day`, `:wday`, `:yday`, `:hour`, `:min`, # `:sec`, `:sec_fraction`, `:zone`. # # Possible usages: # # dt = DateTime.new(2022, 10, 5, 13, 30) # # if d in wday: 1..5, hour: 10..18 # uses deconstruct_keys underneath # puts "Working time" # end # #=> prints "Working time" # # case dt # in year: ...2022 # puts "too old" # in month: ..9 # puts "quarter 1-3" # in wday: 1..5, month: # puts "working day in month #{month}" # end # #=> prints "working day in month 10" # # Note that deconstruction by pattern can also be combined with class check: # # if d in DateTime(wday: 1..5, hour: 10..18, day: ..7) # puts "Working time, first week of the month" # end # def deconstruct_keys: (Array[Symbol]?) -> Hash[Symbol, untyped] # # Returns the hour in range (0..23): # # DateTime.new(2001, 2, 3, 4, 5, 6).hour # => 4 # def hour: () -> Integer # # This method is equivalent to strftime('%FT%T%:z'). The optional argument `n` # is the number of digits for fractional seconds. # # DateTime.parse('2001-02-03T04:05:06.123456789+07:00').iso8601(9) # #=> "2001-02-03T04:05:06.123456789+07:00" # def iso8601: (?Integer n) -> String # # Returns a string in a JIS X 0301 format. The optional argument `n` is the # number of digits for fractional seconds. # # DateTime.parse('2001-02-03T04:05:06.123456789+07:00').jisx0301(9) # #=> "H13.02.03T04:05:06.123456789+07:00" # def jisx0301: (?Integer n) -> String # # Returns the minute in range (0..59): # # DateTime.new(2001, 2, 3, 4, 5, 6).min # => 5 # def min: () -> Integer # # Returns the minute in range (0..59): # # DateTime.new(2001, 2, 3, 4, 5, 6).min # => 5 # def minute: () -> Integer # # Duplicates self and resets its offset. # # d = DateTime.new(2001,2,3,4,5,6,'-02:00') # #=> # # d.new_offset('+09:00') #=> # # def new_offset: (?String offset) -> DateTime # # Returns the offset. # # DateTime.parse('04pm+0730').offset #=> (5/16) # def offset: () -> Rational # # This method is equivalent to strftime('%FT%T%:z'). The optional argument `n` # is the number of digits for fractional seconds. # # DateTime.parse('2001-02-03T04:05:06.123456789+07:00').rfc3339(9) # #=> "2001-02-03T04:05:06.123456789+07:00" # def rfc3339: (?Integer n) -> String # # Returns the second in range (0..59): # # DateTime.new(2001, 2, 3, 4, 5, 6).sec # => 6 # def sec: () -> Integer # # Returns the fractional part of the second in range (Rational(0, # 1)...Rational(1, 1)): # # DateTime.new(2001, 2, 3, 4, 5, 6.5).sec_fraction # => (1/2) # def sec_fraction: () -> Rational # # Returns the second in range (0..59): # # DateTime.new(2001, 2, 3, 4, 5, 6).sec # => 6 # def second: () -> Integer # # Returns the fractional part of the second in range (Rational(0, # 1)...Rational(1, 1)): # # DateTime.new(2001, 2, 3, 4, 5, 6.5).sec_fraction # => (1/2) # def second_fraction: () -> Rational # # Returns a string representation of `self`, formatted according the given # +format: # # DateTime.now.strftime # => "2022-07-01T11:03:19-05:00" # # For other formats, see [Formats for Dates and # Times](rdoc-ref:strftime_formatting.rdoc): # def strftime: (?String format) -> String # # Returns a Date object which denotes self. # def to_date: () -> Date # # Returns self. # def to_datetime: () -> DateTime # # Returns a string in an ISO 8601 format. (This method doesn't use the expanded # representations.) # # DateTime.new(2001,2,3,4,5,6,'-7').to_s # #=> "2001-02-03T04:05:06-07:00" # def to_s: () -> String # # Returns a Time object which denotes self. # def to_time: () -> Time # # This method is equivalent to strftime('%FT%T%:z'). The optional argument `n` # is the number of digits for fractional seconds. # # DateTime.parse('2001-02-03T04:05:06.123456789+07:00').iso8601(9) # #=> "2001-02-03T04:05:06.123456789+07:00" # def xmlschema: (?Integer n) -> String # # Returns the timezone. # # DateTime.parse('04pm+0730').zone #=> "+07:30" # def zone: () -> String end