Time without Date

Is there any class or lib to deal with Times without Date? (Eg “3:30
PM” - but I don’t want it associated with a date.)

Ideally, I’d like to:

  • Have clock arithmetic (1 AM minus 90 minutes is 11:30 PM)
  • Convert from DateTime to dateless times (and, by adding a date, back)
  • Store it with ActiveRecord

Any ideas?

List R. wrote:

Is there any class or lib to deal with Times without Date? (Eg “3:30
PM” - but I don’t want it associated with a date.)

Ideally, I’d like to:

  • Have clock arithmetic (1 AM minus 90 minutes is 11:30 PM)
  • Convert from DateTime to dateless times (and, by adding a date, back)
  • Store it with ActiveRecord

Any ideas?

Have you tried the Time class?

#!/usr/bin/ruby -w

is = Time.new

h = 4 # hours

was = is - (h * 60 * 60)

puts “Time now: #{is}\nTime then: #{was}”

Output:

Time now: Thu Oct 26 14:10:41 PDT 2006
Time then: Thu Oct 26 10:10:41 PDT 2006

The Time object is expressed in seconds elapsed since 1/1/1970 00:00
GMT.
Just add or subtract seconds to get what you want. And the 1970
limitation
isn’t actually much of a limitation:

irb(main):008:0> t = Time.new - (100 * 60 * 60 * 24 * 365.25)
=> Fri Oct 26 13:16:52 PST 1906

The problem with Time is that it tracks a date also.

Let me explain:

I have a scheduler, which, for example, can be set to do something at 3
PM. I’d like to store that time in an object, and then take a Time,
chop off the date part, and see if the time matches.

Make sense?

Related question:

Is there a way to convert a Time (or DateTime) to Ruby’s Date class?

List R. wrote:

The problem with Time is that it tracks a date also.

Let me explain:

I have a scheduler, which, for example, can be set to do something at 3
PM. I’d like to store that time in an object, and then take a Time,
chop off the date part, and see if the time matches.

Don’t you mean chop off the time part:

#!/usr/bin/ruby -w

t = Time.new

h = (t.to_i % 86400)

s = h % 60
h /= 60
m = h % 60
h /= 60

puts “Time now: #{sprintf(”%02d:%02d:%02d GMT",h,m,s)}"

If you extract the number of seconds from a Time object as shown above,
you
have an integer, not a Time object, and its time is expressed in GMT.

List R. wrote:

Related question:

Is there a way to convert a Time (or DateTime) to Ruby’s Date class?

I added a few conversion methods like this to ‘ruby-units’ gem

class Time
def to_datetime
DateTime.civil(1970,1,1)+(self.to_f+self.gmt_offset)/86400
end

def to_date
Date.civil(1970,1,1)+(self.to_f+self.gmt_offset)/86400
end
end

This seems to work pretty well, although I haven’t tested it on a wide
range of OSs.

_Kevin

On Fri, 27 Oct 2006, _Kevin wrote:

DateTime.civil(1970,1,1)+(self.to_f+self.gmt_offset)/86400
end

def to_date
Date.civil(1970,1,1)+(self.to_f+self.gmt_offset)/86400
end
end

This seems to work pretty well, although I haven’t tested it on a wide
range of OSs.

it’s nice to have

Time.to_time
Time.to_date

Date.to_time
Date.to_date

DateTime.to_time
DateTime.to_date

so you can do

do_not_care_which_one.to_time
do_not_care_which_one.to_date

a la rails.

2 cts.

-a

On Fri, Oct 27, 2006 at 12:50:03PM +0900, [email protected] wrote:

class Time
range of OSs.
DateTime.to_date

so you can do

do_not_care_which_one.to_time
do_not_care_which_one.to_date

a la rails.

2 cts.

And that’s why we have facets!

% gem install facets
% cat date-time.rb
require 'date'
require 'rubygems'
require 'facets'
require 'date/to_time'
require 'date/to_date'
require 'time/to_date'
require 'time/to_time'

timers = {
    :date => Date.today,
    :time => Time.now,
    :date_time => DateTime.now
}

["to_date", "to_time"].each do |method|
    timers.each_pair do |name,time|
        result      = time.send(method)
        class_name  = result.class.name
        puts "#{name.to_s.rjust(10)}.#{method} yields result 

#{result} with class of #{class_name}"
end
end

puts
puts "superclass of DateTime : #{DateTime.superclass}"


% ruby date-time.rb
      date.to_date yields result 2006-10-27 with class of Date
      time.to_date yields result 2006-10-27 with class of Date
 date_time.to_date yields result 2006-10-27T00:18:27-0600 with class 

of DateTime
date.to_time yields result Fri Oct 27 00:00:00 -0600 2006 with
class of Time
time.to_time yields result Fri Oct 27 00:18:27 -0600 2006 with
class of Time
date_time.to_time yields result Fri Oct 27 00:00:00 -0600 2006 with
class of Time

superclass of DateTime : Date

When to_date is called on an instance of DateTime its still calling it
on Date object since DateTime.superclass == Date. So anywhere a Date is
used, a DateTime can be used.

enjoy,

-jeremy