ParseDate and Time

Why don’t ParseDate and Time play nicely together?

I’d have thought ParseDate could output a Time object, or at least
Time could accept the array that ParseDate emits. Am I attempting
things in the wrong way, missing something important, or using
the wrong classes/modules?

Cheers,
Jason.

Why don’t ParseDate and Time play nicely together?

o/~ ParseDate, Date, Rosemary and Time . . . o/~

Ahem.

I’d have thought ParseDate could output a Time object, or at least
Time could accept the array that ParseDate emits.

It can.

irb(main):009:0> puts Time.gm(*parsedate("1993-01-01T00:00:00Z"))
Fri Jan 01 00:00:00 UTC 1993
=> nil

Or you can assume local time, instead:

irb(main):010:0> puts Time.local(*parsedate(“Wed, Aug 20, 2003 at 02:20:09PM”))
Wed Aug 20 14:20:09 EDT 2003
=> nil

The Ruby class design philosophy is not to use multiple constructors, but
rather to have class methods with names that tell you what they’re doing.
Always look at the class methods if you need some non-default way to make
an instance - there may be one there.

(Oh, Time.local has an alias called Time.mktime, but I prefer to explicitly
note the local timezone assumption).

-Mark

···

On Wed, Aug 20, 2003 at 02:20:09PM +0000, Jason Williams wrote:

I rarely use ParseDate directly and instead use Time.parse() from
time.rb (which is implemented using ParseDate).

Paul

···

On Wed, Aug 20, 2003 at 11:28:50PM +0900, Jason Williams wrote:

Why don’t ParseDate and Time play nicely together?

I’d have thought ParseDate could output a Time object, or at least
Time could accept the array that ParseDate emits. Am I attempting
things in the wrong way, missing something important, or using
the wrong classes/modules?

Ah, it seems to be that * I was missing; what does that do? (Sorry, I’m
new to Ruby :slight_smile:

···

In article 20030820142735.GA22454@mulan.thereeds.org, Mark J. Reed wrote:

irb(main):009:0> puts Time.gm(*parsedate(“1993-01-01T00:00:00Z”))
Fri Jan 01 00:00:00 UTC 1993
=> nil

Aha, that’s excellent, thanks. I was using “Programming Ruby” as my
reference and it doesn’t mention Time.parse.

···

In article 20030820163454.GG16089@atdesk.com, Paul Brannan wrote:

On Wed, Aug 20, 2003 at 11:28:50PM +0900, Jason Williams wrote:

Why don’t ParseDate and Time play nicely together?

I’d have thought ParseDate could output a Time object, or at least
Time could accept the array that ParseDate emits. Am I attempting
things in the wrong way, missing something important, or using
the wrong classes/modules?

I rarely use ParseDate directly and instead use Time.parse() from
time.rb (which is implemented using ParseDate).

In article 20030820142735.GA22454@mulan.thereeds.org, Mark J. Reed
wrote:

irb(main):009:0> puts Time.gm(*parsedate(“1993-01-01T00:00:00Z”))
Fri Jan 01 00:00:00 UTC 1993
=> nil

Ah, it seems to be that * I was missing; what does that do? (Sorry, I’m
new to Ruby :slight_smile:

The star is array expansion.

E.g., this

arr = [x,y,z]
foo(*arr)

is essentially the same as

foo(x,y,z)

and note that

foo(arr)

passes in a single arg which happens to be an array.

Star in a method definition similarly indicates a
variable number of parameters, treated as an array:

def foo(*args)
p args
end

foo(1,2,3) # Prints [1,2,3]
foo(4) # Prints [4]
foo # prints

Hope this helps…
Hal

···

----- Original Message -----
From: “Jason Williams” jason@jasonandali.org.uk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 20, 2003 12:09 PM
Subject: Re: ParseDate and Time

No, that’s a “new” feature.

Gavin

···

On Thursday, August 21, 2003, 3:30:02 AM, Jason wrote:

In article 20030820163454.GG16089@atdesk.com, Paul Brannan wrote:

On Wed, Aug 20, 2003 at 11:28:50PM +0900, Jason Williams wrote:

Why don’t ParseDate and Time play nicely together?

I’d have thought ParseDate could output a Time object, or at least
Time could accept the array that ParseDate emits. Am I attempting
things in the wrong way, missing something important, or using
the wrong classes/modules?

I rarely use ParseDate directly and instead use Time.parse() from
time.rb (which is implemented using ParseDate).

Aha, that’s excellent, thanks. I was using “Programming Ruby” as my
reference and it doesn’t mention Time.parse.

It flattens an array.

In this code:

a = [1,2,3]
foo(a)

the method foo is passed a single argument which happens to be an array.
In this code:

a = [1,2,3]
foo(*a)

the method foo is passed three arguments, each of which is a Fixnum.

-Mark

···

On Wed, Aug 20, 2003 at 05:00:06PM +0000, Jason Williams wrote:

In article 20030820142735.GA22454@mulan.thereeds.org, Mark J. Reed wrote:

irb(main):009:0> puts Time.gm(*parsedate("1993-01-01T00:00:00Z"))
Fri Jan 01 00:00:00 UTC 1993
=> nil

Ah, it seems to be that * I was missing; what does that do? (Sorry, I’m
new to Ruby :slight_smile:

Hi –

irb(main):009:0> puts Time.gm(*parsedate("1993-01-01T00:00:00Z"))
Fri Jan 01 00:00:00 UTC 1993
=> nil

Ah, it seems to be that * I was missing; what does that do? (Sorry, I’m
new to Ruby :slight_smile:

It flattens an array.

I’d quibble not with the spirit but with the wording of this – only
because of the existence of the Array#flatten method, which does
something different:

irb(main):010:0> a
[1, [2, 3, [4, 5], 6, 7], 8]
irb(main):011:0> def m(*args); args; end
nil
irb(main):012:0> m(a)
[[1, [2, 3, [4, 5], 6, 7], 8]]
irb(main):013:0> m(*a)
[1, [2, 3, [4, 5], 6, 7], 8]
irb(main):014:0> m(a.flatten)
[[1, 2, 3, 4, 5, 6, 7, 8]]
irb(main):015:0> m(*a.flatten)
[1, 2, 3, 4, 5, 6, 7, 8]

David

···

On Thu, 21 Aug 2003, Mark J. Reed wrote:

On Wed, Aug 20, 2003 at 05:00:06PM +0000, Jason Williams wrote:

In article 20030820142735.GA22454@mulan.thereeds.org, Mark J. Reed wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav