a ``HK@sdZddlmZmZmZddlmZzeWneyJddlm ZYn0ddl Z ddl m Z m Z gZiZddZedZdeiZd d ZiZd d ZedZd dZGdddeZGdddeZGdddeZdddZdS)zBBase classes and helpers for building zone specific tzinfo classes)datetime timedeltatzinfo) bisect_right)SetN)AmbiguousTimeErrorNonExistentTimeErrorcCs8z t|WSty2t|d}|t|<|YS0dS)z3Create only one instance of each distinct timedeltasecondsN)_timedelta_cacheKeyErrorr)r deltar//usr/lib/python3.9/site-packages/pytz/tzinfo.pymemorized_timedeltas    rcCs<z t|WSty6tt|d}|t|<|YS0dS)z2Create only one instance of each distinct datetimer N)_datetime_cacher _epochr)r dtrrrmemorized_datetimes   rcGsLz t|WStyFt|dt|d|df}|t|<|YS0dS)z/Create only one instance of each distinct tuplerN) _ttinfo_cacher r)argsZttinforrrmemorized_ttinfo-s    rcCs|j|jdddS)zConvert a timedelta to seconds<)r days)Ztdrrr _to_seconds=src@s eZdZdZdZdZddZdS) BaseTzInfoNcCs|jSNzoneselfrrr__str__HszBaseTzInfo.__str__)__name__ __module__ __qualname__ _utcoffset_tznamer!r$rrrrrBsrc@sZeZdZdZddZdddZdddZdd d Zdd d ZdddZ ddZ ddZ dS) StaticTzInfozA timezone that has a constant offset from UTC These timezones are rare, as most locations have changed their offset at some point in their history cCs.|jdur|j|urtd||jj|dS)See datetime.tzinfo.fromutcNfromutc: dt.tzinfo is not selfr)r ValueErrorr(replace)r#rrrrfromutcRszStaticTzInfo.fromutcNcCs|jS)zSee datetime.tzinfo.utcoffset is_dst is ignored for StaticTzInfo, and exists only to retain compatibility with DstTzInfo. )r(r#ris_dstrrr utcoffsetXszStaticTzInfo.utcoffsetcCstS)zSee datetime.tzinfo.dst is_dst is ignored for StaticTzInfo, and exists only to retain compatibility with DstTzInfo. )_notimer1rrrdst`szStaticTzInfo.dstcCs|jS)zSee datetime.tzinfo.tzname is_dst is ignored for StaticTzInfo, and exists only to retain compatibility with DstTzInfo. )r)r1rrrtznamehszStaticTzInfo.tznameFcCs|jdurtd|j|dS)z Convert naive time to local timeN*Not naive datetime (tzinfo is already set)r-)rr.r/r1rrrlocalizeps zStaticTzInfo.localizecCs*|j|ur|S|jdur td||S)aCorrect the timezone information on the given datetime. This is normally a no-op, as StaticTzInfo timezones never have ambiguous cases to correct: >>> from pytz import timezone >>> gmt = timezone('GMT') >>> isinstance(gmt, StaticTzInfo) True >>> dt = datetime(2011, 5, 8, 1, 2, 3, tzinfo=gmt) >>> gmt.normalize(dt) is dt True The supported method of converting between timezones is to use datetime.astimezone(). Currently normalize() also works: >>> la = timezone('America/Los_Angeles') >>> dt = la.localize(datetime(2011, 5, 7, 1, 2, 3)) >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' >>> gmt.normalize(dt).strftime(fmt) '2011-05-07 08:02:03 GMT (+0000)' NNaive time - no tzinfo set)rr.Z astimezoner1rrr normalizevs   zStaticTzInfo.normalizecCs d|jfS)Nzr r"rrr__repr__szStaticTzInfo.__repr__cCstj|jffSr)pytz_pr!r"rrr __reduce__szStaticTzInfo.__reduce__)N)N)N)F)F) r%r&r'__doc__r0r3r5r6r8r:r;r>rrrrr*Ls     r*c@sveZdZdZdZdZdZdZdZdddZ ddZ ddZ dd d Z dd d Z dddZdddZddZddZdS) DstTzInfozA timezone that has a variable offset from UTC The offset might change if daylight saving time comes into effect, or at a point in history when the region decides to change their timezone definition. NcCsz|r||_|\|_|_|_nZi}||_|jd\|_|_|_|||jd<|jddD]}||vrX|||||<qXdS)Nrr)_tzinfosr(_dstr)_transition_info __class__)r#Z_infrAinfrrr__init__szDstTzInfo.__init__cCsl|jdur&t|jdd|jur&td|jdd}tdt|j|d}|j|}||dj|j|dS)r+NrAr,r-rr) rgetattrrAr.r/maxr_utc_transition_timesrC)r#ridxrErrrr0s   zDstTzInfo.fromutccCs8|jdurtd|jj}|jdd}||}||S)aICorrect the timezone information on the given datetime If date arithmetic crosses DST boundaries, the tzinfo is not magically adjusted. This method normalizes the tzinfo to the correct one. To test, first we need to do some setup >>> from pytz import timezone >>> utc = timezone('UTC') >>> eastern = timezone('US/Eastern') >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' We next create a datetime right on an end-of-DST transition point, the instant when the wallclocks are wound back one hour. >>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc) >>> loc_dt = utc_dt.astimezone(eastern) >>> loc_dt.strftime(fmt) '2002-10-27 01:00:00 EST (-0500)' Now, if we subtract a few minutes from it, note that the timezone information has not changed. >>> before = loc_dt - timedelta(minutes=10) >>> before.strftime(fmt) '2002-10-27 00:50:00 EST (-0500)' But we can fix that by calling the normalize method >>> before = eastern.normalize(before) >>> before.strftime(fmt) '2002-10-27 01:50:00 EDT (-0400)' The supported method of converting between timezones is to use datetime.astimezone(). Currently, normalize() also works: >>> th = timezone('Asia/Bangkok') >>> am = timezone('Europe/Amsterdam') >>> dt = th.localize(datetime(2011, 5, 7, 1, 2, 3)) >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' >>> am.normalize(dt).strftime(fmt) '2011-05-06 20:02:03 CEST (+0200)' Nr9r-)rr.r(r/r0)r#roffsetrrrr:s -  zDstTzInfo.normalizeFc s|jdurtdt}tddtddfD]b}||}tdt|j|d}|j|}|j|}| |j |d}|j dd|kr,| |q,t |dkr| St |dkr durt|nHr|j|tdd d d tdd S|j|tdd d d tdd Sdurt|fd d|D} t | dkrF| dSt | dkr\t|} i} | D]"} | j dd| jj} | | | <qd| ttg | S)aConvert naive time to local time. This method should be used to construct localtimes, rather than passing a tzinfo argument to a datetime constructor. is_dst is used to determine the correct timezone in the ambigous period at the end of daylight saving time. >>> from pytz import timezone >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' >>> amdam = timezone('Europe/Amsterdam') >>> dt = datetime(2004, 10, 31, 2, 0, 0) >>> loc_dt1 = amdam.localize(dt, is_dst=True) >>> loc_dt2 = amdam.localize(dt, is_dst=False) >>> loc_dt1.strftime(fmt) '2004-10-31 02:00:00 CEST (+0200)' >>> loc_dt2.strftime(fmt) '2004-10-31 02:00:00 CET (+0100)' >>> str(loc_dt2 - loc_dt1) '1:00:00' Use is_dst=None to raise an AmbiguousTimeError for ambiguous times at the end of daylight saving time >>> try: ... loc_dt1 = amdam.localize(dt, is_dst=None) ... except AmbiguousTimeError: ... print('Ambiguous') Ambiguous is_dst defaults to False >>> amdam.localize(dt) == amdam.localize(dt, False) True is_dst is also used to determine the correct timezone in the wallclock times jumped over at the start of daylight saving time. >>> pacific = timezone('US/Pacific') >>> dt = datetime(2008, 3, 9, 2, 0, 0) >>> ploc_dt1 = pacific.localize(dt, is_dst=True) >>> ploc_dt2 = pacific.localize(dt, is_dst=False) >>> ploc_dt1.strftime(fmt) '2008-03-09 02:00:00 PDT (-0700)' >>> ploc_dt2.strftime(fmt) '2008-03-09 02:00:00 PST (-0800)' >>> str(ploc_dt2 - ploc_dt1) '1:00:00' Use is_dst=None to raise a NonExistentTimeError for these skipped times. >>> try: ... loc_dt1 = pacific.localize(dt, is_dst=None) ... except NonExistentTimeError: ... print('Non-existent') Non-existent Nr7)rrrr-)ZhoursTr2Fcs g|]}t|jjkr|qSr)boolrrB).0prNrr osz&DstTzInfo.localize..)rr.setrrHrrIrCrAr:r/addlenpoprr8rlistr(min) r#rr2Zpossible_loc_dtr Zloc_dtrJrErZfiltered_possible_loc_dtZdatesZlocal_dtZutc_timerrNrr8s^;           zDstTzInfo.localizecCs4|dur dS|j|ur*|||}|jjS|jSdS)aLSee datetime.tzinfo.utcoffset The is_dst parameter may be used to remove ambiguity during DST transitions. >>> from pytz import timezone >>> tz = timezone('America/St_Johns') >>> ambiguous = datetime(2009, 10, 31, 23, 30) >>> str(tz.utcoffset(ambiguous, is_dst=False)) '-1 day, 20:30:00' >>> str(tz.utcoffset(ambiguous, is_dst=True)) '-1 day, 21:30:00' >>> try: ... tz.utcoffset(ambiguous) ... except AmbiguousTimeError: ... print('Ambiguous') Ambiguous N)rr8r(r1rrrr3s   zDstTzInfo.utcoffsetcCs4|dur dS|j|ur*|||}|jjS|jSdS)aSee datetime.tzinfo.dst The is_dst parameter may be used to remove ambiguity during DST transitions. >>> from pytz import timezone >>> tz = timezone('America/St_Johns') >>> normal = datetime(2009, 9, 1) >>> str(tz.dst(normal)) '1:00:00' >>> str(tz.dst(normal, is_dst=False)) '1:00:00' >>> str(tz.dst(normal, is_dst=True)) '1:00:00' >>> ambiguous = datetime(2009, 10, 31, 23, 30) >>> str(tz.dst(ambiguous, is_dst=False)) '0:00:00' >>> str(tz.dst(ambiguous, is_dst=True)) '1:00:00' >>> try: ... tz.dst(ambiguous) ... except AmbiguousTimeError: ... print('Ambiguous') Ambiguous N)rr8rBr1rrrr5s   z DstTzInfo.dstcCs6|dur|jS|j|ur,|||}|jjS|jSdS)aSee datetime.tzinfo.tzname The is_dst parameter may be used to remove ambiguity during DST transitions. >>> from pytz import timezone >>> tz = timezone('America/St_Johns') >>> normal = datetime(2009, 9, 1) >>> tz.tzname(normal) 'NDT' >>> tz.tzname(normal, is_dst=False) 'NDT' >>> tz.tzname(normal, is_dst=True) 'NDT' >>> ambiguous = datetime(2009, 10, 31, 23, 30) >>> tz.tzname(ambiguous, is_dst=False) 'NST' >>> tz.tzname(ambiguous, is_dst=True) 'NDT' >>> try: ... tz.tzname(ambiguous) ... except AmbiguousTimeError: ... print('Ambiguous') Ambiguous N)r!rr8r)r1rrrr6s   zDstTzInfo.tznamecCsJ|jr d}nd}|jtkr0d|j|j|j|fSd|j|j|j|fSdS)NZDSTZSTDzz)rBr(r4r!r))r#r5rrrr;s zDstTzInfo.__repr__cCs"tj|jt|jt|j|jffSr)r<r=r!rr(rBr)r"rrrr>s zDstTzInfo.__reduce__)NN)F)N)N)N)r%r&r'r?rIrCr!rArBrFr0r:r8r3r5r6r;r>rrrrr@s    7   ' &r@cCst|}|dur|St|}t|}z|j|||fWStyJYn0|jD] }|j|krV|j|krV|SqV|||f}|||j|j|<|j|S)a%Factory function for unpickling pytz tzinfo instances. This is shared for both StaticTzInfo and DstTzInfo instances, because database changes could cause a zones implementation to switch between these two base classes and we can't break pickles on a pytz version upgrade. N) r<timezonerrAr valuesr(rBrD)r!r3Z dstoffsetr6ZtzZ localized_tzrErrr unpicklers"     r[)NNN)r?rrrZbisectrrS NameErrorZsetsrr<Zpytz.exceptionsrr__all__r rZutcfromtimestamprrrrrr4rrr*r@r[rrrrs0      Pw