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
You need to specify the time format explicitly to parse. This small
chunk of code would help.
···
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.
---
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.
ah, perfect. actually that bit of code solves two problems for me.
thanks very much.
http://home.cogeco.ca/~tsummerfelt1
telnet://ventedspleen.dyndns.org
···
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.
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
···
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")
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
···
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.
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
···
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.