DateTime.new vs. DateTime.now

W

Wes Gamble

This may be a well known thing but it surprised me.

Apparently, the time zone of DateTime.now is GMT, but the time zone of
DateTime.new(y, m, d, h, m, s) is local.

Below, I store DateTime.now in x. Then I construct a new DateTime
object using the various components of x. You might expect that x - y
would be 0 or very close to it, but in fact it is very close to the
offset of my time zone (US Central) from GMT, which is .25 day (or 360
min if you multiply .25 day * (1440 min/day)). This implies that
DateTime.now is in GMT and DateTime.new(y, m, d, h, m, s) is in local
time.

Can anyone point me to definitive documentation about this behavior?

Thanks,
Wes
)
=> Rational(0, 1)

P. S. Another unusual thing is this:
=> 2454519.30391494

What's that???
 
T

Thomas Wieczorek

You forgot to set the offset parameter which is defaulted to 0. It is
a fraction of a day and represents the time added to a timezone.

irb(main):020:0> x = DateTime.now
=> #<DateTime: 424140856819/172800,1/24,2299161>
irb(main):021:0> y = DateTime.new(x.year, x.month, x.day, x.hour, x.min, x.sec,
x.sec_fraction, x.offset)
=> #<DateTime: 424140864017/172800,1/172800,1/24>
irb(main):022:0> (x-y).to_f
=> -0.0416550925925926

The difference is probably just a float point precision error.

You can find more to new(which is aliased as civil) at
http://www.ruby-doc.org/core/classes/DateTime.html#M002822
 
W

Wes Gamble

Thomas,

Thanks for that - that helps.

I guess I should have explained the reason I even bothered to look into
this so deeply.

I really expected that DateTime.now would return the current, local
time. To me, this is against the grain of Ruby "doing what you would
expect."

I needed to compare the current time with a time stored in local time.
In order to do that, I have to capture DateTime.now, and then construct
a new DateTime using DateTime.new with its components in order to get a
local time that I can then use to calculate the difference between my
stored time and the current time.

I've gotten past my problem, but it seems to me that DateTime.now should
represent local time, and if I want gmt, then I can ask for something
like DateTime.gmt.

If there is a simpler way to ask for a DateTime object that represents
the current, local time, I was unable to find it.

Wes
 
J

Justin Collins

Wes Gamble wrote:
If there is a simpler way to ask for a DateTime object that represents
the current, local time, I was unable to find it.

Wes

That is precisely what I get with DateTime.now. Maybe your machine is
set to GMT?

irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = DateTime.now
=> #<DateTime: 5890846158565549/2400000000,-1/3,2299161>
irb(main):003:0> d.strftime
=> "2008-02-22T09:35:08-08:00"
irb(main):004:0> d.hour
=> 9
irb(main):005:0> d.min
=> 35
irb(main):006:0> d.sec
=> 8
irb(main):007:0> d.zone
=> "-08:00"


-Justin
 
B

Brian Adkins

Wes Gamble wrote:




That is precisely what I get with DateTime.now. Maybe your machine is
set to GMT?

irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = DateTime.now
=> #<DateTime: 5890846158565549/2400000000,-1/3,2299161>
irb(main):003:0> d.strftime
=> "2008-02-22T09:35:08-08:00"
irb(main):004:0> d.hour
=> 9
irb(main):005:0> d.min
=> 35
irb(main):006:0> d.sec
=> 8
irb(main):007:0> d.zone
=> "-08:00"

-Justin

Yep, local time here also:
irb(main):001:0> require 'date'
=> true
irb(main):002:0> puts DateTime.now
2008-02-23T10:41:51-05:00
 
W

Wes Gamble

My findings are in agreement with yours.

If I call puts or strftime on DateTime.now, I will get local time.
If I call time component methods (hour, min, sec, etc.), I will get
local time.

But if I just take a DateTime.now and do math with it, it is in GMT.

I would assert that the strftime, puts, and all of the time component
methods do an implicit GMT-to-local time conversion as part of their
processing. That is the only thing that would explain the behavior that
I am seeing.

Thanks,
Wes
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top