Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created June 14, 2012 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/2929793 to your computer and use it in GitHub Desktop.
Save peterhellberg/2929793 to your computer and use it in GitHub Desktop.
Slot length calculation
# encoding: utf-8
require 'time'
class SlotLength
# Contains the number of seconds in the slot
attr_reader :seconds
attr_reader :ordered_seconds
# Contains the string for the slot length
attr_reader :length
# Contains the string for the ordered length
attr_reader :ordered_length
# Creates an instance of a SlotLength
#
# @param [String] length
#
def initialize(length)
unless length.match /^\d\d:\d\d:\d\d$/
raise ArgumentError.new("Invalid length format")
end
# Do some calculations
@length = length
@seconds = calculate_seconds(@length)
@ordered_seconds = calculate_ordered_seconds(@seconds)
@ordered_length = generate_ordered_length(@ordered_seconds)
end
# Return the length string
#
# @return [String]
#
def to_s
length
end
private
# Calculate the number of seconds
#
# @return [Fixnum]
#
def calculate_seconds(length)
Time.parse(length).to_i - day_start_tick
end
# Calculate the number of seconds for the ordered length
#
# @return [Fixnum]
#
def calculate_ordered_seconds(seconds)
seconds < 1110 ? seconds-60 : seconds-90
end
# Generate the ordered length string
#
# @return [String]
#
def generate_ordered_length(ordered_seconds)
Time.at(day_start_tick+ordered_seconds).strftime('%H:%M:%S')
end
# Get the tick for the start of the day
#
# @return [Fixnum]
#
def day_start_tick
Time.parse('00:00:00').to_i
end
end
# encoding: utf-8
require_relative "spec_helper"
require_relative "../lib/slot_length"
describe SlotLength do
subject { SlotLength }
it "raises an ArgumentError if the length string is invalid" do
-> { s('foo') }.must_raise ArgumentError
end
describe "seconds" do
it "returns the number of seconds in the slot" do
s('00:08:00').seconds.must_equal 480
s('00:30:00').seconds.must_equal 1800
s('00:50:00').seconds.must_equal 3000
end
end
describe "ordered_seconds" do
it "returns the number of seconds for the ordered length" do
s('00:05:00').ordered_seconds.must_equal 240 # 4 min
s('00:10:00').ordered_seconds.must_equal 540 # 9 min
s('00:15:00').ordered_seconds.must_equal 840 # 14 min
s('00:20:00').ordered_seconds.must_equal 1110 # 18 min 30 sec
s('00:30:00').ordered_seconds.must_equal 1710 # 28 min 30 sec
s('01:00:00').ordered_seconds.must_equal 3510 # 58 min 30 sec
s('02:00:00').ordered_seconds.must_equal 7110 # 1 hour 58 min 30 sec
end
end
describe "ordered_length_string" do
it "returns the string for the ordered length" do
s('00:05:00').ordered_length.must_equal '00:04:00'
s('00:10:00').ordered_length.must_equal '00:09:00'
s('00:15:00').ordered_length.must_equal '00:14:00'
s('00:20:00').ordered_length.must_equal '00:18:30'
s('00:30:00').ordered_length.must_equal '00:28:30'
s('01:00:00').ordered_length.must_equal '00:58:30'
s('02:00:00').ordered_length.must_equal '01:58:30'
end
end
describe "to_s" do
it "returns the initial length string" do
s('00:20:00').to_s.must_equal '00:20:00'
"#{s('00:10:00')}".must_equal '00:10:00'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment