Professional Web Applications Themes

time comparison - Ruby

i want to p 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.p("26 Jan 23:19:26 2004") #string expanded for example t2=Time.now i'm want to compare the current date/time with the pd 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... ...

  1. #1

    Default time comparison

    i want to p 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.p("26 Jan 23:19:26 2004") #string expanded for example
    t2=Time.now

    i'm want to compare the current date/time with the pd 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...


    --
    http://home.cogeco.ca/~tsummerfelt1
    tony Guest

  2. #2

    Default Re: time comparison

    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.


    On Thu, 2004-01-29 at 13:44, tony summerfelt wrote: 




    Guillaume Guest

  3. #3

    Default Re: time comparison

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

    Sorry,
    Guillaume.

    On Thu, 2004-01-29 at 13:51, Guillaume Marcais wrote: 
    >
    >
    >[/ref]




    Guillaume Guest

  4. #4

    Default Re: time comparison


    On Thu, Jan 29, 2004 at 06:35:54PM +0000, tony summerfelt wrote: 

    You can do math on t1 and t2 as if they were numbers. The result is in
    seconds.

    secondsAgo = t2 - t1
    daysAgo = secondsAgo /
    60 / # seconds per minute
    60 / # minutes per hour
    24 # hours per day

    or, just use the result directly:

    daysAgo = secondsAgo / 86_400 # seconds in a day

    -Mark

    Mark Guest

  5. #5

    Default Re: time comparison

    On Fri, Jan 30, 2004 at 03:51:31AM +0900, Guillaume Marcais wrote: 

    3600. There are 3,600 seconds in an hour, not 2600. :)


    -Mark
    Mark Guest

  6. #6

    Default Re: time comparison

    Mark J. Reed wrote: 
    >
    >
    > 3600. There are 3,600 seconds in an hour, not 2600. :)[/ref]

    Not in funkadecimal. :)

    Ha, he must have been reading _2600_ recently.

    Hal



    Hal Guest

  7. #7

    Default Re: time comparison

    On Thu, 2004-01-29 at 15:14, Hal Fulton wrote: 
    > >
    > >
    > > 3600. There are 3,600 seconds in an hour, not 2600. :)[/ref]
    >
    > Not in funkadecimal. :)
    >
    > Ha, he must have been reading _2600_ recently.
    >
    > Hal[/ref]

    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.



    Guillaume Guest

  8. #8

    Default Re: time comparison

    On Thu, 29 Jan 2004 at 18:51 GMT, Guillaume Marcais <fr> wrote:
     

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

    --
    http://home.cogeco.ca/~tsummerfelt1
    tony Guest

  9. #9

    Default Re: time comparison

    In Message-Id: <TigSb.551$cgocable.net>
    tony summerfelt <com> writes:
     

    How about the following?

    require "date"

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

    p t2 > t1

    regards.


    --
    to January 30, 2004
    No gains without pains.



    YANAGAWA Guest

Similar Threads

  1. Comparison with Custom CMS
    By JacquieMC in forum Macromedia Contribute General Discussion
    Replies: 2
    Last Post: November 25th, 10:21 PM
  2. RAW vs JPEG comparison for D70
    By paul in forum Photography
    Replies: 20
    Last Post: January 19th, 02:01 AM
  3. Formatting a time field to 24 hour time (Military time) in the Datagrid
    By David Hearn in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: September 10th, 02:50 PM
  4. Mac - PC Comparison
    By Ed A. Ortiz in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 6
    Last Post: July 16th, 07:49 PM
  5. raw vs. ufs comparison
    By Jan Andres in forum Sun Solaris
    Replies: 5
    Last Post: July 16th, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139