Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • date.rb
  • date/delta.rb
  • date/delta/parser.rb
  • date/format.rb

Class/Module Index [+]

Quicksearch

Date

Class representing a date.

See the documentation to the file date.rb for an overview.

Internally, the date is represented as an Astronomical Julian Day Number, ajd. The Day of Calendar Reform, sg, is also stored, for conversions to other date formats. (There is also an of field for a time zone offset, but this is only for the use of the DateTime subclass.)

A new Date object is created using one of the object creation class methods named after the corresponding date format, and the arguments appropriate to that date format; for instance, Date::civil() (aliased to Date::new()) with year, month, and day-of-month, or Date::ordinal() with year and day-of-year. All of these object creation class methods also take the Day of Calendar Reform as an optional argument.

Date objects are immutable once created.

Once a Date has been created, date values can be retrieved for the different date formats supported using instance methods. For instance, mon() gives the Civil month, cwday() gives the Commercial day of the week, and yday() gives the Ordinal day of the year. Date values can be retrieved in any format, regardless of what format was used to create the Date instance.

The Date class includes the Comparable module, allowing date objects to be compared and sorted, ranges of dates to be created, and so forth.

format.rb: Written by Tadayoshi Funaba 1999-2009 $Id: format.rb,v 2.43 2008-01-17 20:16:31+09 tadf Exp $

Constants

ABBR_DAYNAMES

Abbreviated day names, in English.

ABBR_MONTHNAMES

Abbreviated month names, in English.

DAYNAMES

Full names of days of the week, in English. Days of the week count from 0 to 6 (except in the commercial week); a day’s numerical representation indexed into this array gives the name of that day.

ENGLAND

The Julian Day Number of the Day of Calendar Reform for England and her Colonies.

GREGORIAN

A constant used to indicate that a Date should always use the Gregorian calendar.

ITALY

The Julian Day Number of the Day of Calendar Reform for Italy and the Catholic countries.

JULIAN

A constant used to indicate that a Date should always use the Julian calendar.

MONTHNAMES

Full month names, in English. Months count from 1 to 12; a month’s numerical representation indexed into this array gives the name of that month (hence the first element is nil).

Public Instance Methods

%(n) click to toggle source
 
               # File date/delta.rb, line 292
def % (n) dx_conv1(:%, n) end
            
*(n) click to toggle source
 
               # File date/delta.rb, line 274
def * (n) dx_muldiv(:*, n) end
            
**(n) click to toggle source
 
               # File date/delta.rb, line 314
def ** (n) dx_conv1(:**, n) end
            
+(n) click to toggle source
 
               # File date/delta.rb, line 259
def + (n) dx_addsub(:+, n) end
            
+@() click to toggle source
 
               # File date/delta.rb, line 245
def +@ () self.class.new!(+@delta) end
            
-(n) click to toggle source
 
               # File date/delta.rb, line 260
def - (n) dx_addsub(:-, n) end
            
-@() click to toggle source
 
               # File date/delta.rb, line 244
def -@ () self.class.new!(-@delta) end
            
/(n) click to toggle source
 
               # File date/delta.rb, line 275
def / (n) dx_muldiv(:/, n) end
            
<=>(other) click to toggle source
 
               # File date/delta.rb, line 317
def <=> (other)
  if @delta.imag != 0
    raise ArgumentError, "<=>: #{self} has month"
  end
  case other
  when Numeric; return @delta.real <=> other
  when Delta;   return @delta.real <=> other.delta.real
  else
    begin
      l, r = other.coerce(self)
      return l <=> r
    rescue NoMethodError
    end
  end
  nil
end
            
==(other) click to toggle source
 
               # File date/delta.rb, line 334
def == (other)
  case other
  when Numeric; return @delta == other
  when Delta;   return @delta == other
  else
    begin
      l, r = other.coerce(self)
      return l == r
    rescue NoMethodError
    end
  end
  nil
end
            
abs() click to toggle source
 
               # File date/delta.rb, line 368
def abs() dx_conv0(:abs) end
            
asctime() click to toggle source

alias_method :format, :strftime

 
               # File date/format.rb, line 341
def asctime() strftime('%c') end
            
Also aliased as: ctime
ceil() click to toggle source
 
               # File date/delta.rb, line 370
def ceil() dx_conv0(:ceil) end
            
coerce(other) click to toggle source
 
               # File date/delta.rb, line 348
def coerce(other)
  case other
  when Numeric; return other, @delta
  else
    super
  end
end
            
ctime() click to toggle source
Alias for: asctime
div(n) click to toggle source
 
               # File date/delta.rb, line 294
def div(n) dx_conv1(:div, n) end
            
divmod(n) click to toggle source
 
               # File date/delta.rb, line 296
def divmod(n) [div(n), modulo(n)] end
            
eql?(other) click to toggle source
 
               # File date/delta.rb, line 356
def eql? (other) Delta === other && self == other end
            
floor() click to toggle source
 
               # File date/delta.rb, line 371
def floor() dx_conv0(:floor) end
            
hash() click to toggle source
 
               # File date/delta.rb, line 357
def hash() @delta.hash end
            
inspect() click to toggle source
 
               # File date/delta.rb, line 382
def inspect() format('#<%s: %s (%s)>', self.class, to_s, @delta) end
            
integer?() click to toggle source
 
               # File date/delta.rb, line 242
def integer? () @delta.imag == 0 && @delta.real.integer? end
            
iso8601() click to toggle source
 
               # File date/format.rb, line 345
def iso8601() strftime('%F') end
            
marshal_dump() click to toggle source
 
               # File date/delta.rb, line 392
def marshal_dump() @delta end
            
marshal_load(a) click to toggle source
 
               # File date/delta.rb, line 394
def marshal_load(a)
  @delta = a
  @__ca__ = {}
end
            
modulo(n) click to toggle source
 
               # File date/delta.rb, line 295
def modulo(n) dx_conv1(:modulo, n) end
            
quo(n) click to toggle source
 
               # File date/delta.rb, line 315
def quo(n) dx_muldiv(:quo, n) end
            
quotient(n) click to toggle source
 
               # File date/delta.rb, line 298
def quotient(n)
  if @delta.imag != 0
    raise ArgumentError, "quotient: #{self} has month"
  end
  case n
  when Numeric
    return self.class.new!(Complex((@delta.real / n).truncate))
  else
    l, r = n.coerce(self)
    return l.__send__(m, r)
  end
end
            
quotrem(n) click to toggle source
 
               # File date/delta.rb, line 312
def quotrem(n) [quotient(n), remainder(n)] end
            
remainder(n) click to toggle source
 
               # File date/delta.rb, line 311
def remainder(n) dx_conv1(:remainder, n) end
            
rfc3339() click to toggle source
 
               # File date/format.rb, line 347
def rfc3339() iso8601 end
            
round() click to toggle source
 
               # File date/delta.rb, line 372
def round() dx_conv0(:round) end
            
strftime(fmt='%F') click to toggle source
 
               # File date/format.rb, line 215
def strftime(fmt='%F')
  fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
    f = {}
    m = $&
    s, w, c = $1, $2, $3
    if s
      s.scan(/./) do |k|
        case k
        when '-'; f[:p] = '-'
        when '_'; f[:p] = "\s"
        when '0'; f[:p] = '0'
        when '^'; f[:u] = true
        when '#'; f[:x] = true
        end
      end
    end
    if w
      f[:w] = w.to_i
    end
    case c
    when 'A'; emit_ad(DAYNAMES[wday], 0, f)
    when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
    when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
    when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
    when 'C', 'EC'; emit_sn((year / 100).floor, 2, f)
    when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
    when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'd', 'Od'; emit_n(mday, 2, f)
    when 'e', 'Oe'; emit_a(mday, 2, f)
    when 'F'
      if m == '%F'
        format('%.4d-%02d-%02d', year, mon, mday) # 4p
      else
        emit_a(strftime('%Y-%m-%d'), 0, f)
      end
    when 'G'; emit_sn(cwyear, 4, f)
    when 'g'; emit_n(cwyear % 100, 2, f)
    when 'H', 'OH'; emit_n(hour, 2, f)
    when 'h'; emit_ad(strftime('%b'), 0, f)
    when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
    when 'j'; emit_n(yday, 3, f)
    when 'k'; emit_a(hour, 2, f)
    when 'L'
      f[:p] = nil
      w = f[:w] || 3
      u = 10**w
      emit_n((sec_fraction * u).floor, w, f)
    when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
    when 'M', 'OM'; emit_n(min, 2, f)
    when 'm', 'Om'; emit_n(mon, 2, f)
    when 'N'
      f[:p] = nil
      w = f[:w] || 9
      u = 10**w
      emit_n((sec_fraction * u).floor, w, f)
    when 'n'; emit_a("\n", 0, f)
    when 'P'; emit_ad(strftime('%p').downcase, 0, f)
    when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
    when 'Q'
      s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
      emit_sn(s, 1, f)
    when 'R'; emit_a(strftime('%H:%M'), 0, f)
    when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
    when 'S', 'OS'; emit_n(sec, 2, f)
    when 's'
      s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
      emit_sn(s, 1, f)
    when 'T'
      if m == '%T'
        format('%02d:%02d:%02d', hour, min, sec) # 4p
      else
        emit_a(strftime('%H:%M:%S'), 0, f)
      end
    when 't'; emit_a("\t", 0, f)
    when 'U', 'W', 'OU', 'OW'
      emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
    when 'u', 'Ou'; emit_n(cwday, 1, f)
    when 'V', 'OV'; emit_n(cweek, 2, f)
    when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
    when 'w', 'Ow'; emit_n(wday, 1, f)
    when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
    when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'Y', 'EY'; emit_sn(year, 4, f)
    when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
    when 'Z'; emit_au(strftime('%:z'), 0, f)
    when /\A(:{0,3})z/
      t = $1.size
      sign = if offset < 0 then -1 else +1 end
      fr = offset.abs
      ss = fr.div(SECONDS_IN_DAY) # 4p
      hh, ss = ss.divmod(3600)
      mm, ss = ss.divmod(60)
      if t == 3
        if    ss.nonzero? then t =  2
        elsif mm.nonzero? then t =  1
        else                   t = -1
        end
      end
      case t
      when -1
        tail = []
        sep = ''
      when 0
        f[:w] -= 2 if f[:w]
        tail = ['%02d' % mm]
        sep = ''
      when 1
        f[:w] -= 3 if f[:w]
        tail = ['%02d' % mm]
        sep = ':'
      when 2
        f[:w] -= 6 if f[:w]
        tail = ['%02d' % mm, '%02d' % ss]
        sep = ':'
      end
      ([emit_z(sign * hh, 2, f)] + tail).join(sep)
    when '%'; emit_a('%', 0, f)
    when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
    else
      m
    end
  end
end
            
to_c() click to toggle source
 
               # File date/delta.rb, line 378
def to_c() @delta end
            
to_f() click to toggle source
 
               # File date/delta.rb, line 376
def to_f() dx_conv0(:to_f) end
            
to_i() click to toggle source
 
               # File date/delta.rb, line 375
def to_i() dx_conv0(:to_i) end
            
Also aliased as: to_int
to_int() click to toggle source
Alias for: to_i
to_r() click to toggle source
 
               # File date/delta.rb, line 377
def to_r() dx_conv0(:to_r) end
            
to_s() click to toggle source
 
               # File date/delta.rb, line 384
def to_s
  format(%Q(%s(%dd %.02d:%02d'%02d"%03d)%s(%dy %dm)), # '
         if @delta.real < 0 then '-' else '+' end,
         days.abs, hours.abs, mins.abs, secs.abs, sec_fractions.abs * 1000,
         if @delta.imag < 0 then '-' else '+' end,
         years.abs, months.abs)
end
            
truncate() click to toggle source
 
               # File date/delta.rb, line 373
def truncate() dx_conv0(:truncate) end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus