Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time function does not work
#1
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!
Reply
#2
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())
tester_V likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you buran!
I do not know why I did not see it myself...
Thank you for the coaching!
Reply
#4
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
tester_V likes this post
Reply
#5
Tanks snippsat!
I have never about "oguru an dlogger", it might be very useful too..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  return next item each time a function is executed User3000 19 2,176 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  I dont know why my function won't work? MehHz2526 3 1,150 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  write new function or change the old one to work "smartter? korenron 3 1,926 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  string function doesn't work in script ClockPillow 3 2,333 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,131 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Can you end the Time.sleep function boier96 9 9,222 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Why does unpickling only work ouside of a function? pjfarley3 5 3,367 Dec-24-2020, 08:31 AM
Last Post: pjfarley3
  function call at defined system time? Holon 5 3,159 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,450 Aug-15-2020, 08:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020