regebro / tzlocal

A Python module that tries to figure out what your local timezone is

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not seeing Daylight Saving Time info, nor any debugging info

bjkeefe opened this issue · comments

Caveat: when I pip installed tzlocal a few days ago, this is what I got:

Successfully installed pytz-deprecation-shim-0.1.0.post0 tzdata-2023.3 tzlocal-4.3

Perhaps all of what follows is due to my having v4.3, and not v5.something. If so, please advise. TIA!

Proceeding with the issue report ...

Following the readme, I ran the following:

> $ py
> Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import logging
> >>> logging.basicConfig(level="DEBUG")
> >>> import tzlocal
> >>> tzlocal.get_localzone()
> _PytzShimTimezone(zoneinfo.ZoneInfo(key='America/New_York'), 'America/New_York')
> >>>
> 

The readme indicates I should have seen something like DST (or STD, for a different time of year) at the end of that.

Also note the difference in appearance of the result of the call to tzlocal.get_localzone(), compared to what is shown in the readme; e.g., <DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD>.

Also note the absence of any debugging in the REPL session pasted above.

Ah, the readme is outdated, <DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD> is a pytz object. zoneinfo.ZoneInfo(key='America/New_York') is the correct output for later versions.

OK, fixed that, thanks for the headsup!

Thanks! And, you're welcome: glad I could provide a tiny bit of help.

If it's not too much of a pain, could you also tell me:

  1. whether or not Daylight Saving Time info is available via tzlocal?
  2. why I don't see any debugging output when I do what is shown in the readme?

Again, TIA.

Daylight saving info is available in several different ways, depending on what you want. None of it is directly via tzlocal. Usually the best way is to get it from the time module.

You won't see the debug output if you don't turn it on the first time you get the local zone, because subsequent times it will be cached.

First time:

>>> import logging
>>> logging.basicConfig(level="DEBUG")
>>> import tzlocal
>>> tzlocal.get_localzone()
DEBUG:root:/etc/timezone found, contents:
 Europe/Warsaw

DEBUG:root:/etc/localtime found
DEBUG:root:2 found:
 {'/etc/timezone': 'Europe/Warsaw', '/etc/localtime is a symlink to': 'Europe/Warsaw'}
zoneinfo.ZoneInfo(key='Europe/Warsaw')

Second time:

>>> tzlocal.get_localzone()
zoneinfo.ZoneInfo(key='Europe/Warsaw')

I just released 5.0, which is what I was effectively using when testing this, so if you still have problems, installing 5.0 might help, there's been a few changes.

Just upgraded to tzlocal-5.0, and that did the trick, as far as debugging goes. My output is slightly different from yours:

>>> import logging
>>> logging.basicConfig(level="DEBUG")
>>> import tzlocal
>>> tzlocal.get_localzone()
DEBUG:root:Looking up time zone info from registry
DEBUG:root:Found a TZ environment: America/New_York
DEBUG:root:Found a TZ environment: America/New_York
zoneinfo.ZoneInfo(key='America/New_York')
>>> tzlocal.get_localzone()
DEBUG:root:Found a TZ environment: America/New_York
zoneinfo.ZoneInfo(key='America/New_York')

As for Daylight Saving Time, I will take a look at your time suggestion.

[added] For anyone else reading along, and wondering, both of these seem to do the trick:

>>> import time
>>> time.localtime().tm_isdst
1
>>> time.daylight
1

Thanks for your help, and for your timely responses. I would consider this issue closed, at least from my perspective.

Great!