Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Millisecond in time.

Reply
Thread Tools

Millisecond in time.

 
 
Warren
Guest
Posts: n/a
 
      07-01-2003
Hi,

I have 1056030443784 (which is a time in millisecond) but I need this
time in the "normal" format like 2003-06-26 12:54.22

But I can't find any methods in ruby that can help me.
Any advice ?

Regards
W.B
 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      07-01-2003
Hi,

In message "Millisecond in time."
on 03/07/01, Warren <(E-Mail Removed)> writes:

|I have 1056030443784 (which is a time in millisecond) but I need this
|time in the "normal" format like 2003-06-26 12:54.22

Time.at(1056030443784/1000.0).strftime("%Y-%m-%d %H:%M.%S")

matz.

 
Reply With Quote
 
 
 
 
Harry Ohlsen
Guest
Posts: n/a
 
      07-01-2003
Warren wrote:
> Hi,
>
> I have 1056030443784 (which is a time in millisecond) but I need this
> time in the "normal" format like 2003-06-26 12:54.22


Time has an "at" method that takes a number of seconds and returns a time, so you could do something like ...

ms = 1056030443784

time = Time.at(ms / 1000)

puts time

It also takes a number of microseconds as a second argument, so you could also do

time = Time.at(ms / 1000, (ms % 1000) * 1000)

to get a more accurate time, if necessary. Apologies if my arithmetic is crap in the second example .

H.



 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-01-2003
On Tue, Jul 01, 2003 at 07:28:48PM +0900, Anders Borch wrote:
> > It also takes a number of microseconds as a second argument, so you
> > could also do
> >
> > time = Time.at(ms / 1000, (ms % 1000) * 1000)

>
> wouldn't that be
>
> time = Time.at(ms / 1000, ms % 1000)
>
> '%' returns the fraction part *as an integer* iirc


but 1 millisecond is 1000 microseconds.

 
Reply With Quote
 
Harry Ohlsen
Guest
Posts: n/a
 
      07-01-2003
Anders Borch wrote:

>> It also takes a number of microseconds as a second argument, so you
>> could also do
>>
>> time = Time.at(ms / 1000, (ms % 1000) * 1000)

>
>
> wouldn't that be
>
> time = Time.at(ms / 1000, ms % 1000)
>
> '%' returns the fraction part *as an integer* iirc


Yes, but the second argument to Time#at is in microseconds.

Since the original value was milliseconds, taking it mod 1000 gives us just the leftover milliseconds (ie, the ones that don't add up to a multiple of a second).

We then need to multiply that by 1000, since microseconds are 1000 times smaller than milliseconds.

Or is my logic still screwed up? As I said, my arithmetic is terrible .




 
Reply With Quote
 
Harry Ohlsen
Guest
Posts: n/a
 
      07-01-2003
Given that we're talking about modulo arithmetic ...

I'm sitting here trying to write some Ruby code that generates the "magic table" from the remainders that pop up when executing Euclid's algorithm. I don't suppose anyone happens to remember how that works?

I'm writing the Ruby partly to remind myself of the number theory, but with the intention of producing something that my old lecturer might be able to use to allow him to easily come up with new tutorial, quiz and assignment questions, without having to slog through the calculations.

You can see an example of such a table in the solution to question 7, part (b) in the following PDF ...

http://www.maths.usyd.edu.au:8000/u/...09/02nt01s.pdf

I remember it has to do with multiplying two diagonal elements then adding one on the left, but can't for the life of me work out which ones !!

Cheers,

Harry O.


 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      07-01-2003
>>>>> "H" == Harry Ohlsen <(E-Mail Removed)> writes:

H> I'm sitting here trying to write some Ruby code that generates the
H> "magic table" from the remainders that pop up when executing Euclid's
H> algorithm. I don't suppose anyone happens to remember how that works?

Euh, this ???


svg% cat b.rb
#!/usr/bin/ruby
def lcr(a, b, v)
r = a % b
if r == 0
[]
else
q = a / b
x = [v[0] - q * v[2], v[1] - q * v[3]]
[x] + lcr(b, r, [v[2], v[3], *x])
end
end

def lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[a, b]]
end

p lc(93512222, 6812345)
svg%

svg% b.rb
[[1, -13], [-1, 14], [3, -41], [-4, 55], [7, -96], [-11, 151], [227, -3116], [-919, 12615], [42501, -583406], [-43420, 596021], [1345101, -18464036], [-2733622, 37524093], [93512222, 6812345]]
svg%


Guy Decoux

 
Reply With Quote
 
Harry Ohlsen
Guest
Posts: n/a
 
      07-01-2003
ts wrote:

> Euh, this ???
>
>
> svg% cat b.rb
> #!/usr/bin/ruby
> def lcr(a, b, v)
> r = a % b
> if r == 0
> []
> else
> q = a / b
> x = [v[0] - q * v[2], v[1] - q * v[3]]
> [x] + lcr(b, r, [v[2], v[3], *x])
> end
> end
>
> def lc(a, b)
> if b == 0
> [[1, 0]]
> else
> lcr(a, b, [1, 0, 0, 1])
> end + [[a, b]]
> end
>
> p lc(93512222, 6812345)
> svg%
>
> svg% b.rb
> [[1, -13], [-1, 14], [3, -41], [-4, 55], [7, -96], [-11, 151], [227, -3116], [-919, 12615], [42501, -583406], [-43420, 596021], [1345101, -18464036], [-2733622, 37524093], [93512222, 6812345]]
> svg%


I'm not sure about all those negative numbers, but a heck of a lot of the values look right. So, I think your code is definitely a good starting point for me.

It'll save me going down into the garage tonight ... it's 9:50pm here in Sydney ... to try and find my notes, anyway .

Thanks!

--
-------------------------------------
I have always imagined that Paradise will be
a kind of library. - Jorge Luis Borges.


 
Reply With Quote
 
Harry Ohlsen
Guest
Posts: n/a
 
      07-01-2003
ts wrote:

>>>>>>"H" == Harry Ohlsen <(E-Mail Removed)> writes:

>
>
> H> I'm sitting here trying to write some Ruby code that generates the
> H> "magic table" from the remainders that pop up when executing Euclid's
> H> algorithm. I don't suppose anyone happens to remember how that works?
>
> Euh, this ???
>
>
> svg% cat b.rb
> #!/usr/bin/ruby
> def lcr(a, b, v)
> r = a % b
> if r == 0
> []
> else
> q = a / b
> x = [v[0] - q * v[2], v[1] - q * v[3]]
> [x] + lcr(b, r, [v[2], v[3], *x])
> end
> end
>
> def lc(a, b)
> if b == 0
> [[1, 0]]
> else
> lcr(a, b, [1, 0, 0, 1])
> end + [[a, b]]
> end
>
> p lc(93512222, 6812345)
> svg%
>
> svg% b.rb
> [[1, -13], [-1, 14], [3, -41], [-4, 55], [7, -96], [-11, 151], [227, -3116], [-919, 12615], [42501, -583406], [-43420, 596021], [1345101, -18464036], [-2733622, 37524093], [93512222, 6812345]]
> svg%
>
>
> Guy Decoux
>
>


I was able to get your code to produce the table precisely as it was in the exercise solution by making a couple of tiny changes, although I'm going to have to think about how your recursive code relates to my simplistic implementation of the Euclidean algorithm ...


def lcr(a, b, v)
r = a % b
if r == 0
[]
else
q = a / b
x = [v[0] - q * v[2], v[1] - q * v[3]]
[x] + lcr(b, r, [v[2], v[3], *x])
end
end

def lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[b, a]]
end

p lc(93512222, 6812345).map { |x| [x[1].abs, x[0].abs] }

D:\HarryO\Mathematics\NumberTheory>ruby guy.rb

[[13, 1], [14, 1], [41, 3], [55, 4], [96, 7], [151, 11], [3116, 227], [12615, 91
9], [583406, 42501], [596021, 43420], [18464036, 1345101], [37524093, 2733622],
[93512222, 6812345]]



 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      07-02-2003
>>>>> "H" == Harry Ohlsen <(E-Mail Removed)> writes:

H> Obviously, your definition of the table is slightly different to my old
H> lecturer's, because his doesn't generate any negative values. I'll just
H> have to sit down and work out where the approaches differ.

It came from here :

http://www.maths.usyd.edu.au:8000/u/...ATH3009/wk2.pd



Guy Decoux


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to implement a calendar clock with millisecond resolution, please help. Hunter C Programming 7 09-12-2011 11:59 PM
UTC time in millisecond palmis Java 26 01-19-2006 07:38 AM
timer with millisecond precision WeAreBorg C Programming 2 09-29-2005 12:27 AM
How to sleep during millisecond Yeounkun, Oh C Programming 11 12-14-2004 02:29 PM
millisecond delay in C - linux Macgyver C Programming 8 01-07-2004 12:28 PM



Advertisments