4

Can anyone tell me why I'm having problems with Python programs showing the incorrect time (it's giving me +5 hrs).

My computer is set correctly for EST. For example, if I went into cygdrive or cmd or powershell and then open up a python command line and do the following, I get the correct time:

>>> from datetime import datetime
>>> print(datetime.now())
2018-05-15 20:29:28.360810

However, if I try to run that same line of code within a .py file and from cygdrive, cmd, or powershell run the below code, I get the wrong date/time:

---In the 'test.py' file---

from datetime import datetime
print(datetime.now())

python test.py

2018-05-16 01:29:49.636899

Any clues what's going on?

BTW, I'm also getting an error trying to import pytz, however when I try a pip install, I get 'requirement already satisfied' with the filepath showing where it is located. Just an FYI, more than anything.

Thank you!

Will Dutcher
  • 339
  • 1
  • 4
  • 11

2 Answers2

0

Looks like one is running from a windows install and one from within cygwin. This suggests that Cygwin may have a different timezone configured.

Try running date in Cygwin to confirm it matches the incorrect time that python is showing. Then take a look at this answer for issues with setting the timezone in Cygwin

Cygwin shows UTC time instead of local time

Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
0

I'm experiencing the same issue. Python program gives correct time when run in cmd environment. The same python program run under cygwin gives London time. And yet, echo $TZ gives America/Los_Angeles and the command 'date' in cygwin gives Portland time.

Based on another answer, "unset TZ" in the cygwin window fixed the problem.

incircuitous
  • 131
  • 6