# Millisecond in time

**URL:** <https://rubytalk.org/t/millisecond-in-time/6646>\
**Category:** ruby-talk\
**Created:** [1 July 2003 07:47 UTC](https://rubytalk.org/t/millisecond-in-time/6646 "2003-07-01T07:47:06Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Place4oracle](https://avatars.discourse-cdn.com/v4/letter/p/e79b87/32.png) [@Place4oracle](https://rubytalk.org/u/Place4oracle)\
**Post date:** [1 July 2003 07:47 UTC](https://rubytalk.org/t/millisecond-in-time/6646/1 "2003-07-01T07:47:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [1 July 2003 08:00 UTC](https://rubytalk.org/t/millisecond-in-time/6646/2 "2003-07-01T08:00:54Z")

</div>

Hi,

> **···**
>
> In message “Millisecond in time.” on 03/07/01, Warren [place4oracle@yahoo.com](mailto:place4oracle@yahoo.com) 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.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [1 July 2003 08:05 UTC](https://rubytalk.org/t/millisecond-in-time/6646/3 "2003-07-01T08:05:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anders\_Borch2](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Anders\_Borch2](https://rubytalk.org/u/Anders_Borch2)\
**Post date:** [1 July 2003 10:28 UTC](https://rubytalk.org/t/millisecond-in-time/6646/4 "2003-07-01T10:28:48Z")

</div>

Harry Ohlsen wrote:

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

wouldn’t that be

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

‘%’ returns the fraction part _as an integer_ iirc

> **···**
>
> > to get a more accurate time, if necessary. Apologies if my arithmetic  
> > is crap in the second example :-).
> > 
> > H.
> 
> –  
> dc -e  
> 4dd_od3_dddn1-89danrn10-dan3+ann6_dan_2_an13dn1+dn2-dn3+5_ddan2/9+an13nap

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [1 July 2003 10:35 UTC](https://rubytalk.org/t/millisecond-in-time/6646/5 "2003-07-01T10:35:14Z")

</div>

but 1 millisecond is 1000 microseconds.

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

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [1 July 2003 10:41 UTC](https://rubytalk.org/t/millisecond-in-time/6646/6 "2003-07-01T10:41:58Z")

</div>

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 :-).

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [1 July 2003 10:52 UTC](https://rubytalk.org/t/millisecond-in-time/6646/7 "2003-07-01T10:52:35Z")

</div>

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/terry/3009/02nt01s.pdf](http://www.maths.usyd.edu.au:8000/u/terry/3009/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.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [1 July 2003 11:47 UTC](https://rubytalk.org/t/millisecond-in-time/6646/8 "2003-07-01T11:47:51Z")

</div>

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

Euh, this ???

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

def lc(a, b)  
&nbsp;&nbsp;&nbsp;if b == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[1, 0]]  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lcr(a, b, [1, 0, 0, 1])  
&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [1 July 2003 11:56 UTC](https://rubytalk.org/t/millisecond-in-time/6646/9 "2003-07-01T11:56:55Z")

</div>

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]]  
> + 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.

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [1 July 2003 12:15 UTC](https://rubytalk.org/t/millisecond-in-time/6646/10 "2003-07-01T12:15:39Z")

</div>

ts wrote:

> > > > > > “H” == Harry Ohlsen [harryo@qiqsolutions.com](mailto:harryo@qiqsolutions.com) writes:
> 
> > 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?
> 
> 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]]  
> + 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]]  
 + 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]]

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [1 July 2003 12:26 UTC](https://rubytalk.org/t/millisecond-in-time/6646/11 "2003-07-01T12:26:24Z")

</div>

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

Well, if I'm remember correctly : if (p, q) are number in the "magic  
table", then

p = p[k-1] \* (-1)\*\*k-1  
q = q[k-1] \* (-1)\*\*k

where p[k-1], q[k-1] are the value given by the Euclidean algorithm

This must explain the negative number becuase it give the result of the  
Euclidena algorithm.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [1 July 2003 23:52 UTC](https://rubytalk.org/t/millisecond-in-time/6646/12 "2003-07-01T23:52:24Z")

</div>

ts wrote:

> Well, if I’m remember correctly : if (p, q) are number in the “magic  
> table”, then
> 
> p = p[k-1] \* (-1)\*\*k-1  
> q = q[k-1] \* (-1)\*\*k
> 
> where p[k-1], q[k-1] are the value given by the Euclidean algorithm
> 
> This must explain the negative number becuase it give the result of the  
> Euclidena algorithm.

Thanks, Guy.

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

That’s not a problem to me, because I’m keen to understand it. That’s my reason for writing the code, after all :-).

Cheers,

Harry O.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [2 July 2003 08:31 UTC](https://rubytalk.org/t/millisecond-in-time/6646/13 "2003-07-02T08:31:58Z")

</div>

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

It came from here :

[http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd](http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd)

Guy Decoux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [2 July 2003 08:34 UTC](https://rubytalk.org/t/millisecond-in-time/6646/14 "2003-07-02T08:34:19Z")

</div>

> [http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd](http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^

it's pdf (the `f' is missing)

&nbsp;&nbsp;[http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pdf](http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pdf)

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [2 July 2003 08:43 UTC](https://rubytalk.org/t/millisecond-in-time/6646/15 "2003-07-02T08:43:49Z")

</div>

ts wrote:

> It came from here :
> 
> [http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd](http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd)

How amazing! Bob Howlett (the author of that PDF) is one of my other old lecturers :-).

I’ll have to point out to he and Terry Gagen (the person I’m hoping to help with this code) that maybe they should get their stories straight :-).

Harry O.

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [2 July 2003 09:50 UTC](https://rubytalk.org/t/millisecond-in-time/6646/16 "2003-07-02T09:50:50Z")

</div>

ts wrote:

> It came from here :
> 
> [http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd](http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd)

Thanks again, Guy. Based on that reference, I was able to get my naive (non-recursive) implementation working and generating the correct magic table …

module Euclid  
class MagicTable \< Array  
Row = Struct.new(“Row”, :d, :p, :q)

```
  def initialize
     self << Row.new(nil, 0, 1)
     self << Row.new(nil, 1, 0)
  end

  alias :<< push

  def <<(row)
     push(Row.new(*row))
  end

  def to_s
     s = ""

     self.each do |row|
        d = row.d

        if d.nil?
           s << " "
        else
           s << (sprintf "%4d ", d)
        end
     end

     s << "\n"

     self.each { |row| s << (sprintf "%4d ", row.p) }

     s << "\n"

     self.each { |row| s << (sprintf "%4d ", row.q) }

     s << "\n"

     return s
  end

```

end

def Euclid.GCD(a, b)  
magic = MagicTable.new

```
  if a < b
     a, b = b, a
  end

  while b > 0
     d = a / b
     r = a % b

     p = d * magic[-1].p + magic[-2].p
     q = d * magic[-1].q + magic[-2].q

     magic << [d, p, q]

     a, b = b, a % b
  end

  return a, magic

```

end

end

if $0 == **FILE**  
gcd, magic = Euclid.GCD(787, 268)

puts “(787, 268) = #{gcd}\n\n”

puts “Magic Table …\n\n#{magic}”  
end

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen1](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Harry\_Ohlsen1](https://rubytalk.org/u/Harry_Ohlsen1)\
**Post date:** [2 July 2003 08:45 UTC](https://rubytalk.org/t/millisecond-in-time/6646/17 "2003-07-02T08:45:33Z")

</div>

By the way, thanks for that link. It’s basically what I was looking for before I asked the original question!
