# Millisec in strin representation of Time

**URL:** <https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439>\
**Category:** ruby-talk\
**Created:** [27 August 2005 22:07 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439 "2005-08-27T22:07:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [27 August 2005 22:07 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/1 "2005-08-27T22:07:16Z")

</div>

Is there a way to use Time#strftime to generate a string that contains  
milliseconds, and to do so in such a way that Time.parse understands it?

In other words, a value of ms\_format that makes this work:

t\_now = [Time.at](http://Time.at)(1125180070.216)  
diff = Time.parse(t\_now.strftime(ms\_format)) - t\_now  
diff.abs \< 0.001 # ==\> true

The default format does not represent milliseconds, so the diff can be  
up to 1.0:

default\_format = "%a %b %d %H:%M:%S %Z %Y"  
t\_now = [Time.at](http://Time.at)(1125180070.216)  
diff = Time.parse(t\_now.strftime(default\_format)) - t\_now  
p diff.abs # =\> 0.216

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [27 August 2005 22:39 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/2 "2005-08-27T22:39:37Z")

</div>

This may not exactly answer your question, but you you parse with strptime() instead?

James Edward Gray II

> **···**
>
> On Aug 27, 2005, at 5:07 PM, Joel VanderWerf wrote:
> 
> > Is there a way to use Time#strftime to generate a string that contains  
> > milliseconds, and to do so in such a way that Time.parse understands it?
> > 
> > In other words, a value of ms\_format that makes this work:
> > 
> > t\_now = Time.at(1125180070.216)  
> > diff = Time.parse(t\_now.strftime(ms\_format)) - t\_now  
> > diff.abs \< 0.001 # ==\> true
> > 
> > The default format does not represent milliseconds, so the diff can be  
> > up to 1.0:
> > 
> > default\_format = "%a %b %d %H:%M:%S %Z %Y"  
> > t\_now = Time.at(1125180070.216)  
> > diff = Time.parse(t\_now.strftime(default\_format)) - t\_now  
> > p diff.abs # =\> 0.216

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [27 August 2005 23:08 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/3 "2005-08-27T23:08:43Z")

</div>

Time.parse works via ParseDate.parsedate, and that, in turn, passes all the  
heavy lifting to Date.\_parse. Date.\_parse, nifty bit of code that it is,  
does parse out fractional seconds. However, parsedate doesn't look for the  
fractional seconds, so they get lost in the conversion back to a Time object.

And if one reimplements the parsedate method to not ignore the fractional  
seconds, one runs into other problems with Time when it encounters that. So,  
while I'd love to be wrong, the only way that I see around this is dealing  
with the fractional seconds yourself by reopening Time and overriding  
Time#parse.

Here's one way. If you put this in an external file, require it after you  
require 'time'.

class Time  
&nbsp;&nbsp;class \<\< Time  
&nbsp;&nbsp;&nbsp;&nbsp;alias :parse\_old :parse  
&nbsp;&nbsp;&nbsp;&nbsp;def parse(date, now=Time.now)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = parse\_old(date,now)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = Date.\_parse(date)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t += d[:sec\_fraction].to\_f if d.has\_key? :sec\_fraction  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

And here's an example of it in use:

irb(main):003:0\> a = Time.parse('23 Aug 2005 19:00:01.1')  
=\> Tue Aug 23 19:00:01 MDT 2005  
irb(main):004:0\> a.to\_f  
=\> 1124845201.1  
irb(main):005:0\> b = Time.parse('23 Aug 2005 19:00:01.9')  
=\> Tue Aug 23 19:00:01 MDT 2005  
irb(main):006:0\> b.to\_f  
=\> 1124845201.9  
irb(main):007:0\> b-a  
=\> 0.8

Kirk Haines

> **···**
>
> On Saturday 27 August 2005 4:07 pm, Joel VanderWerf wrote:
> 
> > Is there a way to use Time#strftime to generate a string that contains  
> > milliseconds, and to do so in such a way that Time.parse understands it?
> > 
> > In other words, a value of ms\_format that makes this work:
> > 
> > t\_now = Time.at(1125180070.216)  
> > diff = Time.parse(t\_now.strftime(ms\_format)) - t\_now  
> > diff.abs \< 0.001 # ==\> true

---

<div class="post-metadata">

**Author:** ![Tanaka\_Akira1](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@Tanaka\_Akira1](https://rubytalk.org/u/Tanaka_Akira1)\
**Post date:** [28 August 2005 15:41 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/4 "2005-08-28T15:41:43Z")

</div>

In article \<200508271709.46894.khaines@enigo.com\>,  
&nbsp;&nbsp;Kirk Haines \<khaines@enigo.com\> writes:

> Time.parse works via ParseDate.parsedate, and that, in turn, passes all the  
> heavy lifting to Date.\_parse. Date.\_parse, nifty bit of code that it is,  
> does parse out fractional seconds. However, parsedate doesn't look for the  
> fractional seconds, so they get lost in the conversion back to a Time object.

I see. I modified time.rb to use Date.\_parse directly.

> **···**
>
> --  
> Tanaka Akira

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [28 August 2005 20:21 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/5 "2005-08-28T20:21:14Z")

</div>

Kirk Haines wrote:  
...

> class Time  
> &nbsp;&nbsp;class \<\< Time  
> &nbsp;&nbsp;&nbsp;&nbsp;alias :parse\_old :parse  
> &nbsp;&nbsp;&nbsp;&nbsp;def parse(date, now=Time.now)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = parse\_old(date,now)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = Date.\_parse(date)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t += d[:sec\_fraction].to\_f if d.has\_key? :sec\_fraction

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t

> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

If the \_date\_ arg doesn't have fractional seconds, then parse still  
needs to return t.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [28 August 2005 20:06 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/6 "2005-08-28T20:06:03Z")

</div>

Tanaka Akira wrote:

> In article \<200508271709.46894.khaines@enigo.com\>,  
> &nbsp;&nbsp;Kirk Haines \<khaines@enigo.com\> writes:
> 
> > Time.parse works via ParseDate.parsedate, and that, in turn, passes all the  
> > heavy lifting to Date.\_parse. Date.\_parse, nifty bit of code that it is,  
> > does parse out fractional seconds. However, parsedate doesn't look for the  
> > fractional seconds, so they get lost in the conversion back to a Time object.
> 
> I see. I modified time.rb to use Date.\_parse directly.

Works perfectly. Many thanks to both of you, Kirk and Tanaka!

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Andrew\_S\_Townley](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Andrew\_S\_Townley](https://rubytalk.org/u/Andrew_S_Townley)\
**Post date:** [29 August 2005 12:58 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/7 "2005-08-29T12:58:56Z")

</div>

Hi folks,

Did I miss something or did someone actually answer this part of the  
question?

> Is there a way to use Time#strftime to generate a string that contains  
> milliseconds,

I found myself hitting this same thing on Saturday (kinda ironic that  
was when the original post was sent to the list). What I want to do is  
something similar to the Java SimpleDateFormat where you can do  
something like: yyyy-MM-dd HH:mm:ss.SSS z

I looked at the implementation of strftime in the ruby library and  
there's some unused letters for %, however what I want is to be able to  
specify the precision at the same time. I had considered hacking the  
implementation of strftime and sending it to the list, but I hadn't  
gotten there yet. I don't want to have to just do something like:

"%d" % (time.usec / (10\*precision))

if I can avoid it. Before I go off and implement something silly, does  
anyone have any ideas?

Thanks in advance,

ast

> **···**
>
> On Sun, 2005-08-28 at 21:21, Joel VanderWerf wrote:
> 
> > Kirk Haines wrote:  
> > ...  
> > \> class Time  
> > \> class \<\< Time  
> > \> alias :parse\_old :parse  
> > \> def parse(date, now=Time.now)  
> > \> t = parse\_old(date,now)  
> > \> d = Date.\_parse(date)  
> > \> t += d[:sec\_fraction].to\_f if d.has\_key? :sec\_fraction  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t  
> > \> end  
> > \> end  
> > \> end
> > 
> > If the \_date\_ arg doesn't have fractional seconds, then parse still  
> > needs to return t.
> 
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> The information in this email is confidential and may be legally privileged. Access to this email by anyone other than the intended addressee is unauthorized. If you are not the intended recipient of this message, any review, disclosure, copying, distribution, retention, or any action taken or omitted to be taken in reliance on it is prohibited and may be unlawful. If you are not the intended recipient, please reply to or forward a copy of this message to the sender and delete the message, any attachments, and any copies thereof from your system.  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [29 August 2005 18:19 UTC](https://rubytalk.org/t/millisec-in-strin-representation-of-time/20439/8 "2005-08-29T18:19:40Z")

</div>

Andrew S. Townley wrote:

> Hi folks,
> 
> Did I miss something or did someone actually answer this part of the  
> question?
> 
> > Is there a way to use Time#strftime to generate a string that contains  
> > milliseconds,

No, only the parsing side of the question was discussed.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
