[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Time, Date and DateTime
From:       Siep Korteling <s.korteling () gmail ! com>
Date:       2010-02-28 23:47:20
Message-ID: 5ea2451092923c47d4303c9f79bc76ae () ruby-forum ! com
[Download RAW message or body]

Rajinder Yadav wrote:
> Hi,
> 
> I am trying to understand the key differences between Time, Date and
> DateTime classes.
> 
> When should one use one over the other? Does anyone know of a good post
> that explains each of these classes and post on how to do basic time,
> date arithmetic or conversion between each?

Note: I am by no means an expert on this.

Time provides you with a date and a time and is fine if you are sure the 
date ranges from the year 1970 to 2037.
It's excellent for timestamps and such. Essentially, it's just the 
number of seconds elapsed since 1970-1-1. So if the provided methods 
don't suit you, you have to calculate in seconds, using #to_i and #at .

Date is a heavyweight, allowing for Julian or Gregorian calendars, 
specifying the reform dates. You don't have to worry about date ranges 
(unless you need dates like -5000-1-1). Calculating is superior to Time 
objects.

DateTime is the same as a Date with hours, minutes and seconds.

All three can be enhanced by require-ing active_support.

The creation of a Date object is about ten times slower than the 
creation of a time object (on my PC).

In Ruby 1.9 conversion is simple: #to_date, #to_time and #to_datetime. 
For versions earlier: google, or copy the 1.9 methods from the 
documentation (untested).

hth,

Siep

-- 
Posted via http://www.ruby-forum.com/.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic