# a=Time.now;(Time.parse(a.to\_s)-a)!=0

**URL:** <https://www.ruby-forum.com/t/a-time-now-time-parse-a-to-s-a-0/118366>\
**Category:** Ruby\
**Created:** [October 26, 2007, 6:04am UTC](https://www.ruby-forum.com/t/a-time-now-time-parse-a-to-s-a-0/118366 "2007-10-26T06:04:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [October 26, 2007, 6:04am UTC](https://www.ruby-forum.com/t/a-time-now-time-parse-a-to-s-a-0/118366/1 "2007-10-26T06:04:23Z")

</div>

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 C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [October 26, 2007, 6:17am UTC](https://www.ruby-forum.com/t/a-time-now-time-parse-a-to-s-a-0/118366/2 "2007-10-26T06:17:15Z")

</div>

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

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

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [October 26, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/a-time-now-time-parse-a-to-s-a-0/118366/3 "2007-10-26T17:29:25Z")

</div>

John C. a écrit :

> a-Time.xmlschema(b)’

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

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [October 26, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/a-time-now-time-parse-a-to-s-a-0/118366/4 "2007-10-26T18:03:55Z")

</div>

On Oct 25, 2007, at 10:03 PM, John C. 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/](http://codeforpeople.com/)
