Professional Web Applications Themes

newbie question on how to subtract two dates. - Ruby

--0-2040115834-1072473489=:21116 Content-Type: text/plain; cht=us-ascii I have a file with a file name of the form filename.yymmdd, where yy=two digits year, mm=two digits month and dd=two digits day. For example, perf.030906 indicates 03=year 2003, 09=September and 06=the sixth day of the month (September 06, 2003). This file contains performance data collected during the day and each system contains one file per day. We have few dozen systems collecting this data. Every days, around 1:00 AM I have to take the file for the previous day and copy it to a central server where the new file will have the name: hostname.perf.yymmdd. ...

  1. #1

    Default newbie question on how to subtract two dates.

    --0-2040115834-1072473489=:21116
    Content-Type: text/plain; cht=us-ascii


    I have a file with a file name of the form filename.yymmdd, where yy=two digits year, mm=two digits month and dd=two digits day. For example, perf.030906 indicates 03=year 2003, 09=September and 06=the sixth day of the month (September 06, 2003).

    This file contains performance data collected during the day and each system contains one file per day. We have few dozen systems collecting this data.

    Every days, around 1:00 AM I have to take the file for the previous day and copy it to a central server where the new file will have the name: hostname.perf.yymmdd. The question I have is how do I subtract two dates. For example, if:

    today=010104

    yesterday=123103

    Is there a ruby method that will allow me to perform something like: today - x and get yesterday?

    Thank you


    ---------------------------------
    Do you Yahoo!?
    Protect your identity with Yahoo! Mail AddressGuard
    --0-2040115834-1072473489=:21116--


    Ruby Guest

  2. #2

    Default Re: newbie question on how to subtract two dates.

    On Sat, 27 Dec 2003 06:18:33 +0900, Ruby Ruby wrote:
     
    Check out the Date class.
    http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/files/date_rb.html
    Tim Guest

  3. #3

    Default Re: newbie question on how to subtract two dates.

    Tim Hunter wrote:
     
    >Check out the Date class.
    >http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/files/date_rb.html
    >
    >.
    >
    >
    >[/ref]

    You just need to subtract (or add) the number of seconds you want from a
    Date to get another date: in other words, to find the date 24 hours
    prior to the current date:

    yesterday = Time.now - 24 * 60 * 60

    (Incidentally, I looked at the Date class doentation... I already
    knew how to do Date arithmetic in Ruby and knew what I was looking for,
    and it still took me a while to find where in the doentation it talks
    about how to subtract some value x from a date to get another date.
    Since this is a fairly common operation on dates, could this be given as
    an example somewhere near the front of the docs? I know I would never
    have found it there if I didn't already know what it was I wanted to do,
    and how to do it.)

    --
    Jamis Buck
    byu.edu

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



    Jamis Guest

  4. #4

    Default Re: newbie question on how to subtract two dates.

    On Saturday, December 27, 2003, 9:16:38 AM, Jamis wrote:
     
     


    Thanks for the tip. I'll update the docs soon.

    Cheers,
    Gavin



    Gavin Guest

  5. #5

    Default Re: newbie question on how to subtract two dates.

    Ruby Ruby wrote: 
    You could use something like this:

    require 'date'
    puts today=Date.today #=>2003-12-27
    puts tomorrow=Date.new(2003,12,28) #=>2003-12-28
    puts today-1 #=>2003-12-26
    puts today+30 #=>2004-01-26

    cheers
    Adartse


    Osuka Guest

Similar Threads

  1. How to subtract in a calculation in Acrobat Pro 9
    By sander_wessel@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 9
    Last Post: March 26th, 09:07 AM
  2. subtract 3d objects
    By braddini in forum Macromedia Director 3D
    Replies: 2
    Last Post: May 29th, 09:24 PM
  3. Unable to subtract pixels from a selection
    By vnative in forum Macromedia Fireworks
    Replies: 2
    Last Post: November 21st, 06:31 PM
  4. An expression question on dates
    By Microsoft in forum Microsoft Access
    Replies: 6
    Last Post: July 6th, 02:09 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