Getting time from the system

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    Getting time from the system

    Hi,

    Is there any functions available for finding the time from the system?.I would like to find the start time and end time of the processing of the application after execution.

    Thanks in advance
    PSB
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by psbasha
    Hi,

    Is there any functions available for finding the time from the system?.I would like to find the start time and end time of the processing of the application after execution.

    Thanks in advance
    PSB
    check out the time module. you should really spend some time here , especially the library reference

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by psbasha
      Hi,

      Is there any functions available for finding the time from the system?.I would like to find the start time and end time of the processing of the application after execution.

      Thanks in advance
      PSB
      From the time module:
      >>> import time
      >>> time.localtime( time.time())
      (2007, 3, 6, 22, 8, 18, 1, 65, 0)
      >>> time.strftime(' %H:%M:%S', time.localtime( time.time()))
      '22:08:50'
      >>>

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        looking at the OP's post again, i think he needs the timeit module.. :)

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by psbasha
          Hi,

          Is there any functions available for finding the time from the system?.I would like to find the start time and end time of the processing of the application after execution.

          Thanks in advance
          PSB
          Good catch GD; thanks for keeping an eye on me.
          This may be more what PSB is looking for:
          Code:
          from time import time, sleep
          startTime = time()
          for i in range(5):
              sleep(.5)
          print "Elapsed time is %.1f" % (time() - startTime)

          Comment

          • psbasha
            Contributor
            • Feb 2007
            • 440

            #6
            Thanks for the reply.

            This is what I am looking for.

            Regards
            PSB

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #7
              Is there any method available ,which gives the time in this format HH:MM:SS

              -PSB

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by psbasha
                Is there any method available ,which gives the time in this format HH:MM:SS

                -PSB
                See reply #3. for strftime() usage.

                Comment

                • psbasha
                  Contributor
                  • Feb 2007
                  • 440

                  #9
                  Originally posted by bartonc
                  See reply #3. for strftime() usage.
                  I would like to find the start and end time difference in this format
                  HH:MM:SS

                  -PSB

                  Comment

                  • ghostdog74
                    Recognized Expert Contributor
                    • Apr 2006
                    • 511

                    #10
                    Originally posted by psbasha
                    I would like to find the start and end time difference in this format
                    HH:MM:SS

                    -PSB
                    show some stuff you have done with strftime(). barton have already directed you where to look for examples.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by psbasha
                      I would like to find the start and end time difference in this format
                      HH:MM:SS

                      -PSB
                      The datetime module is what you're looking for:
                      Code:
                      >>> import datetime
                      >>> startTime = time.time()
                      >>> elapsedTime = time.time() - startTime
                      >>> t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                      >>> t.isoformat()
                      '00:00:31'
                      >>>
                      Output: '00:00:31.00061 5'
                      Use datetime.dateti me for periods greater than 24 hours.

                      Comment

                      • psbasha
                        Contributor
                        • Feb 2007
                        • 440

                        #12
                        Originally posted by bartonc
                        The datetime module is what you're looking for:
                        Code:
                        >>> import datetime
                        >>> startTime = time.time()
                        >>> elapsedTime = time.time() - startTime
                        >>> t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                        >>> t.isoformat()
                        '00:00:31'
                        >>>
                        Output: '00:00:31.00061 5'
                        Use datetime.dateti me for periods greater than 24 hours.
                        Code:
                        sample.py
                        from time import time, sleep
                        import datetime
                        
                        def process_time1():
                            startTime = time.time()
                            
                            for i in range(0,1000):
                                sleep(i)
                                
                            elapsedTime = time.time() - startTime
                            t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                            print t.isoformat() 
                        
                        if __name__ == '__main__':
                        
                            process_time1()
                        ---------------------------------------------------------------------------------
                        Traceback (most recent call last):
                        File "C:/Project/Sampletime.py", line 16, in -toplevel-
                        process_time1()
                        File "C://Project/Sampletime.py", line 5, in process_time1
                        startTime = time.time()
                        AttributeError: 'builtin_functi on_or_method' object has no attribute 'time'
                        >>>
                        ---------------------------------------------------------------------------------
                        getting the above error.

                        Comment

                        • ghostdog74
                          Recognized Expert Contributor
                          • Apr 2006
                          • 511

                          #13
                          Originally posted by psbasha
                          Code:
                          sample.py
                          from time import time, sleep
                          import datetime
                          
                          def process_time1():
                              startTime = time.time()
                              
                              for i in range(0,1000):
                                  sleep(i)
                                  
                              elapsedTime = time.time() - startTime
                              t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                              print t.isoformat() 
                          
                          if __name__ == '__main__':
                          
                              process_time1()
                          ---------------------------------------------------------------------------------
                          Traceback (most recent call last):
                          File "C:/Project/Sampletime.py", line 16, in -toplevel-
                          process_time1()
                          File "C://Project/Sampletime.py", line 5, in process_time1
                          startTime = time.time()
                          AttributeError: 'builtin_functi on_or_method' object has no attribute 'time'
                          >>>
                          ---------------------------------------------------------------------------------
                          getting the above error.
                          Hint:
                          you have "from time import time" from the begining of your code.

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by psbasha
                            Code:
                            sample.py
                            from time import time, sleep
                            import datetime
                            
                            def process_time1():
                                startTime = time.time()
                                
                                for i in range(0,1000):
                                    sleep(i)
                                    
                                elapsedTime = time.time() - startTime
                                t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                                print t.isoformat() 
                            
                            if __name__ == '__main__':
                            
                                process_time1()
                            ---------------------------------------------------------------------------------
                            Traceback (most recent call last):
                            File "C:/Project/Sampletime.py", line 16, in -toplevel-
                            process_time1()
                            File "C://Project/Sampletime.py", line 5, in process_time1
                            startTime = time.time()
                            AttributeError: 'builtin_functi on_or_method' object has no attribute 'time'
                            >>>
                            ---------------------------------------------------------------------------------
                            getting the above error.
                            Code:
                            from time import sleep
                            import time
                            import datetime
                            
                            def process_time1():
                                startTime = time.time()
                                
                                for i in range(0,1000):
                                    sleep(i)
                                    
                                elapsedTime = time.time() - startTime
                                t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                                print t.isoformat() 
                            
                            if __name__ == '__main__':
                            
                                process_time1()

                            Comment

                            • psbasha
                              Contributor
                              • Feb 2007
                              • 440

                              #15
                              Originally posted by bartonc
                              Code:
                              from time import sleep
                              import time
                              import datetime
                              
                              def process_time1():
                                  startTime = time.time()
                                  
                                  for i in range(0,1000):
                                      sleep(i)
                                      
                                  elapsedTime = time.time() - startTime
                                  t = datetime.time(0,0,int(elapsedTime), (elapsedTime%1)*1000)
                                  print t.isoformat() 
                              
                              if __name__ == '__main__':
                              
                                  process_time1()
                              Thanks Barton,
                              This is what I am looking for.
                              I need this functionality for finding out ,how much time my application is taking for execution.Based on the more time taken I have to find out which function is taking more time ( similar to profiling).

                              -PSB

                              Comment

                              Working...