PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > DateTime.new vs. DateTime.now
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
DateTime.new vs. DateTime.now

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 07h28   #1
Wes Gamble
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut DateTime.new vs. DateTime.now

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

>> x = DateTime.now

=> #<DateTime: 212070424110293/86400000,-1/4,2299161>
>> y = DateTime.new(x.year, x.month, x.day, x.hour, x.min, x.sec, x.sec_fraction

)
=> #<DateTime: 212070402509707/86400000,293/86400000,2299161>
>> x - y

=> Rational(10800293, 43200000)
>> (x - y).to_f

=> 0.250006782407407
>> (x - y).to_f * 1440

=> 360.009766666667
>> y - y

=> Rational(0, 1)

P. S. Another unusual thing is this:

>> (DateTime.now - DateTime.new).to_f

=> 2454519.30391494

What's that???
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 08h20   #2
Thomas Wieczorek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DateTime.new vs. DateTime.now

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...e.html#M002822

  Réponse avec citation
Vieux 22/02/2008, 16h22   #3
Wes Gamble
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DateTime.new vs. DateTime.now

Thomas,

Thanks for that - that s.

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

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

  Réponse avec citation
Vieux 22/02/2008, 17h43   #4
Justin Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DateTime.new vs. DateTime.now

Wes Gamble wrote:
<snip>
> 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

  Réponse avec citation
Vieux 23/02/2008, 15h43   #5
Brian Adkins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DateTime.new vs. DateTime.now

On Feb 22, 12:43 pm, Justin Collins <justincoll...@ucla.edu> wrote:
> Wes Gamble wrote:
>
> <snip>
>
> > 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


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
  Réponse avec citation
Vieux 23/02/2008, 19h48   #6
Wes Gamble
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: DateTime.new vs. DateTime.now

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

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14639 seconds with 14 queries