Class: Tempus

Inherits:
Object
  • Object
show all
Defined in:
lib/tempus.rb,
lib/tempus/version.rb

Overview

Class to manipulate efficiently time

Example:

>> duration = Tempus.new

> #<Tempus:0xb7016b40 @data=0.0>

Defined Under Namespace

Classes: Error

Constant Summary collapse

HOURS_REGEX =
/(\%h|\%hh|\%H|\%HH)/.freeze
MINUTES_REGEX =
/(\%m|\%mm|\%M|\%MM)/.freeze
SECONDS_REGEX =
/(\%s|\%ss|\%S|\%SS)/.freeze
VERSION =
'1.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0, only_hours = true) ⇒ Tempus

Returns a new instance of Tempus.



24
25
26
# File 'lib/tempus.rb', line 24

def initialize(value = 0, only_hours = true)
  @data = transform(value, only_hours)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/tempus.rb', line 20

def data
  @data
end

Instance Method Details

#*(other) ⇒ Object

Multiplication values of instance



125
126
127
# File 'lib/tempus.rb', line 125

def *(other)
  Tempus.new(data * transform(other))
end

#+(other) ⇒ Object

Sum values of instance



115
116
117
# File 'lib/tempus.rb', line 115

def +(other)
  Tempus.new(data + transform(other))
end

#-(other) ⇒ Object

Subtract values of instance



120
121
122
# File 'lib/tempus.rb', line 120

def -(other)
  Tempus.new(data - transform(other))
end

#/(other) ⇒ Object

Division values of instance



130
131
132
# File 'lib/tempus.rb', line 130

def /(other)
  Tempus.new(data / transform(other))
end

#==(other) ⇒ Object



148
149
150
151
152
# File 'lib/tempus.rb', line 148

def ==(other)
  return false unless other.is_a?(Tempus)

  data == other.data
end

#format_number_abs(number) ⇒ Object



80
81
82
# File 'lib/tempus.rb', line 80

def format_number_abs(number)
  format('%02d', number.to_i.abs)
end

#from_string(value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/tempus.rb', line 39

def from_string(value)
  str = value.to_s.split(':')
  value = 0

  i[hours minutes seconds].each_with_index do |m, i|
    value += str.at(i).to_i.abs.send(m.to_s)
  end

  str.to_s.include?('-') ? value * -1 : value
end

#from_time(value, only_hours = true) ⇒ Object



50
51
52
# File 'lib/tempus.rb', line 50

def from_time(value, only_hours = true)
  (only_hours ? value - value.beginning_of_day : value).to_f
end

#hoursObject



92
93
94
# File 'lib/tempus.rb', line 92

def hours
  (data.to_f / 1.hour).to_i
end

#humanObject



134
135
136
137
138
139
140
141
142
# File 'lib/tempus.rb', line 134

def human
  [
    ('menos' if negative?),
    "#{hours.abs} horas",
    "#{minutes.abs} minutos",
    'e',
    "#{seconds.abs} segundos"
  ].compact.join(' ')
end

#inspectObject



144
145
146
# File 'lib/tempus.rb', line 144

def inspect
  "<Tempus seconds=#{data}, formated=#{self}>"
end

#minutesObject



88
89
90
# File 'lib/tempus.rb', line 88

def minutes
  ((data.to_f - hours.hour) / 1.minute).to_i
end

#secondsObject



84
85
86
# File 'lib/tempus.rb', line 84

def seconds
  ((data.to_f - hours.hour - minutes.minute) / 1.second).to_i
end

#set(value, only_hours = true) ⇒ Object



28
29
30
# File 'lib/tempus.rb', line 28

def set(value, only_hours = true)
  @data = transform(value, only_hours)
end

#to_s(string = '%H:%M:%S') ⇒ Object Also known as: to_string

Format the duration to para HH:MM:SS .

Sample:

>> duration = Tempus.new(30.hours + 5.minutes + 3.seconds)

> #<Tempus:0xb6e4b860 @data=108303.0>

>> duration.to_s

> “30:05:03”

>> duration = Tempus.new(-30.hours - 5.minutes - 3.seconds)

> #<Tempus:0xb6e4b860 @data=-108303.0>

>> duration.to_s

> “-30:05:03”

>> duration.to_s(“%H:%M”)

> “-30:05”

>> duration.to_s(“%H*%M*%S”)

> “-30*05*03”



68
69
70
71
72
73
74
75
76
# File 'lib/tempus.rb', line 68

def to_s(string = '%H:%M:%S')
  text = string.dup

  text['%'] = '-%' if text != '' && negative?

  text = text.gsub(HOURS_REGEX, format_number_abs(hours))
  text = text.gsub(MINUTES_REGEX, format_number_abs(minutes))
  text.gsub(SECONDS_REGEX, format_number_abs(seconds))
end

#transform(value, only_hours = true) ⇒ Object



32
33
34
35
36
37
# File 'lib/tempus.rb', line 32

def transform(value, only_hours = true)
  return from_string(value).to_f if value.is_a?(String)
  return from_time(value, only_hours).to_f if value.is_a?(Time)

  value.to_f
end

#value_in_daysObject Also known as: to_xls_time



108
109
110
# File 'lib/tempus.rb', line 108

def value_in_days
  to_i / 1.day.to_f
end

#value_in_hoursObject

Convert value to hours.



99
100
101
# File 'lib/tempus.rb', line 99

def value_in_hours
  to_i / 1.hour.to_f
end

#value_in_minutesObject

Convert value to minutes.



104
105
106
# File 'lib/tempus.rb', line 104

def value_in_minutes
  to_i / 1.minute.to_f
end