Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
datetime unexpected result
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 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.

import time
import datetime

currenthour = time.strftime("%H")
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.

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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected result linton 4 680 May-02-2020, 01:15 PM
Last Post: linton
  list sum gives unexpected result Nesso 0 538 Feb-04-2020, 08:31 AM
Last Post: Nesso
  Unexpected (?) result with regular expressions guraknugen 2 693 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Confusion with datetime 'dst' function result jsmith1703 4 1,240 Nov-16-2019, 02:55 AM
Last Post: DeaD_EyE
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 3,175 Sep-18-2019, 08:32 AM
Last Post: buran
  Unexpected expected type error result MartinMaker 1 1,010 Feb-16-2019, 05:02 PM
Last Post: micseydel
  unexpected sub result after overloading operator jolinchewjb 1 1,028 Jan-24-2019, 08:23 AM
Last Post: buran
  Erratic Datetime result timsch 0 959 Dec-27-2018, 01:49 AM
Last Post: timsch
  Unexpected result eftimios 1 1,272 Dec-02-2018, 07:39 AM
Last Post: Gribouillis
  SQLAlchemy DateTime result 0's AMarotta97 0 1,161 Oct-01-2018, 04:35 AM
Last Post: AMarotta97

Forum Jump:

User Panel Messages

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