Time leaving with unexpected result

Asked

Viewed 51 times

2

I’m running a code that prints the hours on the terminal but when I use the code below it brings me a totally wrong value on time:

Code:

from time import gmtime, strftime
print(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))

Follows the value it returns:

Resultado com 3 horas de diferença

  • And how can we know what is wrong? For me it is right. By chance this gives the result you expect? print(strftime("%a, %d %b %Y %H:%M:%S +0000", localtime()))

  • Yes, so it was without any anomaly, would you happen to know where could be the error in my code? 'Cause I followed exactly the code of your documentation

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

1

You want to print the local time and the correct function for this is the localtime(), and not the gmtime() which gives you the universal call time (no time zone).

from time import localtime, strftime
print(strftime("%a, %d %b %Y %H:%M:%S +0000", localtime()))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Just be careful because in many cases it is right to work with universal time and not local. We even see a lot of questions here from people who are having a hard time getting started on systems wrong and then can’t get any more.

Conceptually there is already a mistake in this +0000 which gives misleading information, and even a +0300 will only be right by coincidence at certain times of the year. But the subject is absurdly extensive to put here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.