Sha256: 7d78dff5eca4354e06d26d84b8e6a3df84aa6d307cad324894982df0ba9f9a62

Contents?: true

Size: 1009 Bytes

Versions: 19

Compression:

Stored size: 1009 Bytes

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}" unless args.is_a?(Hash)
    raise "No time given." unless args[:time]
    raise "Invalid time given: #{args[:time].class.name}" unless args[:time].is_a?(String)

    match = args[:time].match(/^(\d+):(\d+):(\d+)$/)
    raise "Could not understand time format." unless 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
    (@total_secs.to_f / 3600)
  end

  # Return the total amount of minutes.
  def mins_total
    (@total_secs.to_f / 60)
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
baza-0.0.38 lib/baza/dbtime.rb
baza-0.0.37 lib/baza/dbtime.rb
baza-0.0.36 lib/baza/dbtime.rb
baza-0.0.35 lib/baza/dbtime.rb
baza-0.0.34 lib/baza/dbtime.rb
baza-0.0.33 lib/baza/dbtime.rb
baza-0.0.32 lib/baza/dbtime.rb
baza-0.0.31 lib/baza/dbtime.rb
baza-0.0.30 lib/baza/dbtime.rb
baza-0.0.29 lib/baza/dbtime.rb
baza-0.0.28 lib/baza/dbtime.rb
baza-0.0.27 lib/baza/dbtime.rb
baza-0.0.26 lib/baza/dbtime.rb
baza-0.0.25 lib/baza/dbtime.rb
baza-0.0.24 lib/baza/dbtime.rb
baza-0.0.23 lib/baza/dbtime.rb
baza-0.0.22 lib/baza/dbtime.rb
baza-0.0.21 lib/baza/dbtime.rb
baza-0.0.20 lib/baza/dbtime.rb