Skip to content

Instantly share code, notes, and snippets.

@jerryclinesmith
Created November 22, 2013 16:50
Show Gist options
  • Save jerryclinesmith/7603102 to your computer and use it in GitHub Desktop.
Save jerryclinesmith/7603102 to your computer and use it in GitHub Desktop.
Measure difference between 2 times
class TimeSpan
def initialize(seconds)
@seconds = (seconds || 0).abs
end
def in_seconds
@seconds
end
def in_full_seconds
in_seconds.floor
end
def in_minutes
@seconds.to_f / 60
end
def in_full_minutes
in_minutes.floor
end
def in_hours
@seconds.to_f / (60 * 60)
end
def in_full_hours
in_hours.floor
end
def in_days
@seconds.to_f / (24 * 60 * 60)
end
def in_full_days
in_days.floor
end
def in_weeks
@seconds.to_f / (7 * 24 * 60 * 60)
end
def in_full_weeks
in_weeks.floor
end
def in_months
@seconds.to_f / (30.4375 * 24 * 60 * 60)
end
def in_full_months
in_months.floor
end
def in_years
@seconds.to_f / (365.25 * 24 * 60 * 60)
end
def in_full_years
in_years.floor
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment