Files
dunlop-core/app/services/day_time_seconds.rb
T
2018-01-20 13:02:44 -03:00

32 lines
678 B
Ruby

# Helper class for time of day storage in database as integer value
class DayTimeSeconds
include DayTimeBase
def to_s
return '' unless number
hours, minutes_part = number.divmod(3600)
minutes, seconds_part = minutes_part.divmod(60)
[hours, minutes, seconds_part.round].map{|v| v.to_s.rjust(2, '0')}.join(':')
end
def seconds
number.to_i
end
def minutes
number.to_f / 60.0
end
def hours
number.to_f / 3600.0
end
class << self
def string_to_number(string)
return nil unless string.present?
hours, minutes, seconds = string.split(':')
hours.to_i * 3600 + minutes.to_i * 60 + seconds.to_i
end
end
end