py-sherlock / sherlock

Easy distributed locks for Python with a choice of backends.
MIT License
374 stars 35 forks source link

`FileLock._expiry_time` crash on a different timezone than UTC #71

Open bcebere opened 1 year ago

bcebere commented 1 year ago

Hello,

Thank you for the awesome library.

I am encountering an issue with the FileLock when trying to use it in a timezone different than UTC.

The _expiry_time call throws the exception: ValueError: year 10000 is out of range

when computing datetime.datetime.max.astimezone(datetime.timezone.utc)

As far as I understand, internally, datetime does this:

timestamp() method actually converts your datetime in local timezone. If your timezone is UTC+x (not UTC-x); i.e. your timezone is ahead of UTC time; Then it will add more time in your datetime object (datetime.datetime.max), which will cross the time beyond the year 9999. 

But this is a weird behavior in datetime. Anyway, is it possible to have a way to use sherlock on timezones different than UTC(UTC + x)?

I can work on this, if it looks like a valid problem. One possible fix is to use

expiry_time = datetime.datetime.max.replace(tzinfo=datetime.timezone.utc).astimezone(datetime.timezone.utc)

Python 3.10.9 OS: LinuxMint 21.1

vaidik commented 1 year ago

Hi @bcebere

Glad to hear that you find sherlock useful.

I can confirm this issue as I am able to reproduce this for a UTC+ timezone. Be back shortly with a fix.

james-tisato commented 1 year ago

Is there a plan for when this might be merged? This is blocking my use of the library entirely.