[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

John Carter

10/26/2007 4:04:00 AM

9 Answers

Alex Gutteridge

10/26/2007 4:17:00 AM

0

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



Olivier

10/26/2007 3:29:00 PM

0

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 : john.carter@tait.co.nz
> 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

ara.t.howard

10/26/2007 4:04:00 PM

0


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://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



NEMO

2/23/2012 10:02:00 AM

0

"did you ever look a gorse in its face? they are really ugly"

That sounds more like a Dutchman!

& what the fooooooooook is a "gorse", fool?

der Julius

2/23/2012 10:06:00 AM

0



"NEMO" schreef in bericht
news:1e274ab6-8d65-4c3b-a34d-22140b7263c8@d17g2000vba.googlegroups.com...

"did you ever look a gorse in its face? they are really ugly"

That sounds more like a Dutchman!

& what the fooooooooook is a "gorse", fool?

Verzeihung ein Feher ist unterlaufen.Please read horse instead of gorse

The Revd

2/23/2012 11:32:00 AM

0

On Thu, 23 Feb 2012 11:05:34 +0100, "der Julius"
<derJulius@Paulanernnet.de> wrote:

>
>
>"NEMO" schreef in bericht
>news:1e274ab6-8d65-4c3b-a34d-22140b7263c8@d17g2000vba.googlegroups.com...
>
>"did you ever look a gorse in its face? they are really ugly"
>
>That sounds more like a Dutchman!
>
>& what the fooooooooook is a "gorse", fool?
>
>Verzeihung ein Feher ist unterlaufen.Please read horse instead of gorse

Did you ever look a jew wog in its face. THAT is ugly!

The Peeler

2/23/2012 11:51:00 AM

0

On Thu, 23 Feb 2012 11:05:34 +0100, der Julius, better known as Dumb Heini,
the Dutch resident Nazi troll of sci and scj, wrote:

> "did you ever look a gorse in its face? they are really ugly"
>
> That sounds more like a Dutchman!
>
> & what the fooooooooook is a "gorse", fool?
>
> Verzeihung ein Feher ist unterlaufen.Please read horse instead of gorse

So when will you finally apologize to everyone for a being a COMPLETE IDIOT,
Dumb Dutch Nazi Heini? It's long overdue!


--
The Dutch Nazitard: why do you americans always have to call me hei9nnie
while my posatings clearly indicaate my name is heinrich?
MID: <4dad5b41$0$20532$2e0edba0@news.tweakdsl.nl>

NEMO

2/23/2012 4:43:00 PM

0

" Did you ever look your mother wog in its face, Pig Boy?  THAT is
ugly!"

Ahab the ...

2/23/2012 6:02:00 PM

0

NEMO <brianlambsbigtoe@excite.com> wrote in news:59244959-0690-45c2-9c5c-
9e3617ec77ff@eb6g2000vbb.googlegroups.com:

> " Did you ever look your mother wog in its face, Pig Boy? ?THAT is
> ugly!"

Piss off. This crap is out of line.