Joining a Date and a Time together for migrations and miniformats

Hi everyone, I have a problem born out of a design decision back when
this app was PHP based. Basically it’s an Upcoming events module and
the users can add upcoming event to their website. Long and short: the
date is stored in a seperate field from the time in the database. The
field types are date and time, thus resulting in a Date object and a
Time object, here’s a paste of a sample object instance:

=> #<UpcomingEvent id: 1, title: “Party at Brendons”, happening_on:
“2008-04-18”, happening_at: “2000-01-01 09:00:00”, short_description:
“Wanna know about it?”, long_description: “It’s all here”, price:
“$55”, active: true, location: “Hamiltron”, position: 1,
upcoming_event_set_id: 1>

So essentiall it’s at 9am on the 18th of April 2008.

Now here’s the fun part, now that the data has been migrated from the
old app, and lots of other non-legacy clients will be using this
module also, I’d like to tidy it all up with a migration. I also
require the date and time to be in one field for making a hEvent
(microformat) using the microformat plugin.

The question, how can I join those two fields together to form one
cohesive DateTime or even Time object?

I would use your answer probably to migrate the database to a single
datetime field which would mean no calculation work to get the
microformat date to work every time the page loads.

Your help on this seemingly simple problem would be great, I’ve
stuffed around for hours and hope someone with more experience can
show me the light :slight_smile:

Cheers,

Brendon

On 17 Apr 2008, at 11:36, Brendon wrote:

“Wanna know about it?”, long_description: “It’s all here”, price:
“$55”, active: true, location: “Hamiltron”, position: 1,
upcoming_event_set_id: 1>

So essentiall it’s at 9am on the 18th of April 2008.

Convert the date to a time and then use time.change?

date = Time.mktime(2008,1,1)
=> Tue Jan 01 00:00:00 UTC 2008

time = Time.mktime(2000,1,1,15,30)
=> Sat Jan 01 15:30:00 UTC 2000

date.change(:hour => time.hour, :min => time.min)
=> Tue Jan 01 15:30:00 UTC 2008

Fred

Thanks for that, this is what I came up with in the end for my model:

def happening_datetime
happening_on.to_time.change(:hour => happening_at.hour, :min =>
happening_at.min)
end

Yahoo!

On Apr 17, 10:57 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs