Python Forum

Full Version: datetime unexpected result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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
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.

???
Not on my system
Is your clock set properly?

You see the output that I show, which is correct EST (local)
Yes, both machines have the correct time and date.
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?
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
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).
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
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.
Pages: 1 2