Python Forum

Full Version: time function does not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings!

I'm trying to use 'functions in my code.
I want to add a timestamp for each different operation my code executes.
I made a function and I thought each time I'll call it I'll get a different timestamp.
For some reason, it prints the same timestamp.
import time

def get_time():
    import time
    t_now = time.strftime("%Y-%m-%d %H:%M:%S")
    t_now=str(t_now)
    return t_now
    print(t_now)
tst =  get_time()

print (tst)
time.sleep(5)
print (tst)
Thank you for your help!
There are multiple issues with your code.

On line #9 tst = get_time() you want to make tst alias for get_time (or at least that is what it seems you think). However what is actually going on you call get_time and bind the value returned to name tst. Now tst value is str. What you should do is tst = get_time. In addition, even if you were successful in creating alias, you never call it, i.e. on line 11 and 13 you want tst(), not just tst. If creating alias is necessary is completely different matter - I don't see any benefit in it, just use/call get_time when you need.

You import time on line 1. No need to have second import time inside the function.

time.strftime() will return str, so line 6 is redundant.

The return statement on line 7 will end the function execution and well, return the value t_now. Line #8 will never be reached and executed.

import time
 
def get_time():
    return time.strftime("%Y-%m-%d %H:%M:%S")

tst = get_time # I wouldn't do that, call get_time() when needed
print(tst())
time.sleep(5)
print(tst())
Thank you buran!
I do not know why I did not see it myself...
Thank you for the coaching!
Also a alternative advice can be to use logging,and then use loguru
Then it can be done as simple as this
from loguru import logger
import time

logger.info('start')
time.sleep(5)
logger.info('stop')
Output:
2021-10-16 16:50:13.532 | INFO | __main__:<module>:4 - start 2021-10-16 16:50:18.536 | INFO | __main__:<module>:6 - stop
So timestamp is there automatic.
It's also easy to add functionality.
from loguru import logger
import time
logger.remove() # Only info to file
logger.add("file.log", rotation="2 day")

logger.info('start')
@logger.catch
def foo():
    time.sleep(5)
    return 1 / 0

foo()
logger.info('stop')
file.log:
Output:
2021-10-16 17:00:59.135 | INFO | __main__:<module>:6 - start 2021-10-16 17:01:04.142 | ERROR | __main__:<module>:12 - An error has been caught in function '<module>', process 'MainProcess' (23936), thread 'MainThread' (23940): Traceback (most recent call last): > File "G:\div_code\answer\log_time2.py", line 12, in <module> foo() -> <function foo at 0x00000288F302F790> File "G:\div_code\answer\log_time2.py", line 10, in foo return 1 / 0 ZeroDivisionError: division by zero 2021-10-16 17:01:04.147 | INFO | __main__:<module>:13 - stop
Tanks snippsat!
I have never about "oguru an dlogger", it might be very useful too..