Skip to content

Conversation

@lapointexavier
Copy link
Contributor

Description

I just found an edge case (I believe) that was not handled properly. It seems like the slice on the TZ environment variable value was incorrect, using name[:-1] instead of name[1:].

I would end up with the following:

(Pdb++) name
 ':/etc/localtim'

... instead of:

/etc/localtime

Background

The default timezone value can be TZ=:/etc/localtime. It may vary, but usually starts with :.

Documentation: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html

If the TZ environment variable does not have a value, the operation chooses a time zone by default. In the GNU C Library, the default time zone is like the specification ‘TZ=:/etc/localtime’ (or ‘TZ=:/usr/local/etc/localtime’, depending on how the GNU C Library was configured; see Installation). Other C libraries use their own rule for choosing the default time zone, so there is little we can say about them.

@pganssle
Copy link
Member

Hmmm... This is an untested branch and seems obviously right, but I'm not sure how this has never come up before.

@lapointexavier Did this come up for you "in the wild", or did you figure it out from the docs?

CI failure is fixed in #602, when that is merged you can rebase against master to get a passing CI run.

@lapointexavier
Copy link
Contributor Author

lapointexavier commented Dec 27, 2017

It actually came out since we switched our dev environment to kubernetes. I imagine something's different in the environment as we were using vagrant before.

This block of code started failing and I had to drop in a debugger to see what was going on.

(code edited for simplicity)

LOCAL_TZ = dateutil.tz.gettz()

dt = dateutil.parser.parse('<some datetime>')
dt = dt.astimezone(LOCAL_TZ).replace(tzinfo=None)

dateutil.tz.gettz() would return None, and realized that the environment variable was the following:

echo $TZ
:/etc/localtime

@pganssle
Copy link
Member

Interesting, seems like just no one was hitting that branch before, which is interesting in itself.

For your current situation, here's a workaround:

LOCAL_TZ = dateutil.tz.tzlocal()

dt = dateutil.parser.parse('<some datetime>')
dt = dt.astimezone(LOCAL_TZ).replace(tzinfo=None)

Since you throw away the time zone immediately anyway, it doesn't really matter if you get a tzfile or tz.tzlocal.

@pganssle
Copy link
Member

@lapointexavier I merged #602, so if you want to rebase against master that will fix CI, then I can merge this one.

@lapointexavier
Copy link
Contributor Author

ah great, thanks!

@pganssle pganssle added this to the 2.7.0 milestone Dec 27, 2017
@lapointexavier
Copy link
Contributor Author

@pganssle Should be good to go 🚀

@pganssle
Copy link
Member

@lapointexavier Oh, one other thing (probably should add this to the PR template when I get a chance), can you add yourself to the AUTHORS.md file? Goes in alphabetical order by first name. Put a D next to your name to indicate that your contributions have always been dual-licensed Apache/BSD. You can use any name you want, all fields are optional.

@jbrockmendel
Copy link
Contributor

Nice catch.

@pganssle pganssle merged commit ea06a73 into dateutil:master Dec 27, 2017
@pganssle
Copy link
Member

@lapointexavier By the way, I masked those e-mail address domains because I hadn't asked permission from those authors to actually publish their e-mail address. If you want your e-mail address included, I can include it next time I make a change to AUTHORS.MD. I assume you realize this and didn't want your e-mail included, but I thought I'd make that clear.

@lapointexavier
Copy link
Contributor Author

@pganssle yeah I was a little ambivalent. I guess it wouldn't hurt anyway. Here it is: lapointe.xavier@protonmail.com

@lapointexavier lapointexavier deleted the fix_timezone_detection branch January 3, 2018 04:09
@pganssle pganssle mentioned this pull request Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants