Time.parse bug?

  • Thread starter tony summerfelt
  • Start date
T

tony summerfelt

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/~tsummerfelt1
telnet://ventedspleen.dyndns.org
 
D

Daniel Berger

tony said:
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
 
A

Assaph Mehr

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
 
Y

Yukihiro Matsumoto

In message "Re: Time.parse bug?"

|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.
 
T

tony summerfelt

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/~tsummerfelt1
telnet://ventedspleen.dyndns.org
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Time.parse bug?"

|> 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.
 
T

tony summerfelt

|> 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/~tsummerfelt1
telnet://ventedspleen.dyndns.org
 
T

tony summerfelt

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/~tsummerfelt1
telnet://ventedspleen.dyndns.org
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top