time python library code example

Example 1: python clock

# Here is a self updating clock function, just run it and it'll self update itself until you hit CTRL-C
import datetime
import time
def clock():
    while True:
        print(datetime.datetime.now().strftime("%H:%M:%S"), end="\r")
        time.sleep(1)

clock()

Example 2: python time library

#also see datetime
import time
now = time.time()
print(now)

Example 3: time.time()

from time import time

# 1 January 1970 is the standard date that all computers measure time against 
# Prints the number of seconds that have passed since 1 January 1970
print(time())

Example 4: time a function python

Proper answer to timing a loop over a function multiple times
import timeit
timeit.timeit('func_to_time()',globals=globals(),number=1000)

Example 5: time python

#import time module:
import time
#module has various attributes: 
dir(time)
[..., 'localtime', 'mktime', 'sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us', 'time']

#default expression is in seconds with zero being start of runtime
secFromStart = time.time()

Example 6: Python Time Module

>>> strptime('Fri Mar 01 23:38:40 2019')
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=1, tm_hour=23, tm_min=38, tm_sec=40, tm_wday=4, tm_yday=60, tm_isdst=-1)