Millisec in strin representation of Time

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

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

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
  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
    end
  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

In article <200508271709.46894.khaines@enigo.com>,
  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

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

        t

    end
  end
end

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

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Tanaka Akira wrote:

In article <200508271709.46894.khaines@enigo.com>,
  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!

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

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.

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407