class Tempus

Class to manipulate efficiently time

Example:

>>  duration = Tempus.new
=> #<Tempus:0xb7016b40 @data=0.0>

Constants

HOURS_REGEX
MINUTES_REGEX
OPERATIONS
SECONDS_REGEX
VERSION

Attributes

data[R]

Public Class Methods

new(value = 0, only_hours = true) click to toggle source
# File lib/tempus.rb, line 26
def initialize(value = 0, only_hours = true)
  @parser = Parser.new(only_hours)

  set(value)
end

Public Instance Methods

==(other) click to toggle source
# File lib/tempus.rb, line 110
def ==(other)
  return false unless other.is_a?(Tempus)

  data == other.data
end
format_number_abs(number) click to toggle source
# File lib/tempus.rb, line 62
def format_number_abs(number)
  format('%02<number>d', number: number.to_i.abs)
end
hours() click to toggle source
# File lib/tempus.rb, line 74
def hours
  (data.to_f / 1.hour).to_i
end
human() click to toggle source
# 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() click to toggle source
# File lib/tempus.rb, line 106
def inspect
  "<Tempus seconds=#{data}, formated=#{self}>"
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# 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
minutes() click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/tempus.rb, line 122
def respond_to_missing?(method_name, include_private = false)
  OPERATIONS.include?(method_name.to_sym) || super
end
seconds() click to toggle source
# File lib/tempus.rb, line 66
def seconds
  ((data.to_f - hours.hour - minutes.minute) / 1.second).to_i
end
set(value) click to toggle source
# File lib/tempus.rb, line 32
def set(value)
  @data = @parser.parse(value)
end
to_s(string = '%H:%M:%S') click to toggle source

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"
# 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
Also aliased as: to_string
to_string(string = '%H:%M:%S')
Alias for: to_s
to_xls_time()
Alias for: value_in_days
value_in_days() click to toggle source
# File lib/tempus.rb, line 90
def value_in_days
  to_i / 1.day.to_f
end
Also aliased as: to_xls_time
value_in_hours() click to toggle source

Convert value to hours.

# File lib/tempus.rb, line 81
def value_in_hours
  to_i / 1.hour.to_f
end
value_in_minutes() click to toggle source

Convert value to minutes.

# File lib/tempus.rb, line 86
def value_in_minutes
  to_i / 1.minute.to_f
end