Class: Date::Delta

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/date/delta.rb,
lib/date/delta/parser.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

UNIT_PREFIXES =
{
  'yotta'      => Rational(10**24),
  'zetta'      => Rational(10**21),
  'exa'        => Rational(10**18),
  'peta'       => Rational(10**15),
  'tera'       => Rational(10**12),
  'giga'       => Rational(10**9),
  'mega'       => Rational(10**6),
  'kilo'       => Rational(10**3),
  'hecto'      => Rational(10**2),
  'deca'       => Rational(10**1),
  'deka'       => Rational(10**1),
  'deci'       => Rational(1, 10**1),
  'centi'      => Rational(1, 10**2),
  'milli'      => Rational(1, 10**3),
  'decimilli'  => Rational(1, 10**4),
  'centimilli' => Rational(1, 10**5),
  'micro'      => Rational(1, 10**6),
  'nano'       => Rational(1, 10**9),
  'millimicro' => Rational(1, 10**9),
  'pico'       => Rational(1, 10**12),
  'micromicro' => Rational(1, 10**12),
  'femto'      => Rational(1, 10**15),
  'atto'       => Rational(1, 10**18),
  'zepto'      => Rational(1, 10**21),
  'yocto'      => Rational(1, 10**24)
}
IUNITS =
{
  'year'       => Complex(0, 12),
  'month'      => Complex(0, 1)
}
RUNITS =
{
  'day'        => Rational(1),
  'week'       => Rational(7),
  'sennight'   => Rational(7),
  'fortnight'  => Rational(14),
  'hour'       => Rational(1, 24),
  'minute'     => Rational(1, 1440),
  'second'     => Rational(1, 86400)
}
UNITS =
{}
UNITS4KEY =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delta) ⇒ Delta

Returns a new instance of Delta.



143
144
145
146
# File 'lib/date/delta.rb', line 143

def initialize(delta)
  @delta = delta
  @__ca__ = {}
end

Class Method Details

.delta_to_dhms(delta) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/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) ⇒ Object



137
138
139
140
141
# File 'lib/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) ⇒ Object



185
# File 'lib/date/delta.rb', line 185

def self.diff(d1, d2) new(d1.ajd - d2.ajd) end

.minsObject



174
# File 'lib/date/delta.rb', line 174

alias_method :mins, :minutes

.new(arg = 0, h = 0, min = 0, s = 0) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/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!Object



148
# File 'lib/date/delta.rb', line 148

alias_method :new!, :new

.once(*ids) ⇒ Object

:nodoc: – restricted



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/date/delta.rb', line 189

def once(*ids) # :nodoc: -- restricted
	for id in ids
	  module_eval <<-"end;"
	    alias_method :__#{id.object_id}__, :#{id.to_s}
	    private :__#{id.object_id}__
	    def #{id.to_s}(*args)
 @__ca__[#{id.object_id}] ||= __#{id.object_id}__(*args)
	    end
	  end;
  end
end

.parse(str) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/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

.secsObject



175
# File 'lib/date/delta.rb', line 175

alias_method :secs, :seconds

Instance Method Details

#%(n) ⇒ Object



292
# File 'lib/date/delta.rb', line 292

def % (n) dx_conv1(:%, n) end

#*(n) ⇒ Object



274
# File 'lib/date/delta.rb', line 274

def * (n) dx_muldiv(:*, n) end

#**(n) ⇒ Object



314
# File 'lib/date/delta.rb', line 314

def ** (n) dx_conv1(:**, n) end

#+(n) ⇒ Object



259
# File 'lib/date/delta.rb', line 259

def + (n) dx_addsub(:+, n) end

#+@Object



245
# File 'lib/date/delta.rb', line 245

def +@ () self.class.new!(+@delta) end

#-(n) ⇒ Object



260
# File 'lib/date/delta.rb', line 260

def - (n) dx_addsub(:-, n) end

#-@Object



244
# File 'lib/date/delta.rb', line 244

def -@ () self.class.new!(-@delta) end

#/(n) ⇒ Object



275
# File 'lib/date/delta.rb', line 275

def / (n) dx_muldiv(:/, n) end

#<=>(other) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/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) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/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

#absObject



368
# File 'lib/date/delta.rb', line 368

def abs() dx_conv0(:abs) end

#ceilObject



370
# File 'lib/date/delta.rb', line 370

def ceil() dx_conv0(:ceil) end

#coerce(other) ⇒ Object



348
349
350
351
352
353
354
# File 'lib/date/delta.rb', line 348

def coerce(other)
  case other
  when Numeric; return other, @delta
  else
	super
  end
end

#daysObject



215
# File 'lib/date/delta.rb', line 215

def days() dhms[2] end

#dhmsObject



205
# File 'lib/date/delta.rb', line 205

def dhms() self.class.delta_to_dhms(@delta) end

#div(n) ⇒ Object



294
# File 'lib/date/delta.rb', line 294

def div(n) dx_conv1(:div, n) end

#divmod(n) ⇒ Object



296
# File 'lib/date/delta.rb', line 296

def divmod(n) [div(n), modulo(n)] end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


356
# File 'lib/date/delta.rb', line 356

def eql? (other) Delta === other && self == other end

#floorObject



371
# File 'lib/date/delta.rb', line 371

def floor() dx_conv0(:floor) end

#hashObject



357
# File 'lib/date/delta.rb', line 357

def hash() @delta.hash end

#hoursObject



216
# File 'lib/date/delta.rb', line 216

def hours() dhms[3] end

#inspectObject



382
# File 'lib/date/delta.rb', line 382

def inspect() format('#<%s: %s (%s)>', self.class, to_s, @delta) end

#integer?Boolean

Returns:

  • (Boolean)


242
# File 'lib/date/delta.rb', line 242

def integer? () @delta.imag == 0 && @delta.real.integer? end

#marshal_dumpObject



392
# File 'lib/date/delta.rb', line 392

def marshal_dump() @delta end

#marshal_load(a) ⇒ Object



394
395
396
397
# File 'lib/date/delta.rb', line 394

def marshal_load(a)
  @delta = a
  @__ca__ = {}
end

#minutesObject Also known as: mins



217
# File 'lib/date/delta.rb', line 217

def minutes() dhms[4] end

#modulo(n) ⇒ Object



295
# File 'lib/date/delta.rb', line 295

def modulo(n) dx_conv1(:modulo, n) end

#monthsObject



214
# File 'lib/date/delta.rb', line 214

def months() dhms[1] end

#nonzero?Boolean

Returns:

  • (Boolean)


240
# File 'lib/date/delta.rb', line 240

def nonzero?() unless zero? then self end end

#quo(n) ⇒ Object



315
# File 'lib/date/delta.rb', line 315

def quo(n) dx_muldiv(:quo, n) end

#quotient(n) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/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) ⇒ Object



312
# File 'lib/date/delta.rb', line 312

def quotrem(n) [quotient(n), remainder(n)] end

#remainder(n) ⇒ Object



311
# File 'lib/date/delta.rb', line 311

def remainder(n) dx_conv1(:remainder, n) end

#roundObject



372
# File 'lib/date/delta.rb', line 372

def round() dx_conv0(:round) end

#second_fractionsObject Also known as: sec_fractions



219
# File 'lib/date/delta.rb', line 219

def second_fractions() dhms[6] end

#secondsObject Also known as: secs



218
# File 'lib/date/delta.rb', line 218

def seconds() dhms[5] end

#to_cObject



378
# File 'lib/date/delta.rb', line 378

def to_c() @delta end

#to_fObject



376
# File 'lib/date/delta.rb', line 376

def to_f() dx_conv0(:to_f) end

#to_iObject Also known as: to_int



375
# File 'lib/date/delta.rb', line 375

def to_i() dx_conv0(:to_i) end

#to_rObject



377
# File 'lib/date/delta.rb', line 377

def to_r() dx_conv0(:to_r) end

#to_sObject



384
385
386
387
388
389
390
# File 'lib/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

#truncateObject



373
# File 'lib/date/delta.rb', line 373

def truncate() dx_conv0(:truncate) end

#yearsObject



213
# File 'lib/date/delta.rb', line 213

def years() dhms[0] end

#zero?Boolean

Returns:

  • (Boolean)


239
# File 'lib/date/delta.rb', line 239

def zero?() @delta.zero? end