[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Time.parse bug?

tony summerfelt

3/3/2005 5:40:00 PM

i was porting some perl log parsing code over to ruby. final results
were weird. i've been scratching my head over a bug for the last hour
until i finally decided to create the minimal program that recreates
it.

a timestamp line logged yesterday:

02/03/05 22:30:49

that's march 2nd, 2005 est

but this:

require 'time'
t=Time.parse("02/03/05 22:30:49")
puts t

outputs this:

Thu Feb 03 22:30:49 EST 2005


is there any way to parse the line with a specific format (in this
case switching the day and month around.

i could split the line up, rearrange manually and use t.strftime but
that's closer to what my perl code was doing (and i was trying to
avoid)
http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org



8 Answers

djberg96

3/3/2005 10:06:00 PM

0

tony summerfelt wrote:
> i was porting some perl log parsing code over to ruby. final results
> were weird. i've been scratching my head over a bug for the last hour
> until i finally decided to create the minimal program that recreates
> it.
>
> a timestamp line logged yesterday:
>
> 02/03/05 22:30:49
>
> that's march 2nd, 2005 est
>
> but this:
>
> require 'time'
> t=Time.parse("02/03/05 22:30:49")
> puts t
>
> outputs this:
>
> Thu Feb 03 22:30:49 EST 2005
>
>
> is there any way to parse the line with a specific format (in this
> case switching the day and month around.

Internally, Time.parse is using ParseDate. From what I've read,
ParseDate uses your locale setting to determine the date. That means
you would have to reset your locale to get the value you want.

I agree that it would be nice if you could specify a locale as an
argument to Time.parse.

Regards,

Dan

Assaph Mehr

3/3/2005 10:25:00 PM

0


Hi Tony,

> is there any way to parse the line with a specific format (in this
> case switching the day and month around.

Use DateTime.strptime which is the inverse of strftime - it allows you
to parse according to a specified format. This gives a DateTime object
which you can probably use, or simply convert it to a Time object. In
your example:

c:\>irb
irb(main):001:0> require 'date'
=> false
irb(main):002:0> require 'time'
=> true
irb(main):003:0> s = '02/03/05 22:30:49'
=> "02/03/05 22:30:49"
irb(main):004:0> dt = DateTime.strptime(s, "%d/%m/%y %H:%M:%S")
=> #<DateTime: 211976562649/86400,0,2299161>
irb(main):005:0> dt.to_s
=> "2005-03-02T22:30:49Z"
irb(main):006:0> Time.parse dt.to_s
=> Wed Mar 02 22:30:49 UTC 2005

HTH,
Assaph

Yukihiro Matsumoto

3/3/2005 11:17:00 PM

0


In message "Re: Time.parse bug?"
on Fri, 4 Mar 2005 02:39:33 +0900, tony summerfelt <snowzone5@hotmail.com> writes:

|a timestamp line logged yesterday:
|
|02/03/05 22:30:49
|
|that's march 2nd, 2005 est
|
|but this:
|
|require 'time'
|t=Time.parse("02/03/05 22:30:49")
|puts t
|
|outputs this:
|
|Thu Feb 03 22:30:49 EST 2005
|
|
|is there any way to parse the line with a specific format (in this
|case switching the day and month around.

You need to specify the time format explicitly to parse. This small
chunk of code would help.

---
require 'date/format'

def Time.strptime(date, fmt)
Time.mktime(*Date._strptime(date, fmt).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :wday))
end
---

log = "02/03/05 22:30:49"
p Time.strptime(log, "%d/%m/%y %X")

Time.strptime will be available by default in the future.

matz.


tony summerfelt

3/3/2005 11:47:00 PM

0

On Fri, 4 Mar 2005 08:17:19 +0900, you wrote:

>You need to specify the time format explicitly to parse. This small
>chunk of code would help.

ah, perfect. actually that bit of code solves two problems for me.
thanks very much.

http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org



tony summerfelt

3/4/2005 1:34:00 PM

0

On Fri, 4 Mar 2005 08:17:19 +0900, you wrote:

>

>def Time.strptime(date, fmt)
> Time.mktime(*Date._strptime(date, fmt).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :wday))
>end

> log = "02/03/05 22:30:49"
> p Time.strptime(log, "%d/%m/%y %X")

i tried changing it to Time.strptime(log, %d/%m/%y %r")

and adding :merid to the values_at arguments but got: `strptime':
undefined method `values_at' for nil:NilClass (NoMethodError)

%r, %P and %p are defined in format.rb so i'm stumped as to what the
problem is...
http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org



Yukihiro Matsumoto

3/7/2005 2:36:00 AM

0

Hi,

In message "Re: Time.parse bug?"
on Fri, 4 Mar 2005 22:33:33 +0900, tony summerfelt <snowzone5@hotmail.com> writes:

|> p Time.strptime(log, "%d/%m/%y %X")
|
|i tried changing it to Time.strptime(log, %d/%m/%y %r")
|
|and adding :merid to the values_at arguments but got: `strptime':
|undefined method `values_at' for nil:NilClass (NoMethodError)
|
|%r, %P and %p are defined in format.rb so i'm stumped as to what the
|problem is...

That means invalid format error. %r only accepts hours 1..12, use %R
instead.

matz.


tony summerfelt

3/7/2005 5:04:00 PM

0

On Mon, 7 Mar 2005 11:35:52 +0900, you wrote:


>|> p Time.strptime(log, "%d/%m/%y %X")

>|i tried changing it to Time.strptime(log, %d/%m/%y %r")

>That means invalid format error. %r only accepts hours 1..12, use %R
>instead.

ok, i see now...once i have it parse properly then i can format it
with strftime...the following worked for me which was what i
organically wanted:

sample = "02/03/05 22:30:49"
t= Time.strptime(sample, "%d/%m/%y %R")
p t.strftime("%d %b %I:%M:%S %p")

thanks once again :)


http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org



tony summerfelt

3/8/2005 9:18:00 PM

0

On Mon, 7 Mar 2005 11:35:52 +0900, you wrote:

>That means invalid format error. %r only accepts hours 1..12, use %R
>instead.

my attempts to make it more generic have hit a brick wall

is there anyway to designate text/numbers/etc that's on either side
of the formatted date?

sample="some alphanumeric #ddg%^324&78* 08 Mar 15:17:18 more text"

t= Time.strptime(logline, "%d %b %R")
p t.strftime("%d %b %I:%M:%S %p")

of course gives me the value_at error again...
http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org