Does Ruby have an equivalent to TimeSpan in C#?
Asked Answered
F

5

14

In C# there is a TimeSpan class. It represents a period of time and is returned from many date manipulation options. You can create one and add or subtract from a date etc.

In Ruby and specifically rails there seems to be lots of date and time classes but nothing that represents a span of time?

Ideally I'd like an object that I could use for outputting formatted dates easily enough using the standard date formatting options.

eg.

ts.to_format("%H%M")

Is there such a class?

Even better would be if I could do something like

   ts = end_date - start_date

I am aware that subtracting of two dates results in the number of seconds separating said dates and that I could work it all out from that.

Furunculosis answered 19/6, 2012 at 12:17 Comment(0)
F
1

In the end I forked the suggestion in @tokland's answer. Not quite sure how to make it a proper gem but it's currently working for me:

Timespan fork of time_diff

Furunculosis answered 20/6, 2012 at 2:45 Comment(0)
C
6

You can do something similar like this:

irb(main):001:0> require 'time'         => true
irb(main):002:0> initial = Time.now     => Tue Jun 19 08:19:56 -0400 2012
irb(main):003:0> later = Time.now       => Tue Jun 19 08:20:05 -0400 2012
irb(main):004:0> span = later - initial => 8.393871
irb(main):005:0>

This just returns a time in seconds which isn't all that pretty to look at, you can use the strftime() function to make it look pretty:

irb(main):010:0> Time.at(span).gmtime.strftime("%H:%M:%S") => "00:00:08"
Crybaby answered 19/6, 2012 at 12:21 Comment(3)
This happens to be exactly what I'm doing at the moment. It does have the limitation of not being able to show more than 24 hours of time difference.Furunculosis
@Furunculosis The Time is actually an abstraction of Dates and Times, which may be confusing given the name. You can make Time objects for a given Year/Month/Day/Hour/Minute/Second and perform addition and subtraction on them. ruby-doc.org/core-1.9.3/Time.htmlCrybaby
That's the interesting thing about a timespan though, it has no concept of it's place in reference to something else. It's merely a glorified int with some nice accessor methods.Furunculosis
N
3

Something like this? https://github.com/abhidsm/time_diff

require 'time_diff'
time_diff_components = Time.diff(start_date_time, end_date_time)
Negrito answered 19/6, 2012 at 12:25 Comment(1)
That looks really interesting thank you. Not quite what I wanted but I can definitely work with it or a derivation.Furunculosis
F
1

No, it doesn't. You can just add seconds or use advance method.

end_date - start_date will have Float type

Forked answered 19/6, 2012 at 12:21 Comment(0)
F
1

In the end I forked the suggestion in @tokland's answer. Not quite sure how to make it a proper gem but it's currently working for me:

Timespan fork of time_diff

Furunculosis answered 20/6, 2012 at 2:45 Comment(0)
G
0

Not yet @toxaq... but I've started something!

https://gist.github.com/thatandyrose/6180560

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
Grimaud answered 8/8, 2013 at 1:51 Comment(1)
Cool, very similar to what's happening in the gem tokland suggested, which I forked to get what I wanted happening.Furunculosis

© 2022 - 2024 — McMap. All rights reserved.