Time without Date

L

List Recv

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?
 
L

List Recv

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?
 
L

List Recv

Related question:

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

List Recv

a = Time.parse('Wed Oct 25 13:10:00 2006')
b = Time.parse('Thu Oct 26 13:10:00 2006')
c = Time.parse('Fri Oct 27 13:05:00 2006')

at = TimeOnly.from_time(a)
bt = TimeOnly.from_time(b)
ct = TimeOnly.from_time(c)

assert_equal at, bt
assert ct < bt
 
K

_Kevin

List said:
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
 
A

ara.t.howard

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.

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
 
J

Jeremy Hinegardner

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,216
Messages
2,571,115
Members
47,720
Latest member
mohdkaif002

Latest Threads

Top