[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Date Class and GMT

Zach Dennis

11/26/2003 4:31:00 PM


I:\>irb
irb(main):001:0> Time.now
=> Wed Nov 26 11:29:24 Eastern Standard Time 2003
irb(main):002:0> Time.now.gmtime
=> Wed Nov 26 16:29:29 UTC 2003
irb(main):003:0>



I need to be able to return the zone by it's numeric representation(
i.e. -0500 ). Like

Wed Nov 26 16:29:29 UTC 2003 -0500

by chance.

I cannot find a function to do this. Is it built in or can i request an
addition?

Thanks,

Zach



2 Answers

Mark J. Reed

11/26/2003 4:40:00 PM

0

On Thu, Nov 27, 2003 at 01:31:08AM +0900, Zach Dennis wrote:
> I need to be able to return the zone by it's numeric representation(
> i.e. -0500 ). Like
>
> Wed Nov 26 16:29:29 UTC 2003 -0500

(You should have deleted the "UTC" from that, since it refers to the
time zone; it's the modern name for GMT.)

You want the DateTime class, included in the 'date' library which comes
with Ruby:

irb(main):001:0> require 'date'
=> true
irb(main):002:0> n=DateTime.now
=> #<DateTime: 105968312236111499/43200000000,-5/24,2299161>
irb(main):003:0> n.to_s
=> "2003-11-26T11:34:32-0500"
irb(main):004:0> n.zone
=> "-0500"
irb(main):005:0> n.strftime("%a %b %d %H:%M:%S %Y %Z")
=> "Wed Nov 26 11:34:32 2003 -0500"

However, as I have posted here before, there is a bug in this
library which for some reason automatically increases the time zone
offset from UTC by one minute, so if you run the above examples on your
system you will probably see -0501 instead of -0500. This problem is
easily fixed by removing the line

d += d / d.abs if d.nonzero?

in the definition of DateTime.now (line 511 of date.rb).

-Mark

Mark J. Reed

11/26/2003 4:43:00 PM

0

On Wed, Nov 26, 2003 at 04:39:45PM +0000, Mark J. Reed wrote:
> On Thu, Nov 27, 2003 at 01:31:08AM +0900, Zach Dennis wrote:
> > I need to be able to return the zone by it's numeric representation(
> > i.e. -0500 ). Like

Oh, it turns out that instead of using DateTime, you can just use the %z
specifier to strftime.

irb(main):001:0> n=Time.now
=> Wed Nov 26 11:41:51 EST 2003
irb(main):002:0> n.zone
=> "EST"
irb(main):003:0> n.strftime("%Z")
=> "EST"
irb(main):004:0> n.strftime("%z")
=> "-0500"
irb(main):005:0> n.strftime("%a %b %d %H:%M:%S %z %Y")
=> "Wed Nov 26 11:41:51 -0500 2003"

-Mark