Module: Abstractive::TimeSpans::Methods

Included in:
Abstractive::TimeSpans, Abstractive::TimeSpans
Defined in:
lib/abstractive/timespans.rb

Constant Summary collapse

MINUTE =
60
HOUR =
MINUTE*60
DAY =
HOUR*24

Instance Method Summary collapse

Instance Method Details

#day_decimal(span) ⇒ Object



25
26
27
28
29
# File 'lib/abstractive/timespans.rb', line 25

def day_decimal(span)
  span = time_spans(span) if span.is_a? Fixnum
  hms = span[1]*HOUR+span[2]*MINUTE+span[3]
  span[0].to_f + (hms.to_f/DAY.to_f)
end

#duration(start, finish) ⇒ Object



59
60
61
# File 'lib/abstractive/timespans.rb', line 59

def duration(start, finish)
  finish.to_i - start.to_i
end

#minus_span(base, sub) ⇒ Object



53
54
55
56
57
# File 'lib/abstractive/timespans.rb', line 53

def minus_span(base, sub)
  sub = day_decimal(sub) unless sub.is_a?(Float)
  _ = DateTime.jd(base.to_datetime.jd - sub)
  DateTime.strptime(_.strftime('%FT%T')+base.strftime('%:z'))
end

#notated_time_length(span) ⇒ Object Also known as: readable_duration



13
14
15
16
17
18
19
20
21
22
# File 'lib/abstractive/timespans.rb', line 13

def notated_time_length(span)
  span = time_spans(span) unless span.is_a? Array
  length = ""
  length += "#{span[0]}d " if span[0] > 0
  length += "#{span[1]}h " if span[1] > 0
  length += "#{span[2]}m " if span[2] > 0
  length += "#{span[3]}s "
  length += "#{span[2]}ms " if span[3] > 0
  length.strip
end

#plus_span(base, add) ⇒ Object



47
48
49
50
51
# File 'lib/abstractive/timespans.rb', line 47

def plus_span(base, add)
  add = day_decimal(add) unless add.is_a?(Float)
  _ = DateTime.jd(base.to_datetime.jd + add)
  DateTime.strptime(_.strftime('%FT%T')+base.strftime('%:z'))
end

#time_spans(length) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abstractive/timespans.rb', line 31

def time_spans(length)
  milliseconds = nil
  if length.is_a?(Float)
    milliseconds = ((length - length.floor) * 100).to_i
    length = length.to_i
  end
  days = (length / DAY).floor
  length = length % DAY if days > 0
  hours = (length / HOUR).floor
  length = length % HOUR if hours > 0
  minutes = (length / MINUTE).floor
  length = length % MINUTE if minutes > 0
  seconds = length
  [days, hours, minutes, seconds, milliseconds]
end