Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/google/apputils/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/google/apputils/__pycache__/datelib.cpython-35.pyc



A��]�<�	@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z
ddlZdZe
e�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Zddd�Zddd�ZejZejd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�dej�Z e �Z!Gdd �d ej�Z"d!d"iZ#d#e#d!e#d$<d#e#d$e#d%<d&e#d%e#d'<e#d'e#d(<d)e#d'e#d*<e#d*e#d+<d,e#d'e#d-<d.e#d'e#d/<ej$d0d1j%e#��Z&d2d3�Z'Gd4d5�d5e"�Z(e(d6d7d8d9d:d:d;e�Z)d<ed"Z*e(d=d"d>d?d@d)d;�Z+dS)Az�Set of classes and functions for dealing with dates and timestamps.

The BaseTimestamp and Timestamp are timezone-aware wrappers around Python
datetime.datetime class.
�Ni@BcCs|tS)z\Convert seconds to microseconds.

  Args:
    seconds: number
  Returns:
    microseconds
  )�_MICROSECONDS_PER_SECOND)�seconds�r�H/opt/alt/python35/lib/python3.5/site-packages/google/apputils/datelib.py�SecondsToMicroseconds(srcCs|tS)z�Convert microseconds to seconds.

  Args:
    microseconds: A number representing some duration of time measured in
      microseconds.
  Returns:
    A number representing the same duration of time measured in seconds.
  )�_MICROSECONDS_PER_SECOND_F)�microsecondsrrr�MicrosecondsToSeconds3s	r	cCstttj���S)zlGet the current time in microseconds, in UTC.

  Returns:
    The number of microseconds since the epoch.
  )�intr�timerrrr�_GetCurrentTimeMicros?srcCstj|dd�d�S)z�Convert time_tuple (in UTC) to seconds (also in UTC).

  Args:
    time_tuple: tuple with at least 6 items.
  Returns:
    seconds.
  N�r)rrr)�calendar�timegm)�
time_tuplerrr�GetSecondsSinceEpochHsrcCsttt|���S)aGet 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
rr)rrrr�
GetTimeMicrosSs
rcCs$tj|j��t}||jS)z�Converts a datetime object to microseconds since the epoch in UTC.

  Args:
    date: A datetime to convert.
  Returns:
    The number of microseconds since the epoch, in UTC, represented by the input
    datetime.
  )rr�utctimetupler�microsecond)�date�microsrrr�DatetimeToUTCMicros`srcCst|�dS)z�Converts a datetime object to milliseconds since the epoch in UTC.

  Args:
    date: A datetime to convert.
  Returns:
    The number of milliseconds since the epoch, in UTC, represented by the input
    datetime.
  i�)r)rrrr�DatetimeToUTCMillists	rcCs5tjj|t�}|dk	r1|j|�}|S)a#Converts a microsecond epoch time to a datetime object.

  Args:
    micros: A UTC time, expressed in microseconds since the epoch.
    tz: The desired tzinfo for the datetime object. If None, the
        datetime will be naive.
  Returns:
    The datetime represented by the input value.
  N)�datetime�utcfromtimestampr�fromutc)r�tz�dtrrr�UTCMicrosToDatetime�s

	
rcCst|d|�S)a#Converts a millisecond epoch time to a datetime object.

  Args:
    millis: A UTC time, expressed in milliseconds since the epoch.
    tz: The desired tzinfo for the datetime object. If None, the
        datetime will be naive.
  Returns:
    The datetime represented by the input value.
  i�)r)Zmillisrrrr�UTCMillisToDatetime�s
rz
US/Pacificc@seZdZdZdS)�TimestampErrorz Generic timestamp-related error.N)�__name__�
__module__�__qualname__�__doc__rrrrr �sr c@seZdZdZdS)�TimezoneNotSpecifiedErrorz4This error is raised when timezone is not specified.N)r!r"r#r$rrrrr%�sr%c@seZdZdZdS)�TimeParseErrorz3This error is raised when we can't parse the input.N)r!r"r#r$rrrrr&�sr&c@s�eZdZdZejd�Zejdd�Zejdej	�Z
ejrkejdej�Z
ne
Z
e
e
Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zddd�Zddd�ZdS)�LocalTimezoneClassz"This class defines local timezone.r�hours�rcCs!|j|�r|jS|jSdS)z;datetime -> minutes east of UTC (negative for west of UTC).N)�_isdst�	DSTOFFSET�	STDOFFSET)�selfrrrr�	utcoffset�szLocalTimezoneClass.utcoffsetcCs!|j|�r|jS|jSdS)z.datetime -> DST offset in minutes east of UTC.N)r*�DSTDIFF�ZERO)r-rrrr�dst�szLocalTimezoneClass.dstcCstj|j|�S)z%datetime -> string name of time zone.)r�tznamer*)r-rrrrr2�szLocalTimezoneClass.tznamec	Csd|j|j|j|j|j|j|j�ddf	}tj|�}tj	|�}|j
dkS)z2Return true if given datetime is within local DST.rr)���)�year�month�day�hour�minute�second�weekdayr�mktime�	localtime�tm_isdst)r-r�ttZstamprrrr*�s
$zLocalTimezoneClass._isdstcCsdS)zReturn string '<Local>'.z<Local>r)r-rrr�__repr__�szLocalTimezoneClass.__repr__FcCs+|jdk	rtd��|jd|�S)z!Convert naive time to local time.Nz*Not naive datetime (tzinfo is already set)�tzinfo)r@�
ValueError�replace)r-r�
unused_is_dstrrr�localize�szLocalTimezoneClass.localizecCs+|jdkrtd��|jd|�S)z7Correct the timezone information on the given datetime.NzNaive time - no tzinfo setr@)r@rArB)r-rrCrrr�	normalize�szLocalTimezoneClass.normalizeN)r!r"r#r$r�	timedeltar0ZHOURr�timezoner,�daylight�altzoner+r/r.r1r2r*r?rDrErrrrr'�s	
r'csNeZdZdZeZedd��Zedd��Z�fdd�Z�fdd	�Z	e�fd
d��Z
e�fdd
��Ze�fdd��Ze�fdd��Z
e�fdd��Zed�fdd��Z�fdd�Zedd��Zdd�Zdd�Zed�fdd ��Z�S)!�
BaseTimestampaOur 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.
  cCs |js|jd|j�S|S)z*If obj is naive, add local timezone to it.r@)r@rB�
LocalTimezone)�cls�objrrr�AddLocalTimezone�s	zBaseTimestamp.AddLocalTimezonecCs|js|jj|�S|S)z2If obj is naive, localize it to cls.LocalTimezone.)r@rKrD)rLrMrrr�Localizes	zBaseTimestamp.Localizec	sXtt|�j||�}t|�|j|j|j|j|j|j	|j
|j�S)zx.__add__(y) <==> x+y.)�superrJ�__add__�typer4r5r6r7r8r9rr@)r-�args�kwargs�r)�	__class__rrrQs-zBaseTimestamp.__add__c	sntt|�j||�}t|tj�rjt|�|j|j|j|j	|j
|j|j|j
�S|S)zx.__add__(y) <==> x-y.)rPrJ�__sub__�
isinstancerrRr4r5r6r7r8r9rr@)r-rSrTrU)rVrrrWs
-zBaseTimestamp.__sub__cs"|jtt|�j||��S)aKGet 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.
    )rNrPrJ�now)rLrSrT)rVrrrYszBaseTimestamp.nowcs|jtt|�j��S)zzCurrent BaseTimestamp.

    Same as self.__class__.fromtimestamp(time.time()).
    Returns:
      New self.__class__.
    )rNrPrJ�today)rL)rVrrrZ)szBaseTimestamp.todaycs"|jtt|�j||��S)aiGet 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.
    )rOrPrJ�
fromtimestamp)rLrSrT)rVrrr[3szBaseTimestamp.fromtimestampcs"tt|�j�jdtj�S)z9Return a new BaseTimestamp representing UTC day and time.r@)rPrJ�utcnowrB�pytz�utc)rL)rVrrr\BszBaseTimestamp.utcnowcs(tt|�j||�jdtj�S)zDtimestamp -> UTC datetime from a POSIX timestamp (like time.time()).r@)rPrJrrBr]r^)rLrSrT)rVrrrGszBaseTimestamp.utcfromtimestampNcs7tt|�j||�}|r*|jn|j|�S)aParse 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.
    )rPrJ�strptimerDrO)rLZdate_string�formatr�	date_time)rVrrr_MszBaseTimestamp.strptimec	sXtt|�j||�}t|�|j|j|j|j|j|j	|j
|j�S)z)tz -> convert to time in new timezone tz.)rPrJ�
astimezonerRr4r5r6r7r8r9rr@)r-rSrTrU)rVrrrb[s-zBaseTimestamp.astimezonecCs|j|t�S)z�Create new Timestamp object from microsecond UTC timestamp value.

    Args:
      ts: integer microsecond UTC timestamp
    Returns:
      New cls()
    )rr)rL�tsrrr�FromMicroTimestampas	z BaseTimestamp.FromMicroTimestampcCst|j��S)z<Return number of seconds since epoch (timestamp in seconds).)rr)r-rrr�AsSecondsSinceEpochlsz!BaseTimestamp.AsSecondsSinceEpochcCst|j��|jS)z:Return microsecond timestamp constructed from this object.)rrer)r-rrr�AsMicroTimestamppszBaseTimestamp.AsMicroTimestampcs4tt|�j||�}|r0|j|�}|S)z�Combine date and time into timestamp, timezone-aware.

    Args:
      datepart: datetime.date
      timepart: datetime.time
      tz: timezone or None
    Returns:
      timestamp object
    )rPrJ�combinerD)rLZdatepartZtimepartr�result)rVrrrguszBaseTimestamp.combine)r!r"r#r$rK�classmethodrNrOrQrWrYrZr[r\rr_rbrdrerfrgrr)rVrrJ�s&

rJ�sr)�<�m�h��d�D��w�W��Mim�Yz^([0-9]+)([%s])?�cCs�d}x�|r�tj|�}|s(dSyt|jd��}Wntk
rYdSYnX|jd�}|r�tj|�}|s�dS||9}||7}||jd�d�}q	W|S)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.
  rNr)�)�_INTERVAL_REGEXP�matchr
�grouprA�_INTERVAL_CONV_DICT�get�end)�interval�totalrz�num�suffixZ
multiplierrrr�ConvertIntervalToSeconds�s$	
	

r�c@s^eZdZdZeZeZeddd��Z	edd��Z
eddd��ZdS)	�	Timestampz�This 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.
  NcCs�ytjj|�}Wnttfk
r4dSYnX|jsV|pJ|jj|�}||j|j	|j
|j|j|j
|j|j�}|S)a>Use 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 or None if unable to parse the timestring.
    N)�dateutil�parser�parse�	TypeErrorrAr@rKrDr4r5r6r7r8r9r)rL�
timestringrrUrhrrr�
_StringToTime�s
		'zTimestamp._StringToTimecCs&t|�}|r"tjd|�SdS)z�Parse interval date specification and create a timedelta object.

    Args:
      timestring: string interval.

    Returns:
      A datetime.timedelta representing the specified interval or None if
      unable to parse the timestring.
    rN)r�rrF)rLr�rrrr�_IntStringToInterval�szTimestamp._IntStringToIntervalcCsR|j|d|�}|r|S|j|�}|rB|j�|St|��dS)a.Create a Timestamp from a string.

    Args:
      value: String interval or datetime.
          e.g. "2013-01-05 13:00:00" or "1d"
      tz: optional timezone, if timezone is omitted from timestring.

    Returns:
      A new Timestamp.

    Raises:
      TimeParseError if unable to parse value.
    rN)r�r�r\r&)rL�valuerrhrrr�
FromString�szTimestamp.FromString)r!r"r#r$r|ZINTERVAL_CONV_DICTryZINTERVAL_REGEXPrir�r�r�rrrrr��sr�i'����;i?Bli����),r$r�copyr�re�sysr�types�warningsZdateutil.parserr�r]r�floatrrr	rrrrrrr�UTCrGZ
US_PACIFICrAr r%r&r@r'rKrJr|�compile�joinryr�r�ZMAXIMUM_PYTHON_TIMESTAMPZMAXIMUM_MICROSECOND_TIMESTAMPZ#MAXIMUM_MICROSECOND_TIMESTAMP_AS_TSrrrr�<module>sZ	

	9	�#N