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 オブジェクトの初期化

Parameters:

  • value (Numeric)

    Duration値 / 128秒



333
334
335
# File 'lib/when_exe/tmduration.rb', line 333

def initialize(value)
  @duration = value
end

Instance Attribute Details

#durationNumeric

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

Returns:



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

def duration
  @duration
end

#repeatBoolean

繰り返し有無

Returns:

  • (Boolean)


537
538
539
# File 'lib/when_exe/tmobjects.rb', line 537

def repeat
  @repeat
end

Class Method Details

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

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

Parameters:

  • value (Array<Numeric>)

    ( 日, 時, 分, 秒 ) 下位の桁は省略可

Returns:



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 を生成する

Parameters:

Returns:



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

Instance Method Details

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

乗算

Parameters:

Returns:



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

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

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

加算

Parameters:

Returns:

  • (When::TM::Duration)

    (other が Numeric, When::TM::Duration の場合)

  • (other と同じクラス)

    (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

減算

Parameters:

Returns:



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

#-@When::TM::Duration

符号反転

Returns:



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

def -@
  Duration.new(-@duration)
end

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

除算

Parameters:

Returns:



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

比較

Parameters:

Returns:

  • (Integer)

    other との比較により、負,0,正の値を返す



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

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

#==(other) ⇒ Boolean

オブジェクトの同値

Parameters:

Returns:

  • (Boolean)
    true - 同値
    false - 非同値


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

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

Parameters:

  • n (Integer)

    要素のインデクス

Returns:

  • (Numeric)

    (秒は Float, その他は Integer)



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 の生成

Overloads:

  • #initialize(range, count_limit = nil) ⇒ Object

    Parameters:

    • range (Range, When::Parts::GeometricComplex)
      始点 - range.first
      終点 - range.last
    • count_limit (Integer) (defaults to: nil)

      繰り返し回数(デフォルトは指定なし)

  • #initialize(first, direction, count_limit = nil) ⇒ Object

    Parameters:

    • first (When::TM::TemporalPosition)

      始点

    • direction (Symbol)
      :forward - 昇順
      :reverse - 降順
    • count_limit (Integer) (defaults to: nil)

      繰り返し回数(デフォルトは指定なし)



630
631
632
# File 'lib/when_exe/tmobjects.rb', line 630

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

#absWhen::TM::Duration

絶対値

Returns:



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の時間間隔だけ後の時刻オブジェクト

Parameters:

Returns:

  • (引数と同種の時刻オブジェクト)


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

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

#apply_delayed_options(options) ⇒ When::TM::Duration

オブジェクト変換オプションの遅延適用(ダミー)

Parameters:

  • options (Hash)

    以下の通り

Options Hash (options):

Returns:



575
576
577
# File 'lib/when_exe/tmobjects.rb', line 575

def apply_delayed_options(options)
  self
end

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

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

Parameters:

Returns:

  • (引数と同種の時刻オブジェクト)


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

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

#coerce(other) ⇒ Object

coerce



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

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

#inspectString

When::TM::Duration オブジェクトを分かりやすい文字列にして返します

Returns:



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

def inspect
  to_s
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

#set_repeat(repeat) ⇒ When::TM::Duration

繰り返し設定

Parameters:

  • repeat (Boolean)

    設定

Returns:



585
586
587
588
589
# File 'lib/when_exe/tmobjects.rb', line 585

def set_repeat(repeat)
  repeated = dup
  repeated.repeat = repeat
  repeated
end

#signInteger

符号

Returns:

  • (Integer)

    0 との比較により、負,0,正の値を返す



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

def sign
  @duration <=> 0
end

#to_as_durationActiveSupport::Duration

ActiveSupport::Duration への変換

Returns:

  • (ActiveSupport::Duration)


314
315
316
317
318
319
320
# File 'lib/when_exe/tmduration.rb', line 314

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に変換する

Parameters:

  • n (Integer) (defaults to: When::SECOND)

    最下位要素のインデクス(デフォルト When::SECOND)

Returns:

  • (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 への変換

Returns:



305
306
307
# File 'lib/when_exe/tmduration.rb', line 305

def to_duration
  Duration.new(duration)
end

#to_fFloat Also known as: to_float

時間間隔の長さ / 秒

Returns:



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

def to_f
  duration / SECOND
end

#to_iInteger Also known as: to_int

時間間隔の長さ / 秒

Returns:

  • (Integer)

    (四捨五入値)



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 への変換



545
546
547
548
549
550
551
# File 'lib/when_exe/tmobjects.rb', line 545

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 への変換



558
559
560
561
562
563
564
# File 'lib/when_exe/tmobjects.rb', line 558

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

文字列化

Returns:



294
295
296
# File 'lib/when_exe/tmduration.rb', line 294

def to_s
  to_dhms.to_s
end

#unitNumeric

Note:

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

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

Returns:



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