Class: Tempus
- Inherits:
-
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
- OPERATIONS =
i[+ - * /].freeze
- VERSION =
"1.1.1"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(value = 0, only_hours: true) ⇒ Tempus
Returns a new instance of Tempus.
26
27
28
29
30
|
# File 'lib/tempus.rb', line 26
def initialize(value = 0, only_hours: true)
@parser = Parser.new(only_hours: only_hours)
set(value)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
116
117
118
119
120
|
# File 'lib/tempus.rb', line 116
def method_missing(method_name, *args, &block)
return super unless OPERATIONS.include?(method_name)
@data.send(method_name, @parser.parse(args.first)).to_tempus
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
22
23
24
|
# File 'lib/tempus.rb', line 22
def data
@data
end
|
Instance Method Details
#==(other) ⇒ Object
110
111
112
113
114
|
# File 'lib/tempus.rb', line 110
def ==(other)
return false unless other.is_a?(Tempus)
data == other.data
end
|
62
63
64
|
# File 'lib/tempus.rb', line 62
def format_number_abs(number)
format("%02<number>d", number: number.to_i.abs)
end
|
#hours ⇒ Object
74
75
76
|
# File 'lib/tempus.rb', line 74
def hours
(data.to_f / 1.hour).to_i
end
|
#human ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/tempus.rb', line 96
def human
[
("menos" if negative?),
"#{hours.abs} horas",
"#{minutes.abs} minutos",
"e",
"#{seconds.abs} segundos"
].compact.join(" ")
end
|
#inspect ⇒ Object
106
107
108
|
# File 'lib/tempus.rb', line 106
def inspect
"<Tempus seconds=#{data}, formated=#{self}>"
end
|
#minutes ⇒ Object
70
71
72
|
# File 'lib/tempus.rb', line 70
def minutes
((data.to_f - hours.hour) / 1.minute).to_i
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
122
123
124
|
# File 'lib/tempus.rb', line 122
def respond_to_missing?(method_name, include_private = false)
OPERATIONS.include?(method_name.to_sym) || super
end
|
#seconds ⇒ Object
66
67
68
|
# File 'lib/tempus.rb', line 66
def seconds
((data.to_f - hours.hour - minutes.minute) / 1.second).to_i
end
|
#set(value) ⇒ Object
32
33
34
|
# File 'lib/tempus.rb', line 32
def set(value)
@data = @parser.parse(value)
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")
=> "-300503"
50
51
52
53
54
55
56
57
58
|
# File 'lib/tempus.rb', line 50
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
|
#value_in_days ⇒ Object
Also known as:
to_xls_time
90
91
92
|
# File 'lib/tempus.rb', line 90
def value_in_days
to_i / 1.day.to_f
end
|
#value_in_hours ⇒ Object
81
82
83
|
# File 'lib/tempus.rb', line 81
def value_in_hours
to_i / 1.hour.to_f
end
|
#value_in_minutes ⇒ Object
Convert value to minutes.
86
87
88
|
# File 'lib/tempus.rb', line 86
def value_in_minutes
to_i / 1.minute.to_f
end
|