
    "0              
          S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
r
Sr\" \5      rS rS rS rS r\
R$                  r\
R&                  " S	5      r " S
 S\5      r " S S\5      r " S S\5      r " S S\R2                  5      r\" 5       r " S S\R                  5      rSS0rS\S   -  \S'   S\S   -  \S'   S\S   -  \S'   \S   \S'   S\S   -  \S'   \S   \S'   S\S   -  \S '   S!\S   -  \S"'   \R<                  " S#S$R?                  \5      -  5      r S% r! " S& S'\5      r"\"" S(S)S*S+S,S,S-\5      r#S.\-  S-
  r$\"" S/SS0S1S2SS-5      r%g)3zSet of classes and functions for dealing with dates and timestamps.

The BaseTimestamp and Timestamp are timezone-aware wrappers around Python
datetime.datetime class.
    N)parseri@B c                     U [         -  $ )zRConvert seconds to microseconds.

Args:
  seconds: number
Returns:
  microseconds
)_MICROSECONDS_PER_SECONDsecondss    2platform/bq/third_party/google/apputils/datelib.pySecondsToMicrosecondsr	   (   s     
+	++    c                  P    [        [        [        R                  " 5       5      5      $ )zfGet the current time in microseconds, in UTC.

Returns:
  The number of microseconds since the epoch.
)intr	   time r
   r   _GetCurrentTimeMicrosr   3   s     
"499;/	00r
   c                 :    [         R                  " U SS S-   5      $ )z|Convert time_tuple (in UTC) to seconds (also in UTC).

Args:
  time_tuple: tuple with at least 6 items.
Returns:
  seconds.
N   )r   r   r   )calendartimegm
time_tuples    r   GetSecondsSinceEpochr   <   s     
BQ)3	44r
   c                 <    [        [        [        U 5      5      5      $ )zGet a time in microseconds.

Arguments:
  time_tuple: A (year, month, day, hour, minute, second) tuple (the python
    time tuple format) in the UTC time zone.

Returns:
  The number of microseconds since the epoch represented by the input tuple.
)r   r	   r   r   s    r   GetTimeMicrosr   G   s     
"#7
#CD	EEr
   z
US/Pacificc                       \ rS rSrSrSrg)TimestampErrorX   z Generic timestamp-related error.r   N__name__
__module____qualname____firstlineno____doc____static_attributes__r   r
   r   r   r   X   s    (r
   r   c                       \ rS rSrSrSrg)TimezoneNotSpecifiedError]   z4This error is raised when timezone is not specified.r   Nr   r   r
   r   r$   r$   ]   s    <r
   r$   c                       \ rS rSrSrSrg)TimeParseErrorb   z3This error is raised when we can't parse the input.r   Nr   r   r
   r   r'   r'   b   s    ;r
   r'   c                   ,   \ rS rSrSr\R                  " S5      r\R                  " SS9r\R                  " \	R                  * S9r\	R                  (       a  \R                  " \	R                  * S9rO\r\\-
  rS rS rS	 rS
 rS rSS jrSS jrSrg)LocalTimezoneClassj   z"This class defines local timezone.r      )hoursr   c                 ^    U R                  U5      (       a  U R                  $ U R                  $ )z;datetime -> minutes east of UTC (negative for west of UTC).)_isdst	DSTOFFSET	STDOFFSETselfdts     r   	utcoffsetLocalTimezoneClass.utcoffsetx   s#    {{2^^^^r
   c                 ^    U R                  U5      (       a  U R                  $ U R                  $ )z.datetime -> DST offset in minutes east of UTC.)r/   DSTDIFFZEROr2   s     r   dstLocalTimezoneClass.dst   s#    {{2\\YYr
   c                 F    [         R                  U R                  U5         $ )z%datetime -> string name of time zone.)r   tznamer/   r2   s     r   r=   LocalTimezoneClass.tzname   s    ;;t{{2''r
   c           	      "   UR                   UR                  UR                  UR                  UR                  UR
                  UR                  5       SS4	n[        R                  " U5      n[        R                  " U5      nUR                  S:  $ )z2Return true if given datetime is within local DST.r   )yearmonthdayhourminutesecondweekdayr   mktime	localtimetm_isdst)r3   r4   ttstamps       r   r/   LocalTimezoneClass._isdst   sd    
''288RVVRWWbii
**,2
BKKOE		B;;?r
   c                     g)zReturn string '<Local>'.z<Local>r   r3   s    r   __repr__LocalTimezoneClass.__repr__   s    r
   c                 P    UR                   b  [        S5      eUR                  U S9$ )z!Convert naive time to local time.z*Not naive datetime (tzinfo is already set)tzinforT   
ValueErrorreplacer3   r4   unused_is_dsts      r   localizeLocalTimezoneClass.localize   s)    	yyCDD::T:""r
   c                 P    UR                   c  [        S5      eUR                  U S9$ )z7Correct the timezone information on the given datetime.zNaive time - no tzinfo setrS   rU   rX   s      r   	normalizeLocalTimezoneClass.normalize   s)    	yy344::T:""r
   r   N)F)r   r   r   r    r!   datetime	timedeltar9   HOURr   timezoner1   daylightaltzoner0   r8   r5   r:   r=   r/   rP   rZ   r]   r"   r   r
   r   r*   r*   j   s    *			A	$			!	$$  $--8)	]]""DLL=9II	!'(##r
   r*   c                      ^  \ rS rSrSr\r\S 5       r\S 5       rU 4S jr	U 4S jr
\U 4S j5       r\U 4S j5       r\U 4S	 j5       r\U 4S
 j5       r\U 4S j5       r\SS j5       rU 4S jr\S 5       rS rS r\SU 4S jj5       rSrU =r$ )BaseTimestamp   a  Our kind of wrapper over datetime.datetime.

The objects produced by methods now, today, fromtimestamp, utcnow,
utcfromtimestamp are timezone-aware (with correct timezone).
We also overload __add__ and __sub__ method, to fix the result of arithmetic
operations.
c                 Z    UR                   (       d  UR                  U R                  S9$ U$ )z*If obj is naive, add local timezone to it.rS   )rT   rW   LocalTimezoneclsobjs     r   AddLocalTimezoneBaseTimestamp.AddLocalTimezone   s'     ::[[ 1 1[22Jr
   c                 ^    UR                   (       d  U R                  R                  U5      $ U$ )z2If obj is naive, localize it to cls.LocalTimezone.)rT   ri   rZ   rj   s     r   LocalizeBaseTimestamp.Localize   s'     ::'',,Jr
   c           
         > [         [        U ]
  " U0 UD6n[        U 5      " UR                  UR
                  UR                  UR                  UR                  UR                  UR                  UR                  5      $ )zx.__add__(y) <==> x+y.)superrf   __add__typerA   rB   rC   rD   rE   rF   microsecondrT   r3   argskwargsr	__class__s       r   rt   BaseTimestamp.__add__   s[    mT*D;F;A:affaggquuaffahhmmQXX/ /r
   c           
      >  > [         [        U ]
  " U0 UD6n[        U[        R                  5      (       ah  [        U 5      " UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  5      $ U$ )zx.__add__(y) <==> x-y.)rs   rf   __sub__
isinstancer_   ru   rA   rB   rC   rD   rE   rF   rv   rT   rw   s       r   r~   BaseTimestamp.__sub__   ss    mT*D;F;A!X&&''$Z!((qxx1 1Hr
   c                 J   > U R                  [        [        U ]  " U0 UD65      $ )a/  Get a timestamp corresponding to right now.

Args:
  args: Positional arguments to pass to datetime.datetime.now().
  kwargs: Keyword arguments to pass to datetime.datetime.now(). If tz is not
          specified, local timezone is assumed.

Returns:
  A new BaseTimestamp with tz's local day and time.
)rm   rs   rf   nowrk   rx   ry   r{   s      r   r   BaseTimestamp.now   s,     mS%t6v68 8r
   c                 F   > U R                  [        [        U ]  5       5      $ )zjCurrent BaseTimestamp.

Same as self.__class__.fromtimestamp(time.time()).
Returns:
  New self.__class__.
)rm   rs   rf   todayrk   r{   s    r   r   BaseTimestamp.today   s      mS ? ABBr
   c                 J   > U R                  [        [        U ]  " U0 UD65      $ )aM  Get a new localized timestamp from a POSIX timestamp.

Args:
  args: Positional arguments to pass to datetime.datetime.fromtimestamp().
  kwargs: Keyword arguments to pass to datetime.datetime.fromtimestamp().
          If tz is not specified, local timezone is assumed.

Returns:
  A new BaseTimestamp with tz's local day and time.
)rp   rs   rf   fromtimestampr   s      r   r   BaseTimestamp.fromtimestamp   s,     <<mS/@@B Br
   c                 ^   > [         [        U ]  5       R                  [        R
                  S9$ )z9Return a new BaseTimestamp representing UTC day and time.rS   )rs   rf   utcnowrW   pytzutcr   s    r   r   BaseTimestamp.utcnow   s'     +-55TXX5FFr
   c                 b   > [         [        U ]
  " U0 UD6R                  [        R
                  S9$ )zDtimestamp -> UTC datetime from a POSIX timestamp (like time.time()).rS   )rs   rf   utcfromtimestamprW   r   r   r   s      r   r   BaseTimestamp.utcfromtimestamp   s4     5	 12r
   c                     Uc+  U R                  U " [        R                  " X5      SS 6 5      $ UR                  U " [        R                  " X5      SS 6 5      $ )zParse date_string according to format and construct BaseTimestamp.

Args:
  date_string: string passed to time.strptime.
  format: format string passed to time.strptime.
  tz: if not specified, local timezone assumed.
Returns:
  New BaseTimestamp.
Nr   )rp   r   strptimerZ   )rk   date_stringformattzs       r   r   BaseTimestamp.strptime   sQ     
z\\#k B2A FHII;;sT]];?CEFFr
   c           
         > [         [        U ]
  " U0 UD6n[        U 5      " UR                  UR
                  UR                  UR                  UR                  UR                  UR                  UR                  5      $ )z)tz -> convert to time in new timezone tz.)rs   rf   
astimezoneru   rA   rB   rC   rD   rE   rF   rv   rT   rw   s       r   r   BaseTimestamp.astimezone  s[    mT-t>v>A:affaggquuaffahhmmQXX/ /r
   c                 2    U R                  U[        -  5      $ )zCreate new Timestamp object from microsecond UTC timestamp value.

Args:
  ts: integer microsecond UTC timestamp
Returns:
  New cls()
)r   _MICROSECONDS_PER_SECOND_F)rk   tss     r   FromMicroTimestamp BaseTimestamp.FromMicroTimestamp  s     #= =>>r
   c                 4    [        U R                  5       5      $ )z<Return number of seconds since epoch (timestamp in seconds).)r   utctimetuplerO   s    r   AsSecondsSinceEpoch!BaseTimestamp.AsSecondsSinceEpoch   s     1 1 344r
   c                 N    [        U R                  5       5      U R                  -   $ )z:Return microsecond timestamp constructed from this object.)r	   r   rv   rO   s    r   AsMicroTimestampBaseTimestamp.AsMicroTimestamp$  s&    !$":":"<= r
   c                 ^   > [         [        U ]  X5      nU(       a  UR                  U5      nU$ )zCombine date and time into timestamp, timezone-aware.

Args:
  datepart: datetime.date
  timepart: datetime.time
  tz: timezone or None
Returns:
  timestamp object
)rs   rf   combinerZ   )rk   dateparttimepartr   resultr{   s        r   r   BaseTimestamp.combine)  s,     =#.xBF	{{6"fMr
   r   N)r   r   r   r    r!   ri   classmethodrm   rp   rt   r~   r   r   r   r   r   r   r   r   r   r   r   r"   __classcell__)r{   s   @r   rf   rf      s      -   / 8 8 C C B B G G 2 2
 G G/ ? ?5
  r
   rf   sr,   <   mh   dD   wW   Mim  Yz^([0-9]+)([%s])? c                 Z   SnU (       a  [         R                  U 5      nU(       d  g [        UR                  S5      5      nUR                  S5      nU(       a!  [
        R                  U5      nU(       d  gX5-  nX-  nXR                  S5      S n U (       a  M  U$ ! [         a     gf = f)a  Convert a formatted string representing an interval into seconds.

Args:
  interval: String to interpret as an interval.  A basic interval looks like
    "<number><suffix>".  Complex intervals consisting of a chain of basic
    intervals are also allowed.

Returns:
  An integer representing the number of seconds represented by the interval
  string, or None if the interval string could not be decoded.
r   Nr,      )_INTERVAL_REGEXPmatchr   grouprV   _INTERVAL_CONV_DICTgetend)intervaltotalr   numsuffix
multipliers         r   ConvertIntervalToSecondsr   H  s     %""8,EAc [[^F&**62j	c	LE		!&H% 	& 
,  s   B 
B*)B*c                   X    \ rS rSrSr\r\r\	SS j5       r
\	S 5       r\	SS j5       rSrg)		Timestampik  zThis subclass contains methods to parse W3C and interval date spec.

The interval date specification is in the form "1D", where "D" can be
"s"econds "m"inutes "h"ours "D"ays "W"eeks "M"onths "Y"ears.
Nc           
      Z   [         R                  " U5      nUR                  (       d$  U=(       d    U R                  R	                  U5      nU " UR
                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  5      nU$ )zUse dateutil.parser to convert string into timestamp.

dateutil.parser understands ISO8601 which is really handy.

Args:
  timestring: string with datetime
  tz: optional timezone, if timezone is omitted from timestring.
Returns:
  New Timestamp.
)r   parserT   ri   rZ   rA   rB   rC   rD   rE   rF   rv   )rk   
timestringr   rz   r   s        r   _StringToTimeTimestamp._StringToTimet  sv     	Z A88"""
,
,Q
/a!%%188*F Mr
   c                 <    [         R                  " [        U5      S9$ )z@Parse interval date specification and create a timedelta object.r   )r_   r`   r   )rk   r   s     r   _IntStringToIntervalTimestamp._IntStringToInterval  s     &>z&JKKr
   c                     U R                  X5      nU(       a  U$ U R                  U5      nU(       a  U R                  5       U-
  $ [        U5      e)z(Try to create a Timestamp from a string.)r   r   r   r'   )rk   valuer   vals       r   
FromStringTimestamp.FromString  sK     

E
&C
j

"
"5
)C
ZZ\C

r
   r   r   )r   r   r   r    r!   r   INTERVAL_CONV_DICTr   INTERVAL_REGEXPr   r   r   r   r"   r   r
   r   r   r   k  sP    
 +$/ & L L 
  
 r
   r   i'           ;   i?B l        i           )&r!   r   copyr_   resysr   typesdateutilr   r   r   floatr   r	   r   r   r   UTCrb   
US_PACIFICrV   r   r$   r'   rT   r*   ri   rf   r   compilejoinr   r   r   MAXIMUM_PYTHON_TIMESTAMPMAXIMUM_MICROSECOND_TIMESTAMP#MAXIMUM_MICROSECOND_TIMESTAMP_AS_TSr   r
   r   <module>r      s       	 
     # "#;< ,15
F hh]]<(
Z 
 
^ 6# 6#r #$QH%% Ql Ah  3C 88 C  3C 88 C  3C 88 C .s3 C 2377 C .s3 C  3C 88 C !4S!99 C ::0277;N3OOP  F-  - f %"b"b"fc+  !+-E E I &/aQAv&N #r
   