Parsing log with date time entry

hmm interesting

3 of the logs have the month as 1 instead of 01 and it does not get seen
as in range.

What are you doing here time = "#{$2}/#{$1}#{$3}" ?

In order to catch these logs I'm thinking I should drop the date
completely from this part of the scan because the logs are generated
daily do I can just search the date.log for the proper time.

···

--
Posted via http://www.ruby-forum.com/.

Thank you. For Time class, I could get it to work.

This will simplify the code for the OP.

$ irb
1.9.3-p0 :001 > t2 = Time.now
=> 2012-01-17 19:47:19 0100

1.9.3-p0 :002 > t1 = t2 - 900
=> 2012-01-17 19:32:19 0100

1.9.3-p0 :003 > time_range = t1..t2
=> 2012-01-17 19:32:19 0100..2012-01-17 19:47:19 0100

1.9.3-p0 :004 > t3 = Time.now - 300
=> 2012-01-17 19:42:41 0100

1.9.3-p0 :006 > time_range === t3
TypeError: can't iterate from Time
    from (irb):6:in `each'
    from (irb):6:in `include?'
    from (irb):6:in `include?'
    from (irb):6:in `==='
    from (irb):6
    from /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'

1.9.3-p0 :007 > time_range.cover? t3
=> true # :slight_smile:

1.9.3-p0 :008 > time_range.cover? Time.now
=> false # :slight_smile:

However, for Float the === operator seems to work, _without_ Float#succ

1.9.3-p0 :009 > f1 = 13.6
=> 13.6
1.9.3-p0 :010 > f2 = 18.6
=> 18.6
1.9.3-p0 :011 > float_range = f1..f2
=> 13.6..18.6
1.9.3-p0 :012 > float_range === 15.5
=> true
1.9.3-p0 :013 > f1.succ
NoMethodError: undefined method `succ' for 13.6:Float
    from (irb):13
    from /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
1.9.3-p0 :014 > float_range.cover? 15.5
=> true

So, maybe the behavior could be made similar?

I suggest that `Range#=== arg` falls back to `Range#cover? arg`
uniformily if `arg.respond_to?(:succ)` is false (and probably other
conditions).

Thanks again,

Peter

···

On Tue, Jan 17, 2012 at 7:44 PM, Kenichi Kamiya <kachick1@gmail.com> wrote:

Range#=== , need #succ method in Ruby1.9.3
under only #<=>, use Range#cover?

hmm interesting

3 of the logs have the month as 1 instead of 01 and it does not get seen
as in range.

The regexp that I proposed now:

  /^(\d{2}\/\d{2})\/(\d{4})(\s\d{1,2}\:\d{2}\:\d{2}\:\d{1,3})/

will indeed only work on a month like '01' , '12', ...
That is because the {2} required _exactly_ 2 digits.

If you also want to match '1', '5' etc. you may need to write

/^(\d{1,2}\/\d{2})\/(\d{4})(\s\d{1,2}\:\d{2}\:\d{2}\:\d{1,3})/

But then ... for a _string_ comparison, you will need to
expand it always to a 2 character result (otherwise
the month '5' would be "higher" than the month '10';
but '05' is lower than '10').

What are you doing here time = "#{$2}/#{$1}#{$3}" ?

There are 3 groups in the regex (between the brackets) and
I am reordering them to generate the YYYY/mm/dd HH:MM:SS:MMM
format.

I think UNTESTED !! the new regexp then becomes:

/^(\d{1,2})\/\(\d{1,2})\/(\d{4})(\s\d{1,2}\:\d{2}\:\d{2}\:\d{1,3})/
  ^ GRP 1 ^ ^ GRP 2 ^ ^ GRP3^^ GROUP 4 ^

and

time = "#{$3}/#{$1.rjust(2,'0')}/#{$2.rjust(2,'0')}#{$4}"

The GRP comments are to show you the extent of the 4 Regex groups, that
you find back then as $1, $2, $3, $4.

In order to catch these logs I'm thinking I should drop the date
completely from this part of the scan because the logs are generated
daily do I can just search the date.log for the proper time.

That 's also a possibility :slight_smile:

Pete

···

On Mon, Jan 23, 2012 at 6:26 PM, Christopher Graves <gravescl@gmail.com>wrote:

Sorry, I have no skills for C programming.
But, I guess the causes from below codes by only my intuition.
# https://github.com/ruby/ruby/blob/trunk/range.c

# -----------------------------------------------------------------------------
static int
discrete_object_p(VALUE obj)
{
    if (rb_obj_is_kind_of(obj, rb_cTime)) return FALSE; /* until
Time#succ removed */
    return rb_respond_to(obj, id_succ);
}
# -----------------------------------------------------------------------------
static VALUE
range_include(VALUE range, VALUE val)
{
    VALUE beg = RANGE_BEG(range);
    VALUE end = RANGE_END(range);
    int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
rb_obj_is_kind_of(beg, rb_cNumeric) ||
rb_obj_is_kind_of(end, rb_cNumeric);
# -----------------------------------------------------------------------------

It looks specific rules for Time and Numeric.

···

2012/1/18 Peter Vandenabeele <peter@vandenabeele.com>:

On Tue, Jan 17, 2012 at 7:44 PM, Kenichi Kamiya <kachick1@gmail.com> wrote:

Range#=== , need #succ method in Ruby1.9.3
under only #<=>, use Range#cover?

Thank you. For Time class, I could get it to work.

This will simplify the code for the OP.

$ irb
1.9.3-p0 :001 > t2 = Time.now
=> 2012-01-17 19:47:19 0100

1.9.3-p0 :002 > t1 = t2 - 900
=> 2012-01-17 19:32:19 0100

1.9.3-p0 :003 > time_range = t1..t2
=> 2012-01-17 19:32:19 0100..2012-01-17 19:47:19 0100

1.9.3-p0 :004 > t3 = Time.now - 300
=> 2012-01-17 19:42:41 0100

1.9.3-p0 :006 > time_range === t3
TypeError: can't iterate from Time
from (irb):6:in `each'
from (irb):6:in `include?'
from (irb):6:in `include?'
from (irb):6:in `==='
from (irb):6
from /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'

1.9.3-p0 :007 > time_range.cover? t3
=> true # :slight_smile:

1.9.3-p0 :008 > time_range.cover? Time.now
=> false # :slight_smile:

However, for Float the === operator seems to work, _without_ Float#succ

1.9.3-p0 :009 > f1 = 13.6
=> 13.6
1.9.3-p0 :010 > f2 = 18.6
=> 18.6
1.9.3-p0 :011 > float_range = f1..f2
=> 13.6..18.6
1.9.3-p0 :012 > float_range === 15.5
=> true
1.9.3-p0 :013 > f1.succ
NoMethodError: undefined method `succ' for 13.6:Float
from (irb):13
from /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
1.9.3-p0 :014 > float_range.cover? 15.5
=> true

So, maybe the behavior could be made similar?

I suggest that `Range#=== arg` falls back to `Range#cover? arg`
uniformily if `arg.respond_to?(:succ)` is false (and probably other
conditions).

Thanks again,

Peter