a=Time.now;(Time.parse(a.to_s)-a)!=0

Discussion in 'Ruby' started by John Carter, Oct 26, 2007.

  1. John Carter

    John Carter Guest

    Hmm. Ooh yuck.

    Try this...

    $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    0.00848
    $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    0.446575
    $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    0.142796

    ie. Time#to_s doesn't represent the full precision of the internal
    time format.

    Bit of a bummer if you want to round trip an exact timestamp onto disk
    and back.

    This does it right...
    $ ruby -e 'a=Time.now;p a-Marshal::load(Marshal::dump(a))'
    0.0


    ...but look at whats on disk...
    ruby -e 'a=Time.now;p Marshal::dump(a)'
    "\004\bu:\tTime\rC\347\032\200z\343S\302"

    Eeew! Not exactly human friendly.

    The following is probably the most elegant way of exactly round
    tripping a time to disk and back in a human readable form?

    ruby -w -rtime -e 'a=Time.now;b = a.xmlschema(6);p b;p a-Time.xmlschema(b)'
    "2007-10-26T17:01:08.129059+13:00"
    0.0


    Ah well.

    John Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    New Zealand
     
    John Carter, Oct 26, 2007
    #1
    1. Advertising

  2. On 26 Oct 2007, at 13:03, John Carter wrote:

    > Hmm. Ooh yuck.
    >
    > Try this...
    >
    > $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    > 0.00848
    > $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    > 0.446575
    > $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    > 0.142796
    >
    > ie. Time#to_s doesn't represent the full precision of the internal
    > time format.
    >
    > Bit of a bummer if you want to round trip an exact timestamp onto disk
    > and back.
    >
    > This does it right...
    > $ ruby -e 'a=Time.now;p a-Marshal::load(Marshal::dump(a))'
    > 0.0
    >
    >
    > ...but look at whats on disk...
    > ruby -e 'a=Time.now;p Marshal::dump(a)'
    > "\004\bu:\tTime\rC\347\032\200z\343S\302"
    >
    > Eeew! Not exactly human friendly.
    >
    > The following is probably the most elegant way of exactly round
    > tripping a time to disk and back in a human readable form?
    >
    > ruby -w -rtime -e 'a=Time.now;b = a.xmlschema(6);p b;p a-
    > Time.xmlschema(b)'
    > "2007-10-26T17:01:08.129059+13:00"
    > 0.0


    You can use YAML if you only want Time.

    irb(main):002:0> Time.now.to_yaml
    => "--- 2007-10-26 13:10:45.012414 +09:00\n"
    irb(main):003:0> YAML.load(Time.now.to_yaml)
    => Fri Oct 26 13:10:55 +0900 2007

    irb(main):004:0> t = Time.now
    => Fri Oct 26 13:13:05 +0900 2007
    irb(main):005:0> t - YAML.load(t.to_yaml)
    => 0.0

    Though there's a bug with DateTime:

    irb(main):006:0> DateTime.now.to_yaml
    => "--- !timestamp 2007-10-26T13:11:15+0900\n"
    irb(main):007:0> YAML.load(DateTime.now.to_yaml)
    => Wed Sep 19 10:11:26 +0900 2007

    Alex Gutteridge

    Bioinformatics Center
    Kyoto University
     
    Alex Gutteridge, Oct 26, 2007
    #2
    1. Advertising

  3. John Carter a écrit :
    > Hmm. Ooh yuck.
    >
    > Try this...
    >
    > $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    > 0.00848
    > $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    > 0.446575
    > $ ruby -r time -e 'a=Time.now;p a-Time.parse(a.to_s)'
    > 0.142796
    >
    > ie. Time#to_s doesn't represent the full precision of the internal
    > time format.
    >
    > Bit of a bummer if you want to round trip an exact timestamp onto disk
    > and back.
    >
    > This does it right...
    > $ ruby -e 'a=Time.now;p a-Marshal::load(Marshal::dump(a))'
    > 0.0
    >
    >
    > ...but look at whats on disk...
    > ruby -e 'a=Time.now;p Marshal::dump(a)'
    > "\004\bu:\tTime\rC\347\032\200z\343S\302"
    >
    > Eeew! Not exactly human friendly.
    >
    > The following is probably the most elegant way of exactly round
    > tripping a time to disk and back in a human readable form?
    >
    > ruby -w -rtime -e 'a=Time.now;b = a.xmlschema(6);p b;p
    > a-Time.xmlschema(b)'
    > "2007-10-26T17:01:08.129059+13:00"
    > 0.0
    >
    >
    > Ah well.
    >
    > John Carter Phone : (64)(3) 358 6639
    > Tait Electronics Fax : (64)(3) 359 4632
    > PO Box 1645 Christchurch Email :
    > New Zealand
    >
    >
    >
    >

    Hi,
    It seems that Time objects are precise to the microsecond, but to_s (and
    to_i) limit the precision to one second.

    a= Time.now
    b = Time.parse(a.to_s)
    a-b #=> 0.875
    a.usec #=> 875000


    I guess you could get a full representation of a Time object by
    combining to_s (or to_i) with #usec

    --
    Olivier Renaud
     
    Olivier Renaud, Oct 26, 2007
    #3
  4. John Carter

    ara.t.howard Guest

    On Oct 25, 2007, at 10:03 PM, John Carter wrote:

    > ruby -w -rtime -e 'a=Time.now;b = a.xmlschema(6);p b;p a-
    > Time.xmlschema(b)'
    > "2007-10-26T17:01:08.129059+13:00"
    > 0.0


    though i prefer #iso8601 to #xmlschema because even typing the word
    'xml' makes my skin crawl ;-)

    a @ http://codeforpeople.com/
    --
    share your knowledge. it's a way to achieve immortality.
    h.h. the 14th dalai lama
     
    ara.t.howard, Oct 26, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Giddings

    Array and Hash to_s

    Ben Giddings, Jul 19, 2003, in forum: Ruby
    Replies:
    1
    Views:
    88
    Yukihiro Matsumoto
    Jul 20, 2003
  2. Peter Krantz
    Replies:
    2
    Views:
    197
  3. Ben Winekur

    Override Time#to_s -- how?

    Ben Winekur, Oct 24, 2006, in forum: Ruby
    Replies:
    6
    Views:
    169
    Brian Neal
    Oct 24, 2006
  4. Jonas

    Time.to_s

    Jonas, Jul 1, 2007, in forum: Ruby
    Replies:
    3
    Views:
    94
    John Joyce
    Jul 1, 2007
  5. Utsav Gupta

    Reverting date from Time.to_s

    Utsav Gupta, Mar 22, 2010, in forum: Ruby
    Replies:
    1
    Views:
    92
    Chuck Remes
    Mar 22, 2010
Loading...

Share This Page