time comparison

T

tony summerfelt

i want to parse and trim a log file. the date format log file looks like:
26 Jan 23:19:26 2004

to get the time in a format i can use i did

t1=Time.parse("26 Jan 23:19:26 2004") #string expanded for example
t2=Time.now

i'm want to compare the current date/time with the parsed one from the log.
if the line is older than x number of days it will be ignored.

unfortunately i'm still relatively new a ruby and the 'principle of
least suprise' hit me about a dozen times...with suprises :/
i'm used to perl's magic. so i'm here for guideance...
 
G

Guillaume Marcais

t3 = t2 - 24 * 2600 * x # - for Time work on the seconds
if t1 < t3
# Do something about it
# It is more than x days old
end


Hope this helps,
Guillaume.
 
G

Guillaume Marcais

With 3600 seconds in an hour, it even works better!

Sorry,
Guillaume.

t3 = t2 - 24 * 2600 * x # - for Time work on the seconds
if t1 < t3
# Do something about it
# It is more than x days old
end


Hope this helps,
Guillaume.
 
G

Guillaume Marcais

Not in funkadecimal. :)

Ha, he must have been reading _2600_ recently.

Hal

If accuracy really matters to you and funkadecimal doesn't fancy you,
you can always do:

t3 = t2 - x * (Time.mktime(1960, 1, 2) - Time.mktime(1960, 1, 1))

:)

Guillaume.
 
T

tony summerfelt

t3 = t2 - 24 * 2600 * x # - for Time work on the seconds

i had something like that originally but i was getting something about not
being able to convert floats. i was trying for something object oriented...

sometimes you just have to step back from the oo thing and let common sense
trickle in...

i'm almost embarrased i asked the question now...

all the replies were appreciated...
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
tony summerfelt said:
i had something like that originally but i was getting something about not
being able to convert floats. i was trying for something object oriented...

sometimes you just have to step back from the oo thing and let common sense
trickle in...

How about the following?

require "date"

t1 = DateTime.parse("26 Jan 23:19:26 2004").next
t2 = DateTime.now

p t2 > t1

regards.
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top