time.time()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Nessux

    time.time()

    am I doing this wrong:

    print (time.time() / 60) / 60 #time.time has been running for many hours

    if time.time() was (21600/60) then that would equal 360/60 which would
    be 6, but I'm not getting 6 so I'm not doing the division right, any tips?

  • Terry Carroll

    #2
    Re: time.time()

    On Sat, 24 Jan 2004 13:01:40 -0500, Bart Nessux <bart_nessux@ho tmail.com>
    wrote:
    [color=blue]
    >am I doing this wrong:
    >
    >print (time.time() / 60) / 60 #time.time has been running for many hours
    >
    >if time.time() was (21600/60) then that would equal 360/60 which would
    >be 6, but I'm not getting 6 so I'm not doing the division right, any tips?[/color]

    time.time() returns the number of seconds since the epoch. The epoch is
    generally Jan 1, 1970; you can check by trying time.gmtime(0).

    You're expecting time.time() to be 21600/60, or, in other words, for
    time() to be 1548; but 1548 seconds is only about 25 minutes. It's been a
    little more than 34 years since Jan 1, 1970, so the number you should
    expect from time.time() should be in the neighborhood of 34 years * 365.25
    days/year * 24 hours/day * 60 min/hour * 60 sec/min = 1072958400.

    Trying time.time() directly confirms this:
    [color=blue][color=green][color=darkred]
    >>> time.time()[/color][/color][/color]
    1074969863.888

    Yeah, it's not on the nose, but close enough for a sanity check. By the
    way, this is usually a good thing to try when you're lost at sea like this
    -- go back to the original input data, in this case, time.time() output,
    and see if it's giving you what you expect.

    It sounds like you're thinking time.time() does something else, which
    you're trying to do. What is that? There might be another function for
    you.

    Comment

    • Jeff Epler

      #3
      Re: time.time()

      time.time() returns seconds since a particular time in the past ("the
      Epoch"), not seconds since program start or anything like that.

      [color=blue][color=green][color=darkred]
      >>> import time
      >>> help(time.time)[/color][/color][/color]

      Help on built-in function time:

      time(...)
      time() -> floating point number

      Return the current time in seconds since the Epoch.
      Fractions of a second may be present if the system clock provides them.


      Comment

      • Bart Nessux

        #4
        Re: time.time()

        Terry Carroll wrote:
        [color=blue]
        > It sounds like you're thinking time.time() does something else, which
        > you're trying to do. What is that? There might be another function for
        > you.[/color]

        I should have been more clear... here's the code:

        x = time.time()
        fp = os.popen("some_ process", "r")
        fp.read()
        fp.close()
        print (time.time()-x/60)/60

        If fp runs for many hours (in my case it does), I want time.time() to
        return the time it runs in seconds which I would then divide by 60 to
        get the total amount of minutes which I would then divide by 60 to get
        the total amount of hours that the process has ran... does that make
        sense? I don't need to be super precise, just within 1 minute or so of
        the actual amount of time that the process required to run.

        Comment

        • Josiah Carlson

          #5
          Re: time.time()

          > x = time.time()[color=blue]
          > fp = os.popen("some_ process", "r")
          > fp.read()
          > fp.close()
          > print (time.time()-x/60)/60[/color]

          Parenthesis are your friend:
          print (time.time()-x)/3600

          - Josiah

          Comment

          • Peter Otten

            #6
            Re: time.time()

            Bart Nessux wrote:
            [color=blue]
            > Terry Carroll wrote:
            >[color=green]
            >> It sounds like you're thinking time.time() does something else, which
            >> you're trying to do. What is that? There might be another function for
            >> you.[/color]
            >
            > I should have been more clear... here's the code:[/color]

            That's not only clearer - that's different :-)
            [color=blue]
            > x = time.time()
            > fp = os.popen("some_ process", "r")
            > fp.read()
            > fp.close()
            > print (time.time()-x/60)/60
            >
            > If fp runs for many hours (in my case it does), I want time.time() to
            > return the time it runs in seconds which I would then divide by 60 to
            > get the total amount of minutes which I would then divide by 60 to get
            > the total amount of hours that the process has ran... does that make
            > sense? I don't need to be super precise, just within 1 minute or so of
            > the actual amount of time that the process required to run.[/color]
            [color=blue][color=green][color=darkred]
            >>> start = time()
            >>> start[/color][/color][/color]
            1074980579.6237 81[color=blue][color=green][color=darkred]
            >>> sleep(7200)
            >>> duration = time() - start
            >>> print "duration in hours:", duration/60/60[/color][/color][/color]
            duration in hours: 2.0[color=blue][color=green][color=darkred]
            >>> (time() - start) / 60 / 60[/color][/color][/color]
            2.0

            In the last expression, the parentheses are necessary, because - has lower
            precedence than /, i. e. otherwise you would calculate time() -
            (start/60/60). Note how I introduced an additional "duration" variable. If
            a calculation does not yield the expected result, it is often helpful to
            inspect some intermediate values.

            Peter

            PS: I cheated with my sleep() and time() functions:
            [color=blue][color=green][color=darkred]
            >>> def time():[/color][/color][/color]
            .... global _time
            .... try:
            .... return _time
            .... except NameError:
            .... import time
            .... _time = time.time()
            .... return _time
            ....[color=blue][color=green][color=darkred]
            >>> def sleep(s):[/color][/color][/color]
            .... global _time
            .... try:
            .... _time += s
            .... except NameError:
            .... import time
            .... _time = time.time() + s

            Comment

            • Jeff Epler

              #7
              Re: time.time()

              bart,

              a-b/c evaluates as a-(b/c)

              Jeff

              Comment

              • Bart Nessux

                #8
                Re: time.time()

                Jeff Epler wrote:[color=blue]
                > bart,
                >
                > a-b/c evaluates as a-(b/c)
                >
                > Jeff
                >[/color]

                Ah, yes... silly me. That's the problem.

                Comment

                • Terry Carroll

                  #9
                  Re: time.time()

                  On Sat, 24 Jan 2004 16:25:01 -0500, Bart Nessux <bart_nessux@ho tmail.com>
                  wrote:
                  [color=blue]
                  >Terry Carroll wrote:
                  >[color=green]
                  >> It sounds like you're thinking time.time() does something else, which
                  >> you're trying to do. What is that? There might be another function for
                  >> you.[/color]
                  >
                  >I should have been more clear... here's the code:
                  >
                  >x = time.time()
                  >fp = os.popen("some_ process", "r")
                  >fp.read()
                  >fp.close()
                  >print (time.time()-x/60)/60[/color]

                  Ah. You have a precedence problem.

                  Try:

                  print ((time.time()-x)/60)/60


                  Comment

                  Working...