millisec in strin representation of Time

J

Joel VanderWerf

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
 
J

James Edward Gray II

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

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

James Edward Gray II
 
T

Tanaka Akira

Kirk Haines said:
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.
 
J

Joel VanderWerf

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

Andrew S. Townley

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

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

Joel VanderWerf

Andrew said:
Hi folks,

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top