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

In Files

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

Class/Module Index [+]

Quicksearch

Date::Delta

Public Class Methods

delta_to_dhms(delta) click to toggle source
 
               # File date/delta.rb, line 111
def self.delta_to_dhms(delta)
  fr = delta.imag.abs
  y,   fr = fr.divmod(12)
  m,   fr = fr.divmod(1)

  if delta.imag < 0
    y = -y
    m = -m
  end

  fr = delta.real.abs
  ss,  fr = fr.divmod(SECONDS_IN_DAY) # 4p
  d,   ss = ss.divmod(86400)
  h,   ss = ss.divmod(3600)
  min, s  = ss.divmod(60)

  if delta.real < 0
    d = -d
    h = -h
    min = -min
    s = -s
  end

  return y, m, d, h, min, s, fr
end
            
dhms_to_delta(y, m, d, h, min, s, fr) click to toggle source
 
               # File date/delta.rb, line 137
def self.dhms_to_delta(y, m, d, h, min, s, fr)
  fr = 0 if fr == 0
  Complex(0, y.to_i * 12 + m.to_i) +
    Rational(d * 86400 + h * 3600 + min * 60 + (s + fr), 86400) # 4p
end
            
diff(d1, d2) click to toggle source
 
               # File date/delta.rb, line 185
def self.diff(d1, d2) new(d1.ajd - d2.ajd) end
            
new(delta) click to toggle source
 
               # File date/delta.rb, line 143
def initialize(delta)
  @delta = delta
  @__ca__ = {}
end
            
Also aliased as: new!
new(arg=0, h=0, min=0, s=0) click to toggle source
 
               # File date/delta.rb, line 150
def self.new(arg=0, h=0, min=0, s=0)
  if Hash === arg
    d = Complex(0)
    arg.each do |k, v|
      k = k.to_s.downcase
      unless UNITS4KEY[k]
        raise ArgumentError, "unknown keyword #{k}"
      end
      d += v * UNITS4KEY[k]
    end
  else
    d = dhms_to_delta(0, 0, arg, h, min, s, 0)
  end
  new!(d)
end
            
new!(delta) click to toggle source
Alias for: new
parse(str) click to toggle source
 
               # File date/delta.rb, line 177
def self.parse(str)
  d = begin (@@pa ||= Parser.new).parse(str)
      rescue Racc::ParseError
        raise ArgumentError, 'syntax error'
      end
  new!(d)
end
            

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
            
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
            
days() click to toggle source
 
               # File date/delta.rb, line 215
def days() dhms[2] end
            
dhms() click to toggle source
 
               # File date/delta.rb, line 205
def dhms() self.class.delta_to_dhms(@delta) end
            
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
            
hours() click to toggle source
 
               # File date/delta.rb, line 216
def hours() dhms[3] 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
            
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
            
mins() click to toggle source
Alias for: minutes
minutes() click to toggle source
 
               # File date/delta.rb, line 217
def minutes() dhms[4] end
            
Also aliased as: mins
modulo(n) click to toggle source
 
               # File date/delta.rb, line 295
def modulo(n) dx_conv1(:modulo, n) end
            
months() click to toggle source
 
               # File date/delta.rb, line 214
def months() dhms[1] end
            
nonzero?() click to toggle source
 
               # File date/delta.rb, line 240
def nonzero?() unless zero? then self end 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
            
round() click to toggle source
 
               # File date/delta.rb, line 372
def round() dx_conv0(:round) end
            
sec_fractions() click to toggle source
Alias for: second_fractions
second_fractions() click to toggle source
 
               # File date/delta.rb, line 219
def second_fractions() dhms[6] end
            
Also aliased as: sec_fractions
seconds() click to toggle source
 
               # File date/delta.rb, line 218
def seconds() dhms[5] end
            
Also aliased as: secs
secs() click to toggle source
Alias for: seconds
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(%(%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
            
years() click to toggle source
 
               # File date/delta.rb, line 213
def years() dhms[0] end
            
zero?() click to toggle source
 
               # File date/delta.rb, line 239
def zero?() @delta.zero? end
            

Protected Instance Methods

delta() click to toggle source
 
               # File date/delta.rb, line 209
def delta() @delta end