1

The TZ env variable in cygwin contains local timezone info

$ echo $TZ 
America/New_York

The datetime module prints UTC if TZ is set

  $ /usr/local/anaconda3/python
    Python 3.10.9 | packaged by Anaconda, Inc. | (main, Mar  1 2023, 18:18:15) [MSC
    v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> print(datetime.datetime.now())
    2023-07-02 20:49:23.692920
    >>> quit()
  $

and local time when TZ is not set

  $ TZ= /usr/local/anaconda3/python
    Python 3.10.9 | packaged by Anaconda, Inc. | (main, Mar  1 2023, 18:18:15) [MSC
    v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> print(datetime.datetime.now())
    2023-07-02 15:53:31.520504
    >>> quit()
  $

The tzlocal module prints local time zone if TZ is not set

  $ TZ= /usr/local/anaconda3/python
  Python 3.10.9 | packaged by Anaconda, Inc. | (main, Mar  1 2023, 18:18:15) [MSC v.1916 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import tzlocal
  >>> tzlocal.get_localzone().zone
  'America/New_York'
  >>> quit()
  $

and throws error when TZ is set

  $ /usr/local/anaconda3/python
  Python 3.10.9 | packaged by Anaconda, Inc. | (main, Mar  1 2023, 18:18:15) [MSC v.1916 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import tzlocal
  >>> tzlocal.get_localzone().zone
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\cygwin64\usr\local\anaconda3\lib\site-packages\tzlocal\win32.py", line 95, in get_localzone
      utils.assert_tz_offset(_cache_tz)
    File "C:\cygwin64\usr\local\anaconda3\lib\site-packages\tzlocal\utils.py", line 46, in assert_tz_offset
      raise ValueError(msg)
  ValueError: Timezone offset does not match system offset: -14400 != 3600. Please, check your config files.
  >>> quit()
  $

When $TZ is set, tzlocal module works as expected but datetime module does not work as expected.

When $TZ is not set, tzlocal module does not work as expected but datetime module works as expected.

My thinking is that both modules should work as expected when $TZ is set.

Is this a problem with the datetime module in cygwin?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
user3477071
  • 194
  • 1
  • 13
  • what is the tz of your Windows machine that runs the cygwin environment? – FObersteiner Jul 03 '23 at 06:16
  • Windows 10 Date & Time Settings show a Time zone of '(UTC-05:00) Eastern Time (US & Canada)' which is consistent with Cygwin's TZ of 'America/New_York' – user3477071 Jul 03 '23 at 08:39
  • Ok, are you sure you're using cygwin's Python? in your samples, it still says "on win32", which should be "on cygwin". Related: https://stackoverflow.com/q/63713891/10197418, https://stackoverflow.com/q/11655003/10197418 – FObersteiner Jul 03 '23 at 09:00
  • 1
    I am not using Cygwin's python, which does not have any of above issues. Instead I am using Conda's python for windows from cygwin bash. Thanks for the link you provided which explains the reason why I should unset TZ env variable when running Conda Python from cygwin shell. – user3477071 Jul 03 '23 at 23:55
  • So, if the questions I linked provide an answer to your questions, we could close it as a duplicate. However, I'd suggest not to delete it, for other people to find the relevant info. – FObersteiner Jul 04 '23 at 05:53
  • 1
    sure this can be closed as a duplicate – user3477071 Jul 04 '23 at 10:36

0 Answers0