Class: When::TM::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/when_exe/tmobjects.rb,
lib/when_exe/tmduration.rb,
lib/when_exe/core/duration.rb

Overview

When::TM::Duration

Direct Known Subclasses

IntervalLength, PeriodDuration

Defined Under Namespace

Classes: Enumerator

Constant Summary collapse

SYSTEM =

物理的な時間間隔の定数

時間の「秒」を Float で表現して丸め誤差が発生しない範囲で、
もっとも大きな時間間隔(86400s を 2 の因数で割りつくした値)
を単位 SYSTEM とする
1.0
DAY =
SYSTEM * 675
YEAR =
DAY    * 365.2425
MONTH =
YEAR   /  12
WEEK =
DAY    *   7
HOUR =
DAY    /  24
MINUTE =
HOUR   /  60
SECOND =
MINUTE /  60
UnitName =
{YEAR=>'year', MONTH =>'month',  WEEK  =>'week',   DAY   =>'day',
HOUR=>'hour', MINUTE=>'minute', SECOND=>'second', SYSTEM=>'system'}
Unit =
UnitName.invert
DurationUnits =
[DAY, HOUR, MINUTE, SECOND]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Duration

Duration オブジェクトの初期化


325
326
327
# File 'lib/when_exe/tmduration.rb', line 325

def initialize(value)
  @duration = value
end

Instance Attribute Details

#durationNumeric

時間間隔の長さ (128秒単位)


96
97
98
# File 'lib/when_exe/tmduration.rb', line 96

def duration
  @duration
end

Class Method Details

.dhms(*value) ⇒ When::TM::Duration

日時分秒からのオブジェクト生成


70
71
72
73
# File 'lib/when_exe/tmduration.rb', line 70

def dhms(*value)
  units = DurationUnits.dup
  Duration.new(value.inject(0) {|s,v| s + v * units.shift})
end

.unitWhen::TM::Duration

Note:

unit は second, minute, hour, day, week に読み替える

メソッド名に相当する単位で表した value に対応する When::TM::Duration を生成する


# File 'lib/when_exe/tmduration.rb', line 75

Instance Method Details

#*(other) ⇒ When::TM::Duration

乗算


213
214
215
# File 'lib/when_exe/tmduration.rb', line 213

def *(other)
  Duration.new(@duration * other)
end

#+(other) ⇒ When::TM::Duration, other と同じクラス

加算


187
188
189
190
191
192
193
# File 'lib/when_exe/tmduration.rb', line 187

def +(other)
  case other
  when Duration ; Duration.new(@duration + other.duration)
  when Numeric  ; Duration.new(@duration + other * SECOND)
  else          ; other + self
  end
end

#-(other) ⇒ When::TM::Duration

減算


202
203
204
# File 'lib/when_exe/tmduration.rb', line 202

def -(other)
  Duration.new(@duration - (other.kind_of?(Duration) ? other.duration : other * SECOND))
end

#[email protected]When::TM::Duration

符号反転


135
136
137
# File 'lib/when_exe/tmduration.rb', line 135

def [email protected]
  Duration.new(-@duration)
end

#/(other) ⇒ When::TM::Duration, Numeric

除算


225
226
227
# File 'lib/when_exe/tmduration.rb', line 225

def /(other)
  other.kind_of?(Duration) ? @duration / other.duration : Duration.new(@duration / other)
end

#<=>(other) ⇒ Integer

比較


161
162
163
# File 'lib/when_exe/tmduration.rb', line 161

def <=>(other)
  self.to_f <=> other.to_f
end

#==(other) ⇒ Boolean

オブジェクトの同値


173
174
175
176
# File 'lib/when_exe/tmduration.rb', line 173

def ==(other)
  return false unless other.instance_of?(self.class)
  return self.duration == other.duration
end

#[](n) ⇒ Numeric

時間間隔の要素を取り出す


254
255
256
# File 'lib/when_exe/tmduration.rb', line 254

def [](n)
  to_dhms([n+1,When::SECOND].min)[n]
end

#initialize(range, count_limit = nil) ⇒ Object #initialize(first, direction, count_limit = nil) ⇒ Object Also known as: to_enum, enum_for

Enumerator の生成


593
594
595
# File 'lib/when_exe/tmobjects.rb', line 593

def _enumerator(*args)
  return Enumerator.new(*args.unshift(self))
end

#absWhen::TM::Duration

絶対値


143
144
145
# File 'lib/when_exe/tmduration.rb', line 143

def abs
  sign >= 0 ? self.dup : -self
end

#after(time = Time.now) ⇒ 引数と同種の時刻オブジェクト Also known as: since

指定時刻よりselfの時間間隔だけ後の時刻オブジェクト


265
266
267
# File 'lib/when_exe/tmduration.rb', line 265

def after(time=Time.now)
  time + self
end

#before(time = Time.now) ⇒ 引数と同種の時刻オブジェクト Also known as: ago

指定時刻よりselfの時間間隔だけ前の時刻オブジェクト


277
278
279
# File 'lib/when_exe/tmduration.rb', line 277

def before(time=Time.now)
  time - self
end

#coerce(other) ⇒ Object

coerce


316
317
318
# File 'lib/when_exe/tmduration.rb', line 316

def coerce(other)
  [other, @duration]
end

#rational_durationObject


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/when_exe/core/duration.rb', line 17

def rational_duration
  unless @rational_duration
    sec = duration / When::TM::Duration::SECOND
    mod = sec % When::TM::Duration::DAY.to_i
    if mod == 0 || mod != mod.to_i
      @rational_duration = @duration / When::TM::Duration::DAY
    else
      @rational_duration = Rational(sec.to_i, 86400)
    end
  end

  @rational_duration
end

#signInteger

符号


151
152
153
# File 'lib/when_exe/tmduration.rb', line 151

def sign
  @duration <=> 0
end

#to_as_durationActiveSupport::Duration

ActiveSupport::Duration への変換


306
307
308
309
310
311
312
# File 'lib/when_exe/tmduration.rb', line 306

def to_as_duration
  [[:weeks, WEEK], [:days, DAY], [:hours, HOUR], [:minutes, MINUTE], [:seconds, SECOND]].each do |unit|
    div, mod = duration.divmod(unit[1])
    return div.send(unit[0]) if mod == 0
  end
  (duration / SECOND).seconds
end

#to_dhms(n = When::SECOND) ⇒ Array

時間間隔を日時分秒を表すArrayに変換する


236
237
238
239
240
241
242
243
244
245
# File 'lib/when_exe/tmduration.rb', line 236

def to_dhms(n=When::SECOND)
  a = []
  m = @duration
  n.times do |i|
    d, m = m.divmod(DurationUnits[i])
    a << d
  end
  a << m / DurationUnits[n]
  a
end

#to_durationWhen::TM::Duration

Note:

必ずコピーを作る

When::TM::Duration への変換


297
298
299
# File 'lib/when_exe/tmduration.rb', line 297

def to_duration
  Duration.new(duration)
end

#to_fFloat Also known as: to_float

時間間隔の長さ / 秒


117
118
119
# File 'lib/when_exe/tmduration.rb', line 117

def to_f
  duration / SECOND
end

#to_iInteger Also known as: to_int

時間間隔の長さ / 秒


126
127
128
# File 'lib/when_exe/tmduration.rb', line 126

def to_i
  to_f.round
end

#to_interval_lengthWhen::TM::IntervalLength

When::TM::IntervalLength への変換


538
539
540
541
542
543
544
# File 'lib/when_exe/tmobjects.rb', line 538

def to_interval_length
  [['week', WEEK], ['day', DAY], ['hour', HOUR], ['minute', MINUTE], ['second', SECOND]].each do |unit|
    div, mod = duration.divmod(unit[1])
    return When::TM::IntervalLength.new(div, unit[0]) if mod == 0
  end
  When::TM::IntervalLength.new(duration / SECOND, 'second')
end

#to_period_durationWhen::TM::PeriodDuration

When::TM::PeriodDuration への変換


551
552
553
554
555
556
557
# File 'lib/when_exe/tmobjects.rb', line 551

def to_period_duration
  [[When::WEEK, WEEK], [When::DAY, DAY], [When::HOUR, HOUR], [When::MINUTE, MINUTE], [When::SECOND, SECOND]].each do |unit|
    div, mod = duration.divmod(unit[1])
    return When::TM::PeriodDuration.new(div, unit[0]) if mod == 0
  end
  When::TM::PeriodDuration.new(duration / SECOND, When::SECOND)
end

#to_sString

文字列化


286
287
288
# File 'lib/when_exe/tmduration.rb', line 286

def to_s
  to_dhms.to_s
end

#unitNumeric

Note:

unit は second, minute, hour, day, week に読み替える

メソッド名に相当する単位で表した時間間隔の大きさ


# File 'lib/when_exe/tmduration.rb', line 98