-1

I am a beginner in programming and I was messing around with the time module in python, as I recently got a raspberry pi and want to start doing some projects. I imported the time module, and just made a loop to print out the time every second. The code runs correctly, but the time given is not accurate to my location. Currently, it is the 14th and a Friday, around 9 pm, but it is returning the 15th a Saturday, with 0 hours and 10 minutes. Does anyone know how I can obtain the correct time?

I tried a couple of the different functions to receive the current time like .localtime() and .gmtime() but they're all the same.


import time 


while(True):

    thisTime = time.gmtime()

    print(time.asctime(thisTime))

    time.sleep(1)

2 Answers2

0

Go ahead and check out this post, I think this is your solution:

Python get current time in right timezone

So assuming your computer's time is configured correctly, you could use this:

from datetime import datetime
naive_dt = datetime.now()
0

It's unclear what else you've tried, although the following should work:

localtime = time.localtime()
print(time.asctime(localtime))

https://docs.python.org/3/library/time.html#time.localtime

l'L'l
  • 44,951
  • 10
  • 95
  • 146