Python Forum
datetime unexpected result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: datetime unexpected result (/thread-1348.html)

Pages: 1 2


datetime unexpected result - PickyBiker - Dec-26-2016

I am getting the UTC time from the following in my code and I thought it would bring back the local hour. 
At a Python prompt datetime.datetime.now().hour returns 11, but in the code, it is returning 17.  
At a Linux prompt, "date" returns the correct local date and time.  What am I doing wrong.

datetime.datetime.now().hour



RE: datetime unexpected result - Larz60+ - Dec-26-2016

Try:

import time
import datetime

currenthour = time.strftime("%H")
print(currenthour)
print(datetime.datetime.now().hour)
returns:
Output:
14 14 Process finished with exit code 0



RE: datetime unexpected result - PickyBiker - Dec-26-2016

That works fine in a python prompt, but it still returns UTC time in a program running on that machine.
In case it matters, this program is being developed using pycharm on a windows laptop with remote deployment to a raspberry pi.
The raspberry pi "knows" the correct timezone and a python prompt on that machine works correctly with the examples posted above.

???


RE: datetime unexpected result - Larz60+ - Dec-26-2016

Not on my system
Is your clock set properly?

You see the output that I show, which is correct EST (local)


RE: datetime unexpected result - PickyBiker - Dec-27-2016

Yes, both machines have the correct time and date.


RE: datetime unexpected result - Larz60+ - Dec-27-2016

Ok, you got 17 (or 5 PM), what were you expecting?

and, if you run the code that I posted, verbatim, you still get wrong answer?


RE: datetime unexpected result - PickyBiker - Dec-27-2016

When I originally posted, i was getting 17 instead of the 11 I would expect at 11 AM CST. 

Yes, the code you posted was copied and pasted into the program and it comes up 6 hours off. Again, if I use that code in a python prompt on either machine, it works correctly, it is only a problem inside the code. It's like the program always uses UTC instead of the local time.
İmage

İmage



RE: datetime unexpected result - Larz60+ - Dec-27-2016

I have a raspberry pi here, but it's never been fired up (haven't had a reason to do so yet).
I am more experienced with Linux (or UNIX) than windows, i would say used 80 % vs 20 %
since the early 1980's. I have, however, had my head stuck in windows for the past year
(ending soon I hope). I don't recall any differences in date handling.

I guess I have no answer for you. You're experiencing very strange behavior.

Please post a complete copy of your failing code, imports etc. Include whatever is needed to
reproduce the error (on your system).


RE: datetime unexpected result - wavic - Dec-27-2016

The Python interpreter sets a bunch of environment variables at the start. It's the same for every program. 
Linux box here... No problems with time in both - the interpreter and the script


RE: datetime unexpected result - PickyBiker - Dec-27-2016

The problem has been isolated to the remote deployment from the PC to the RPI.

I copied the program to the RPI and ran it there. The resultant local times are correct. 


The incorrect times are only a problem when I am coding in PYCHARM on the PC and remotely auto-deploying to the  RPI. When I test in that situation, the time is wrong. 

Based on this info, I will look to the PYCHARM forum for assistance.