Sha256: 019b1840b45f53a0f7ef5800a5f804cbc4adbbe7e651b241671eb712a6d73b10

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

#This class helps handeling time-columns in databases.
class Baza::Dbtime
  #These variables return information about the object.
  attr_reader :hours, :mins, :secs, :total_secs
  
  #Initializes the object from arguments useually given by Baza::Datarow.
  def initialize(args)
    args = {:time => args} if args.is_a?(String)
    
    raise "Invalid arguments given: #{args}" if !args.is_a?(Hash)
    raise "No time given." if !args[:time]
    raise "Invalid time given: #{args[:time].class.name}" if !args[:time].is_a?(String)
    
    match = args[:time].match(/^(\d+):(\d+):(\d+)$/)
    raise "Could not understand time format." if !match
    
    @hours = match[1].to_i
    @mins = match[2].to_i
    @secs = match[3].to_i
    
    @total_secs = @hours * 3600
    @total_secs += @mins * 60
    @total_secs += @secs
  end
  
  #Returns the total amount of hours.
  def hours_total
    return (@total_secs.to_f / 3600)
  end
  
  #Return the total amount of minutes.
  def mins_total
    return (@total_secs.to_f / 60)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
baza-0.0.19 lib/baza/dbtime.rb
baza-0.0.18 lib/baza/dbtime.rb
baza-0.0.17 include/dbtime.rb
baza-0.0.16 include/dbtime.rb
baza-0.0.15 include/dbtime.rb
baza-0.0.14 include/dbtime.rb