Millisecond in time.

Discussion in 'Ruby' started by Warren, Jul 1, 2003.

  1. Warren

    Warren Guest

    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
     
    Warren, Jul 1, 2003
    #1
    1. Advertising

  2. Hi,

    In message "Millisecond in time."
    on 03/07/01, Warren <> 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.
     
    Yukihiro Matsumoto, Jul 1, 2003
    #2
    1. Advertising

  3. Warren

    Harry Ohlsen Guest

    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.
     
    Harry Ohlsen, Jul 1, 2003
    #3
  4. 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.
     
    Brian Candler, Jul 1, 2003
    #4
  5. Warren

    Harry Ohlsen Guest

    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 :).
     
    Harry Ohlsen, Jul 1, 2003
    #5
  6. Warren

    Harry Ohlsen Guest

    Terribly OT ...

    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

    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.
     
    Harry Ohlsen, Jul 1, 2003
    #6
  7. Warren

    ts Guest

    Re: Terribly OT ...

    >>>>> "H" == Harry Ohlsen <> 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
     
    ts, Jul 1, 2003
    #7
  8. Warren

    Harry Ohlsen Guest

    Re: Terribly OT ...

    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.
     
    Harry Ohlsen, Jul 1, 2003
    #8
  9. Warren

    Harry Ohlsen Guest

    Re: Terribly OT ...

    ts wrote:

    >>>>>>"H" == Harry Ohlsen <> 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]]
     
    Harry Ohlsen, Jul 1, 2003
    #9
  10. Warren

    ts Guest

    Re: Terribly OT ...

    >>>>> "H" == Harry Ohlsen <> 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/bobh/UoS/MATH3009/wk2.pd



    Guy Decoux
     
    ts, Jul 2, 2003
    #10
  11. Warren

    Harry Ohlsen Guest

    Re: Terribly OT ...

    ts wrote:

    > It came from here :
    >
    > 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.
     
    Harry Ohlsen, Jul 2, 2003
    #11
  12. Warren

    Harry Ohlsen Guest

    Re: Terribly OT ...

    ts wrote:

    > It came from here :
    >
    > 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
     
    Harry Ohlsen, Jul 2, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. palmis

    UTC time in millisecond

    palmis, Jan 10, 2006, in forum: Java
    Replies:
    26
    Views:
    24,867
    Roedy Green
    Jan 19, 2006
  2. Macgyver

    millisecond delay in C - linux

    Macgyver, Jan 7, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,585
    August Derleth
    Jan 7, 2004
  3. etantonio
    Replies:
    1
    Views:
    426
    John B. Matthews
    Mar 16, 2009
  4. Alex van der Spek

    logging time format millisecond precision

    Alex van der Spek, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    179
    Alex van der Spek
    Jul 19, 2012
  5. Alex van der Spek
    Replies:
    2
    Views:
    284
    David Bolen
    Jul 20, 2012
Loading...

Share This Page