Time: safe way to go to next day?

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Time: safe way to go to next day?

    Hello,

    When i have a Time object, to get to next day i do: myTime += (60*60*24)
    Not very elegant, but it works and I didn't find a nicer way.
    well, i thought it worked...

    now we are going to change the time in europe...
    So it breaks my assumption...

    any proper way to fix it?

    emmanuel

    irb(main):008:0> now = Time.now
    Tue Oct 21 08:14:44 Central Europe Daylight Time 2003
    irb(main):009:0> todayMidnight = Time.local(now.year, now.month,
    now.day, 0,0,0)
    Tue Oct 21 00:00:00 Central Europe Daylight Time 2003 # today at 0:00

    irb(main):010:0> todayMidnight + (60*60*24)
    Wed Oct 22 00:00:00 Central Europe Daylight Time 2003 # tomorrow at 0:00

    [..]

    irb(main):014:0> todayMidnight + (60*60*24)*5
    Sun Oct 26 00:00:00 Central Europe Daylight Time 2003 # sun at 0:00
    irb(main):015:0> todayMidnight + (60*60*24)*6
    Sun Oct 26 23:00:00 Central Europe Standard Time 2003 # <----------
    !!!! sun at 23:00



    Emmanuel Touzery Guest

  2. Similar Questions and Discussions

    1. CFMX7.0.1 Administrator date time issue showing 13hrsbehind server time
      I am running a W2k SP4 box that has been upgraded from CFMX6 to CFMX7.0.1. The CFMX7.0.1 server is showing the date on the Server Settings >...
    2. SAFE MODE
      Hi Bill, Hit F8 at boot to access the boot options. -- Best of Luck, Rick Rogers aka "Nutcase" MS-MVP - Win9x...
    3. $SAFE = 5 and Safe Ruby Misleading?
      Hey folks. With all this talk of duck typing and such, I got to thinking about some of my code that I *thought* executed untrusted code...
    4. Can't log in to Safe Mode
      Thanks, I'll give that a try! Michael "Rick "Nutcase" Rogers" <rick@mvps.org> wrote in message news:%23RJyfWjPDHA.1584@TK2MSFTNGP11.phx.gbl......
  3. #2

    Default Re: Time: safe way to go to next day?

    Hi,

    At Tue, 21 Oct 2003 15:16:44 +0900,
    Emmanuel Touzery wrote:
    > When i have a Time object, to get to next day i do: myTime += (60*60*24)
    > Not very elegant, but it works and I didn't find a nicer way.
    > well, i thought it worked...
    >
    > now we are going to change the time in europe...
    > So it breaks my assumption...
    If you just want dates, what about date.rb?

    require 'date'
    today = Date.today
    today.to_s # => "2003-10-21"
    (today+5).to_s # => "2003-10-26"

    I've not tested it with DST though.

    --
    Nobu Nakada

    nobu.nokada@softhome.net Guest

  4. #3

    Default Re: Time: safe way to go to next day?

    Hello,

    [email]nobu.nokada@softhome.net[/email] wrote:
    >If you just want dates, what about date.rb?
    >
    > require 'date'
    > today = Date.today
    > today.to_s # => "2003-10-21"
    > (today+5).to_s # => "2003-10-26"
    >
    >I've not tested it with DST though.
    >
    >
    >
    but where is all this stuff documented?
    find.rb, base64.rb, date.rb, the other day someone wanted to remove
    recursively a dir, and was given another one of those magic files...
    which doc did i miss? :O(

    otherwise, yes it's probably what i want. unfortunately i now sit on a
    lot of code to convert :O(
    if someone has a way with Time, it would simplify things for me...

    thanks,

    emmanuel



    Emmanuel Touzery Guest

  5. #4

    Default Re: Time: safe way to go to next day?

    Quoting Emmanuel Touzery <emmanuel.touzery@wanadoo.fr>:
    > otherwise, yes it's probably what i want. unfortunately i now sit on a
    > lot of code to convert :O(
    > if someone has a way with Time, it would simplify things for me...
    If you need just the date part you may probably ignore timezone as well. Use
    UTC:
    irb(main):027:0> ary = Time.now
    => Tue Oct 21 09:45:14 Central Europe Daylight Time 2003
    irb(main):028:0> t = Time.gm(*ary)
    => Tue Oct 21 09:45:14 UTC 2003
    irb(main):029:0> t += 7 * 86400
    => Tue Oct 28 09:45:14 UTC 2003
    irb(main):030:0>

    Regards,

    Dalibor Sramek

    --
    Dalibor Sramek [url]http://www.insula.cz/dali[/url] | In the eyes of cats,
    [email]dalibor.sramek@insula.cz[/email] | all things belong to cats.


    Dalibor Sramek Guest

  6. #5

    Default Re: Time: safe way to go to next day?

    Quoting Emmanuel Touzery <emmanuel.touzery@wanadoo.fr>:
    > but where is all this stuff documented?
    > find.rb, base64.rb, date.rb, the other day someone wanted to remove
    > recursively a dir, and was given another one of those magic files...
    > which doc did i miss? :O(
    Ruby has:
    a, built-in classes (e.g. Array, String, Time etc.)
    b, standard library (organized in files and modules - e.g. date.rb)

    Pickaxe has a chapter on the standard library and another one on the network
    library. Unfortunately it is not complete (and situation became worse with
    1.8). Anyway browsing Ruby directory is a good learning exercise.

    Dalibor

    --
    Dalibor Sramek [url]http://www.insula.cz/dali[/url] | In the eyes of cats,
    [email]dalibor.sramek@insula.cz[/email] | all things belong to cats.

    Dalibor Sramek Guest

  7. #6

    Default Re: Time: safe way to go to next day?

    Emmanuel Touzery <emmanuel.touzery@wanadoo.fr> wrote:
    > [email]nobu.nokada@softhome.net[/email] wrote:
    >
    > >If you just want dates, what about date.rb?
    > >
    > > require 'date'
    > > today = Date.today
    > > today.to_s # => "2003-10-21"
    > > (today+5).to_s # => "2003-10-26"
    > >
    > >I've not tested it with DST though.
    >
    > otherwise, yes it's probably what i want. unfortunately i now sit on a
    > lot of code to convert :O(
    > if someone has a way with Time, it would simplify things for me...
    Here's half a solution:

    class Time
    require 'date'
    DAY = 60*60*24

    alias add +
    def +(s)
    return add(s) unless s % DAY == 0
    d = Date.new(year, mon, day) + s / DAY
    Time.local(d.year, d.mon, d.day, hour, min, sec, usec)
    end
    end
    Sabby and Tabby Guest

  8. #7

    Default Re: Time: safe way to go to next day?


    Given a Time representing the instant you care about, you
    can get the time N days later by just adding N days' worth
    of seconds. Here's a simple method definition:

    class Time
    SECONDS_PER_DAY = 86_400
    def add_days(n)
    self + n * SECONDS_PER_DAY
    end
    end

    No Date required.

    irb(main):001:0> t = Time.now
    => Tue Oct 21 13:15:03 EDT 2003
    irb(main):002:0> t.add_days(5)
    => Sun Oct 26 12:15:03 EST 2003

    -Mark
    Mark J. Reed Guest

  9. #8

    Default Re: Time: safe way to go to next day?

    il Tue, 21 Oct 2003 16:19:48 +0900, Emmanuel Touzery
    <emmanuel.touzery@wanadoo.fr> ha scritto::

    >but where is all this stuff documented?
    >find.rb, base64.rb, date.rb, the other day someone wanted to remove
    >recursively a dir, and was given another one of those magic files...
    >which doc did i miss? :O(
    >
    some is in the pickaxe.
    Some you may just found looking at ruby-dir/lib, for the classes like
    Array or String there is ri.

    You may like to look at rj (as in 'ri'.succ) wich works like ri but
    incorporates some more stuff (see on rubyforge).

    ruby-doc.org has ri++ wich is 'ri + documentation via web' . Quite
    useful too ;)

    gabriele renzi Guest

Posting Permissions

  • You may not post new threads
  • You may 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