Last active
December 20, 2015 19:08
-
-
Save thatandyrose/6180560 to your computer and use it in GitHub Desktop.
very simple class to make calculation spans of time super easy. Inspired by .NET's TimeSpan class, still lots of room for improvement! For Rails I recommend putting it in /config/initializers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimeSpan | |
attr_accessor :milliseconds | |
def self.from_milliseconds(milliseconds) | |
me = TimeSpan.new | |
me.milliseconds = milliseconds | |
return me | |
end | |
def self.from_seconds(seconds) | |
TimeSpan.from_milliseconds(seconds.to_d * 1000) | |
end | |
def self.from_minutes(minutes) | |
TimeSpan.from_milliseconds(minutes.to_d * 60000) | |
end | |
def self.from_hours(hours) | |
TimeSpan.from_milliseconds(hours.to_d * 3600000) | |
end | |
def self.from_days(days) | |
TimeSpan.from_milliseconds(days.to_d * 86400000) | |
end | |
def self.from_years(years) | |
TimeSpan.from_days(years.to_d * 365.242) | |
end | |
def self.diff(start_date_time, end_date_time) | |
TimeSpan.from_seconds(end_date_time - start_date_time) | |
end | |
def seconds | |
self.milliseconds.to_d * 0.001 | |
end | |
def minutes | |
self.seconds.to_d * 0.0166667 | |
end | |
def hours | |
self.minutes.to_d * 0.0166667 | |
end | |
def days | |
self.hours.to_d * 0.0416667 | |
end | |
def years | |
self.days.to_d * 0.00273791 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment