
^Q\                 @   s   d  Z  d d l m Z d d l m Z m Z m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l Z d d l Z d d l Z d d l m Z m Z m Z m Z Gd	 d
   d
 e  Z e j e j  e _ e j e j  e _ d S)z_
Provides the :class:`Arrow <arrow.arrow.Arrow>` class, an enhanced ``datetime``
replacement.

    )absolute_import)datetime	timedeltatzinfo)tz)relativedelta)truncN)utillocalesparser	formatterc               @   s  e  Z d  Z d Z e j Z d d d d d d d g Z d	 d
   e D Z d Z d d d d d d d  Z	 e
 d d d   Z e
 d d    Z e
 d d d   Z e
 d d    Z e
 d d d   Z e
 d d d   Z e
 d d d   Z e
 d d d d d   Z e
 d d d  d!   Z e
 d" d d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d- d.   Z e d/ d0    Z e j d1 d0    Z e d2 d3    Z e d4 d5    Z e d6 d7    Z e d8 d9    Z d: d;   Z  d< d=   Z! d> d?   Z" d@ dA   Z# d" dB dC  Z$ dD dE   Z% dF dG   Z& dH dI dJ dK  Z' d dI dL dM dN dO  Z( dP dQ   Z) dR dS   Z* dT dU   Z+ dV dW   Z, dX dY   Z- dZ d[   Z. d\ d]   Z/ d^ d_   Z0 d` da   Z1 db dc   Z2 dd de   Z3 df dg   Z4 dh di   Z5 dj dk   Z6 dl dm   Z7 dn do   Z8 dp dq   Z9 dr ds   Z: dt du   Z; dv dw   Z< dx dy   Z= dz d{   Z> d| d}   Z? d~ d d  Z@ d d   ZA d d   ZB d d   ZC eD d d    ZE e
 d d    ZF e
 d d    ZG e
 d d    ZH eD d d    ZI d S)Arrowa  An :class:`Arrow <arrow.arrow.Arrow>` object.

    Implements the ``datetime`` interface, behaving as an aware ``datetime`` while implementing
    additional functionality.

    :param year: the calendar year.
    :param month: the calendar month.
    :param day: the calendar day.
    :param hour: (optional) the hour. Defaults to 0.
    :param minute: (optional) the minute, Defaults to 0.
    :param second: (optional) the second, Defaults to 0.
    :param microsecond: (optional) the microsecond. Defaults 0.
    :param tzinfo: (optional) A timezone expression.  Defaults to UTC.

    .. _tz-expr:

    Recognized timezone expressions:

        - A ``tzinfo`` object.
        - A ``str`` describing a timezone, similar to 'US/Pacific', or 'Europe/Berlin'.
        - A ``str`` in ISO-8601 style, as in '+07:00'.
        - A ``str``, one of the following:  'local', 'utc', 'UTC'.

    Usage::

        >>> import arrow
        >>> arrow.Arrow(2013, 5, 5, 12, 30, 45)
        <Arrow [2013-05-05T12:30:45+00:00]>

    yearmonthdayhourminutesecondmicrosecondc             C   s   g  |  ] } d  j  |   q S)z{0}s)format).0a r   @/var/www/dbchiro/venv/lib/python3.4/site-packages/arrow/arrow.py
<listcomp>9   s   	 zArrow.<listcomp>   r   Nc	       	   	   C   s^   t  j |  r$ t j j |  } n  | p3 t j   } t | | | | | | | |  |  _ d  S)N)	r	   Zisstrr   TzinfoParserparsedateutil_tztzutcr   	_datetime)	selfr   r   r   r   r   r   r   r   r   r   r   __init__<   s
    zArrow.__init__c          	   C   s|   t  j   j d t j    } | j | d k r< t j   n |  } |  | j | j | j	 | j
 | j | j | j | j  S)zConstructs an :class:`Arrow <arrow.arrow.Arrow>` object, representing "now" in the given
        timezone.

        :param tzinfo: (optional) a ``tzinfo`` object. Defaults to local time.

        r   N)r   utcnowreplacer   r   
astimezonetzlocalr   r   r   r   r   r   r   r   )clsr   utcdtr   r   r   nowI   s    	''z	Arrow.nowc          	   C   sF   t  j   } |  | j | j | j | j | j | j | j t	 j
    S)zk Constructs an :class:`Arrow <arrow.arrow.Arrow>` object, representing "now" in UTC
        time.

        )r   r#   r   r   r   r   r   r   r   r   r   )r'   r)   r   r   r   r#   X   s    'zArrow.utcnowc          	   C   sj   | p t  j   } |  j |  } t j | |  } |  | j | j | j | j | j	 | j
 | j | j  S)a   Constructs an :class:`Arrow <arrow.arrow.Arrow>` object from a timestamp, converted to
        the given timezone.

        :param timestamp: an ``int`` or ``float`` timestamp, or a ``str`` that converts to either.
        :param tzinfo: (optional) a ``tzinfo`` object.  Defaults to local time.

        Timestamps should always be UTC. If you have a non-UTC timestamp::

            >>> arrow.Arrow.utcfromtimestamp(1367900664).replace(tzinfo='US/Pacific')
            <Arrow [2013-05-07T04:24:24-07:00]>

        )r   r&   _get_timestamp_from_inputr   fromtimestampr   r   r   r   r   r   r   r   )r'   	timestampr   r)   r   r   r   r,   d   s
    'zArrow.fromtimestampc          	   C   sX   |  j  |  } t j |  } |  | j | j | j | j | j | j | j	 t
 j    S)zConstructs an :class:`Arrow <arrow.arrow.Arrow>` object from a timestamp, in UTC time.

        :param timestamp: an ``int`` or ``float`` timestamp, or a ``str`` that converts to either.

        )r+   r   utcfromtimestampr   r   r   r   r   r   r   r   r   )r'   r-   r)   r   r   r   r.   z   s    'zArrow.utcfromtimestampc          	   C   sO   | p | j  p t j   } |  | j | j | j | j | j | j | j	 |  S)a@   Constructs an :class:`Arrow <arrow.arrow.Arrow>` object from a ``datetime`` and
        optional replacement timezone.

        :param dt: the ``datetime``
        :param tzinfo: (optional) A :ref:`timezone expression <tz-expr>`.  Defaults to ``dt``'s
            timezone, or UTC if naive.

        If you only want to replace the timezone of naive datetimes::

            >>> dt
            datetime.datetime(2013, 5, 5, 0, 0, tzinfo=tzutc())
            >>> arrow.Arrow.fromdatetime(dt, dt.tzinfo or 'US/Pacific')
            <Arrow [2013-05-05T00:00:00+00:00]>

        )
r   r   r   r   r   r   r   r   r   r   )r'   r)   r   r   r   r   fromdatetime   s    'zArrow.fromdatetimec             C   s1   | p t  j   } |  | j | j | j d | S)a   Constructs an :class:`Arrow <arrow.arrow.Arrow>` object from a ``date`` and optional
        replacement timezone.  Time values are set to 0.

        :param date: the ``date``
        :param tzinfo: (optional) A :ref:`timezone expression <tz-expr>`.  Defaults to UTC.
        r   )r   r   r   r   r   )r'   dater   r   r   r   fromdate   s    	zArrow.fromdatec          	   C   sU   t  j | |  } | p | j } |  | j | j | j | j | j | j | j	 |  S)a   Constructs an :class:`Arrow <arrow.arrow.Arrow>` object from a date string and format,
        in the style of ``datetime.strptime``.  Optionally replaces the parsed timezone.

        :param date_str: the date string.
        :param fmt: the format string.
        :param tzinfo: (optional) A :ref:`timezone expression <tz-expr>`.  Defaults to the parsed
            timezone if ``fmt`` contains a timezone directive, otherwise UTC.

        )
r   strptimer   r   r   r   r   r   r   r   )r'   Zdate_strfmtr   r)   r   r   r   r2      s    'zArrow.strptimec                s  |  j  |  \ } } } |  j | d k r3 | j n |  }	 |  j |  j d |	  } |  j | |  \ } } |  j |  j d |	  } |  j |    g  }
 xn   | k rt |
  | k  r|
 j      f d d   |  j	 D } |  d |	 |  t
 i | | 6    q W|
 S)a   Returns a list of :class:`Arrow <arrow.arrow.Arrow>` objects, representing
        an iteration of time between two inputs.

        :param frame: the timeframe.  Can be any ``datetime`` property (day, hour, minute...).
        :param start: A datetime expression, the start of the range.
        :param end: (optional) A datetime expression, the end of the range.
        :param tz: (optional) A :ref:`timezone expression <tz-expr>`.  Defaults to
            ``start``'s timezone, or UTC if ``start`` is naive.
        :param limit: (optional) A maximum number of tuples to return.

        **NOTE**: The ``end`` or ``limit`` must be provided.  Call with ``end`` alone to
        return the entire range.  Call with ``limit`` alone to return a maximum # of results from
        the start.  Call with both to cap a range at a maximum # of results.

        **NOTE**: ``tz`` internally **replaces** the timezones of both ``start`` and ``end`` before
        iterating.  As such, either call with naive objects and ``tz``, or aware objects from the
        same timezone and no ``tz``.

        Supported frame values: year, quarter, month, week, day, hour, minute, second.

        Recognized datetime expressions:

            - An :class:`Arrow <arrow.arrow.Arrow>` object.
            - A ``datetime`` object.

        Usage::

            >>> start = datetime(2013, 5, 5, 12, 30)
            >>> end = datetime(2013, 5, 5, 17, 15)
            >>> for r in arrow.Arrow.range('hour', start, end):
            ...     print(repr(r))
            ...
            <Arrow [2013-05-05T12:30:00+00:00]>
            <Arrow [2013-05-05T13:30:00+00:00]>
            <Arrow [2013-05-05T14:30:00+00:00]>
            <Arrow [2013-05-05T15:30:00+00:00]>
            <Arrow [2013-05-05T16:30:00+00:00]>

        **NOTE**: Unlike Python's ``range``, ``end`` *may* be included in the returned list::

            >>> start = datetime(2013, 5, 5, 12, 30)
            >>> end = datetime(2013, 5, 5, 13, 30)
            >>> for r in arrow.Arrow.range('hour', start, end):
            ...     print(repr(r))
            ...
            <Arrow [2013-05-05T12:30:00+00:00]>
            <Arrow [2013-05-05T13:30:00+00:00]>

        Nr   c                s   g  |  ] } t    |   q Sr   )getattr)r   f)currentr   r   r     s   	 zArrow.range.<locals>.<listcomp>)_get_frames_get_tzinfor   _get_datetimer$   _get_iteration_paramsr/   lenappend_ATTRSr   )r'   framestartendr   limit_frame_relativerelative_stepsr   resultsvaluesr   )r6   r   range   s    4$!'zArrow.rangec                su   |  j  | d k r | j n |  } |  j | |  j    d } |  j   | | | |  }   f d d   | D S)a   Returns a list of tuples, each :class:`Arrow <arrow.arrow.Arrow>` objects,
        representing a series of timespans between two inputs.

        :param frame: the timeframe.  Can be any ``datetime`` property (day, hour, minute...).
        :param start: A datetime expression, the start of the range.
        :param end: (optional) A datetime expression, the end of the range.
        :param tz: (optional) A :ref:`timezone expression <tz-expr>`.  Defaults to
            ``start``'s timezone, or UTC if ``start`` is naive.
        :param limit: (optional) A maximum number of tuples to return.

        **NOTE**: The ``end`` or ``limit`` must be provided.  Call with ``end`` alone to
        return the entire range.  Call with ``limit`` alone to return a maximum # of results from
        the start.  Call with both to cap a range at a maximum # of results.

        **NOTE**: ``tz`` internally **replaces** the timezones of both ``start`` and ``end`` before
        iterating.  As such, either call with naive objects and ``tz``, or aware objects from the
        same timezone and no ``tz``.

        Supported frame values: year, quarter, month, week, day, hour, minute, second.

        Recognized datetime expressions:

            - An :class:`Arrow <arrow.arrow.Arrow>` object.
            - A ``datetime`` object.

        **NOTE**: Unlike Python's ``range``, ``end`` will *always* be included in the returned list
        of timespans.

        Usage:

            >>> start = datetime(2013, 5, 5, 12, 30)
            >>> end = datetime(2013, 5, 5, 17, 15)
            >>> for r in arrow.Arrow.span_range('hour', start, end):
            ...     print(r)
            ...
            (<Arrow [2013-05-05T12:00:00+00:00]>, <Arrow [2013-05-05T12:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T13:00:00+00:00]>, <Arrow [2013-05-05T13:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T14:00:00+00:00]>, <Arrow [2013-05-05T14:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T15:00:00+00:00]>, <Arrow [2013-05-05T15:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T16:00:00+00:00]>, <Arrow [2013-05-05T16:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T17:00:00+00:00]>, <Arrow [2013-05-05T17:59:59.999999+00:00]>)

        Nr   c                s   g  |  ] } | j      q Sr   )span)r   r)r>   r   r   r   :  s   	 z$Arrow.span_range.<locals>.<listcomp>)r8   r   r/   rH   rG   )r'   r>   r?   r@   r   rA   r   Z_ranger   )r>   r   
span_range	  s    .$zArrow.span_range   c                sm     d k  r t  d   n  |  j | | | |   t       }    f d d   t d |    D S)a4   Returns an array of tuples, each :class:`Arrow <arrow.arrow.Arrow>` objects,
        representing a series of intervals between two inputs.

        :param frame: the timeframe.  Can be any ``datetime`` property (day, hour, minute...).
        :param start: A datetime expression, the start of the range.
        :param end: (optional) A datetime expression, the end of the range.
        :param interval: (optional) Time interval for the given time frame.
        :param tz: (optional) A timezone expression.  Defaults to UTC.
        
        Supported frame values: year, quarter, month, week, day, hour, minute, second

        Recognized datetime expressions:

            - An :class:`Arrow <arrow.arrow.Arrow>` object.
            - A ``datetime`` object.

        Recognized timezone expressions:

            - A ``tzinfo`` object.
            - A ``str`` describing a timezone, similar to 'US/Pacific', or 'Europe/Berlin'.
            - A ``str`` in ISO-8601 style, as in '+07:00'.
            - A ``str``, one of the following:  'local', 'utc', 'UTC'.

        Usage:

            >>> start = datetime(2013, 5, 5, 12, 30)
            >>> end = datetime(2013, 5, 5, 17, 15)
            >>> for r in arrow.Arrow.interval('hour', start, end, 2):
            ...     print r
            ...
            (<Arrow [2013-05-05T12:00:00+00:00]>, <Arrow [2013-05-05T13:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T14:00:00+00:00]>, <Arrow [2013-05-05T15:59:59.999999+00:00]>)
            (<Arrow [2013-05-05T16:00:00+00:00]>, <Arrow [2013-05-05T17:59:59.999999+00:0]>)
        rK   z%interval has to be a positive integerc                s4   g  |  ]* }  | d   |   d d f  q S)r   rK   r   )r   i)interval	spanRanger   r   r   f  s   	 z"Arrow.interval.<locals>.<listcomp>r   )
ValueErrorrJ   r;   rG   )r'   r>   r?   r@   rM   r   Zboundr   )rM   rN   r   rM   <  s
    $zArrow.intervalc             C   s   d j  |  j j |  j    S)Nz<{0} [{1}]>)r   	__class____name____str__)r!   r   r   r   __repr__j  s    zArrow.__repr__c             C   s   |  j  j   S)N)r    	isoformat)r!   r   r   r   rR   m  s    zArrow.__str__c             C   s)   t  |  d k r |  j |  St |   S)Nr   )r;   r   str)r!   Z	formatstrr   r   r   
__format__p  s    zArrow.__format__c             C   s   |  j  j   S)N)r    __hash__)r!   r   r   r   rW   w  s    zArrow.__hash__c             C   s   | d k r |  j    d S| d k rB t |  j d |  j  d S| j d  sy t |  j | d   } | d  k	 ry | Sn  t j |  |  S)NweekrK   quarterrB   )	isocalendarintr   _MONTHS_PER_QUARTER
startswithr4   r    object__getattribute__)r!   namevaluer   r   r   __getattr__}  s    zArrow.__getattr__c             C   s
   |  j  j S)zG Gets the ``tzinfo`` of the :class:`Arrow <arrow.arrow.Arrow>` object. )r    r   )r!   r   r   r   r     s    zArrow.tzinfoc             C   s   |  j  j d |  |  _  d S)zG Sets the ``tzinfo`` of the :class:`Arrow <arrow.arrow.Arrow>` object. r   N)r    r$   )r!   r   r   r   r   r     s    c             C   s   |  j  S)zU Returns a datetime representation of the :class:`Arrow <arrow.arrow.Arrow>` object. )r    )r!   r   r   r   r     s    zArrow.datetimec             C   s   |  j  j d d  S)zc Returns a naive datetime representation of the :class:`Arrow <arrow.arrow.Arrow>`
        object. r   N)r    r$   )r!   r   r   r   naive  s    zArrow.naivec             C   s   t  j |  j j    S)zk Returns a timestamp representation of the :class:`Arrow <arrow.arrow.Arrow>` object, in
        UTC time. )calendartimegmr    utctimetuple)r!   r   r   r   r-     s    zArrow.timestampc             C   s   |  j  t |  j  d S)zp Returns a floating-point representation of the :class:`Arrow <arrow.arrow.Arrow>`
        object, in UTC time. i@B )r-   floatr   )r!   r   r   r   float_timestamp  s    zArrow.float_timestampc             C   s   |  j  |  j  S)z Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, cloned from the current one.

        Usage:

            >>> arw = arrow.utcnow()
            >>> cloned = arw.clone()

        )r/   r    )r!   r   r   r   clone  s    
zArrow.clonec             K   sd  i  } i  } x | j    D] \ } } | |  j k rA | | | <q | |  j k s\ | d k ry t j d t  | | | <q | d k r t d j |    q | d k r t d j |    q q W| j d	 d
  | d	 | j	 d d
  |  j
 7<|  j j |   } | t |   7} | j d  } | d k	 rW|  j |  } | j d |  } n  |  j |  S)a   Returns a new :class:`Arrow <arrow.arrow.Arrow>` object with attributes updated
        according to inputs.

        Use property names to set their value absolutely::

            >>> import arrow
            >>> arw = arrow.utcnow()
            >>> arw
            <Arrow [2013-05-11T22:27:34.787885+00:00]>
            >>> arw.replace(year=2014, month=6)
            <Arrow [2014-06-11T22:27:34.787885+00:00]>

        You can also replace the timezone without conversion, using a
        :ref:`timezone expression <tz-expr>`::

            >>> arw.replace(tzinfo=tz.tzlocal())
            <Arrow [2013-05-11T22:27:34.787885-07:00]>

        weeksquarterszOreplace() with plural property to shift valueis deprecated, use shift() insteadrX   rY   z%setting absolute {0} is not supportedr   zunknown attribute: "{0}"monthsr   N)zweeksrk   )zweekzquarter)itemsr=   _ATTRS_PLURALwarningswarnDeprecationWarningAttributeErrorr   
setdefaultpopr\   r    r$   r   getr8   r/   )r!   kwargsZabsolute_kwargsrelative_kwargskeyra   r6   r   r   r   r   r$     s,    	!zArrow.replacec             K   s   i  } xK | j    D]= \ } } | |  j k s: | d k rG | | | <q t    q W| j d d  | d | j d d  |  j 7<|  j t |   } |  j |  S)a   Returns a new :class:`Arrow <arrow.arrow.Arrow>` object with attributes updated
        according to inputs.

        Use pluralized property names to shift their current value relatively:

        >>> import arrow
        >>> arw = arrow.utcnow()
        >>> arw
        <Arrow [2013-05-11T22:27:34.787885+00:00]>
        >>> arw.shift(years=1, months=-1)
        <Arrow [2014-04-11T22:27:34.787885+00:00]>

        Day-of-the-week relative shifting can use either Python's weekday numbers
        (Monday = 0, Tuesday = 1 .. Sunday = 6) or using dateutil.relativedelta's
        day instances (MO, TU .. SU).  When using weekday numbers, the returned
        date will always be greater than or equal to the starting date.

        Using the above code (which is a Saturday) and asking it to shift to Saturday:

        >>> arw.shift(weekday=5)
        <Arrow [2013-05-11T22:27:34.787885+00:00]>

        While asking for a Monday:

        >>> arw.shift(weekday=0)
        <Arrow [2013-05-13T22:27:34.787885+00:00]>

        rj   rk   weekdayrl   r   )zweekszquarterszweekday)	rm   rn   rr   rs   rt   r\   r    r   r/   )r!   rv   rw   rx   ra   r6   r   r   r   shift  s    !zArrow.shiftc          	   C   sp   t  | t  s$ t j j |  } n  |  j j |  } |  j | j | j	 | j
 | j | j | j | j | j  S)a   Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, converted
        to the target timezone.

        :param tz: A :ref:`timezone expression <tz-expr>`.

        Usage::

            >>> utc = arrow.utcnow()
            >>> utc
            <Arrow [2013-05-09T03:49:12.311072+00:00]>

            >>> utc.to('US/Pacific')
            <Arrow [2013-05-08T20:49:12.311072-07:00]>

            >>> utc.to(tz.tzlocal())
            <Arrow [2013-05-08T20:49:12.311072-07:00]>

            >>> utc.to('-07:00')
            <Arrow [2013-05-08T20:49:12.311072-07:00]>

            >>> utc.to('local')
            <Arrow [2013-05-08T20:49:12.311072-07:00]>

            >>> utc.to('local').to('utc')
            <Arrow [2013-05-09T03:49:12.311072+00:00]>

        )
isinstancer   r   r   r   r    r%   rP   r   r   r   r   r   r   r   )r!   r   r)   r   r   r   to(  s
    *zArrow.toc                s]    j  |  \ } } } | d k r- d } n | d k rB d } n | }   j j |  }   j d | d  }   f d d   | D }	 x+ t d	 t |	   D] }
 |	 j d  q W  j d
   j |	  } | d k r | t d   j	   d  } n. | d k r+| t d   j
 d d	  } n  | t i | | | 6  t d d  } | | f S)a   Returns two new :class:`Arrow <arrow.arrow.Arrow>` objects, representing the timespan
        of the :class:`Arrow <arrow.arrow.Arrow>` object in a given timeframe.

        :param frame: the timeframe.  Can be any ``datetime`` property (day, hour, minute...).
        :param count: (optional) the number of frames to span.

        Supported frame values: year, quarter, month, week, day, hour, minute, second.

        Usage::

            >>> arrow.utcnow()
            <Arrow [2013-05-09T03:32:36.186203+00:00]>

            >>> arrow.utcnow().span('hour')
            (<Arrow [2013-05-09T03:00:00+00:00]>, <Arrow [2013-05-09T03:59:59.999999+00:00]>)

            >>> arrow.utcnow().span('day')
            (<Arrow [2013-05-09T00:00:00+00:00]>, <Arrow [2013-05-09T23:59:59.999999+00:00]>)

            >>> arrow.utcnow().span('day', count=2)
            (<Arrow [2013-05-09T00:00:00+00:00]>, <Arrow [2013-05-10T23:59:59.999999+00:00]>)

        rX   r   rY   r   NrK   c                s   g  |  ] } t    |   q Sr   )r4   )r   r5   )r!   r   r   r   r  s   	 zArrow.span.<locals>.<listcomp>r   r   daysrl   microseconds)r7   r=   indexrG   r;   r<   rP   r   r   
isoweekdayr   )r!   r>   countZframe_absoluterC   rD   attrr   ZframesrF   rL   floorceilr   )r!   r   rH   M  s$    		!"(z
Arrow.spanc             C   s   |  j  |  d S)a   Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, representing the "floor"
        of the timespan of the :class:`Arrow <arrow.arrow.Arrow>` object in a given timeframe.
        Equivalent to the first element in the 2-tuple returned by
        :func:`span <arrow.arrow.Arrow.span>`.

        :param frame: the timeframe.  Can be any ``datetime`` property (day, hour, minute...).

        Usage::

            >>> arrow.utcnow().floor('hour')
            <Arrow [2013-05-09T03:00:00+00:00]>
        r   )rH   )r!   r>   r   r   r   r     s    zArrow.floorc             C   s   |  j  |  d S)a   Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, representing the "ceiling"
        of the timespan of the :class:`Arrow <arrow.arrow.Arrow>` object in a given timeframe.
        Equivalent to the second element in the 2-tuple returned by
        :func:`span <arrow.arrow.Arrow.span>`.

        :param frame: the timeframe.  Can be any ``datetime`` property (day, hour, minute...).

        Usage::

            >>> arrow.utcnow().ceil('hour')
            <Arrow [2013-05-09T03:59:59.999999+00:00]>
        rK   )rH   )r!   r>   r   r   r   r     s    z
Arrow.ceilzYYYY-MM-DD HH:mm:ssZZen_usc             C   s   t  j |  j |  j |  S)a   Returns a string representation of the :class:`Arrow <arrow.arrow.Arrow>` object,
        formatted according to a format string.

        :param fmt: the format string.

        Usage::

            >>> arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ')
            '2013-05-09 03:56:47 -00:00'

            >>> arrow.utcnow().format('X')
            '1368071882'

            >>> arrow.utcnow().format('MMMM DD, YYYY')
            'May 09, 2013'

            >>> arrow.utcnow().format()
            '2013-05-09 03:56:47 -00:00'

        )r   ZDateTimeFormatterr   r    )r!   r3   localer   r   r   r     s    zArrow.formatFautoc             C   s  t  j |  } | d k rQ t j   j d t j    } | j |  j j	  } nu t
 | t  rl | j } nZ t
 | t  r | j	 d k r | j d |  j j	  } q | j |  j j	  } n	 t    t t j |  j |   } | d k  r d+ n d } t |  }	 |	 } | d k rs|	 d k  r7| j d d | S|	 d	 k  rc| | }
 | j d
 |
 d | S|	 d k  r| j d | d | S|	 d k  r| t t | d d   } | j d | d | S|	 d k  r| j d | d | S|	 d k  r%| t t | d d   } | j d | d | S|	 d k  rG| j d | d | S|	 d k  r| t t | d d   } | j d | d | S|	 d k  r| j d | d | S|	 d k  r|  j j d |  j j } | j d | j } | t t t | |  d   } | j d | d | S|	 d  k  r=| j d! | d | S| t t | d" d   } | j d# | d | Sn4| d$ k r| | } t |  d k  rl| j d d | Sn | d k r| | t d  } n | d k r| | t d,  } nu | d k r| | t d.  } nR | d k r=| | t d1  } n/ | d! k r`| | t d4  } n t d(   t t |   d k r| d) 7} n  | j | | d d* Sd S)5ay   Returns a localized, humanized representation of a relative difference in time.

        :param other: (optional) an :class:`Arrow <arrow.arrow.Arrow>` or ``datetime`` object.
            Defaults to now in the current :class:`Arrow <arrow.arrow.Arrow>` object's timezone.
        :param locale: (optional) a ``str`` specifying a locale.  Defaults to 'en_us'.
        :param only_distance: (optional) returns only time difference eg: "11 seconds" without "in" or "ago" part.
        :param granularity: (optional) defines the precision of the output. Set it to strings 'second', 'minute', 'hour', 'day', 'month' or 'year'.
        Usage::

            >>> earlier = arrow.utcnow().shift(hours=-2)
            >>> earlier.humanize()
            '2 hours ago'

            >>> later = later = earlier.shift(hours=4)
            >>> later.humanize(earlier)
            'in 4 hours'

        Nr   r   rK   r   
   r*   only_distance-   secondsZ   r   i
  <      minutesi  r   i`5 i  hoursi@ r   i  iQ r}   iS; r   i   rl   i$r   i3yearsr      g     >@g     v@zError. Could not understand your level of granularity. Please select between                 "second", "minute", "hour", "day", "week", "month" or "year"sFr   i  i  iQ i  iQ g    DAi  iQ g    ~~A)r
   Z
get_localer   r#   r$   r   r   r%   r    r   r{   r   	TypeErrorr[   r	   total_secondsabsZdescribemaxr   r   rg   rr   r   )r!   otherr   r   Zgranularityr(   r)   deltasigndiffr   r   r   r}   Zself_monthsZother_monthsrl   r   r   r   r   humanize  s    	
#
	zArrow.humanizec             C   s?   t  | t t f  r2 |  j |  j | |  j j  St    d  S)N)r{   r   r   r/   r    r   r   )r!   r   r   r   r   __add__,  s    zArrow.__add__c             C   s   |  j  |  S)N)r   )r!   r   r   r   r   __radd__3  s    zArrow.__radd__c             C   sv   t  | t t f  r2 |  j |  j | |  j j  St  | t  rL |  j | St  | t  ri |  j | j St    d  S)N)	r{   r   r   r/   r    r   r   r   r   )r!   r   r   r   r   __sub__6  s    zArrow.__sub__c             C   s'   t  | t  r | |  j St    d  S)N)r{   r   r    r   )r!   r   r   r   r   __rsub__C  s    zArrow.__rsub__c             C   s(   t  d j t |   t |     d  S)Nzcan't compare '{0}' to '{1}')r   r   type)r!   r   r   r   r   	_cmperrorM  s    	zArrow._cmperrorc             C   s/   t  | t t f  s d S|  j |  j |  k S)NF)r{   r   r   r    r9   )r!   r   r   r   r   __eq__Q  s    zArrow.__eq__c             C   s   |  j  |  S)N)r   )r!   r   r   r   r   __ne__X  s    zArrow.__ne__c             C   s;   t  | t t f  s% |  j |  n  |  j |  j |  k S)N)r{   r   r   r   r    r9   )r!   r   r   r   r   __gt__[  s    zArrow.__gt__c             C   s;   t  | t t f  s% |  j |  n  |  j |  j |  k S)N)r{   r   r   r   r    r9   )r!   r   r   r   r   __ge__b  s    zArrow.__ge__c             C   s;   t  | t t f  s% |  j |  n  |  j |  j |  k  S)N)r{   r   r   r   r    r9   )r!   r   r   r   r   __lt__i  s    zArrow.__lt__c             C   s;   t  | t t f  s% |  j |  n  |  j |  j |  k S)N)r{   r   r   r   r    r9   )r!   r   r   r   r   __le__p  s    zArrow.__le__c             C   s   |  j  j   S)z> Returns a ``date`` object with the same year, month and day. )r    r0   )r!   r   r   r   r0   z  s    z
Arrow.datec             C   s   |  j  j   S)zL Returns a ``time`` object with the same hour, minute, second, microsecond. )r    time)r!   r   r   r   r     s    z
Arrow.timec             C   s   |  j  j   S)z_ Returns a ``time`` object with the same hour, minute, second, microsecond and
        tzinfo. )r    timetz)r!   r   r   r   r     s    zArrow.timetzc             C   s   |  j  j |  S)zw Returns a ``datetime`` object, converted to the specified timezone.

        :param tz: a ``tzinfo`` object.

        )r    r%   )r!   r   r   r   r   r%     s    zArrow.astimezonec             C   s   |  j  j   S)zk Returns a ``timedelta`` object representing the whole number of minutes difference from
        UTC time. )r    	utcoffset)r!   r   r   r   r     s    zArrow.utcoffsetc             C   s   |  j  j   S)z/ Returns the daylight savings time adjustment. )r    dst)r!   r   r   r   r     s    z	Arrow.dstc             C   s   |  j  j   S)z: Returns a ``time.struct_time``, in the current timezone. )r    	timetuple)r!   r   r   r   r     s    zArrow.timetuplec             C   s   |  j  j   S)z. Returns a ``time.struct_time``, in UTC time. )r    rf   )r!   r   r   r   rf     s    zArrow.utctimetuplec             C   s   |  j  j   S)z6 Returns the proleptic Gregorian ordinal of the date. )r    	toordinal)r!   r   r   r   r     s    zArrow.toordinalc             C   s   |  j  j   S)z2 Returns the day of the week as an integer (0-6). )r    ry   )r!   r   r   r   ry     s    zArrow.weekdayc             C   s   |  j  j   S)z6 Returns the ISO day of the week as an integer (1-7). )r    r   )r!   r   r   r   r     s    zArrow.isoweekdayc             C   s   |  j  j   S)z> Returns a 3-tuple, (ISO year, ISO week number, ISO weekday). )r    rZ   )r!   r   r   r   rZ     s    zArrow.isocalendarTc             C   s   |  j  j |  S)zCReturns an ISO 8601 formatted representation of the date and time. )r    rT   )r!   sepr   r   r   rT     s    zArrow.isoformatc             C   s   |  j  j   S)z@ Returns a ctime formatted representation of the date and time. )r    ctime)r!   r   r   r   r     s    zArrow.ctimec             C   s   |  j  j |  S)zd Formats in the style of ``datetime.strptime``.

        :param format: the format string.

        )r    strftime)r!   r   r   r   r   r     s    zArrow.strftimec             C   s
   |  j    S)z7Serializes for the ``for_json`` protocol of simplejson.)rT   )r!   r   r   r   for_json  s    zArrow.for_jsonc             C   sn   |  d  k r t  j   St |  t  r) |  Sy t j j |   SWn* t j k
 ri t d j	 |     Yn Xd  S)Nz"'{0}' not recognized as a timezone)
r   r   r{   r   r   r   r   ParserErrorrO   r   )Ztz_exprr   r   r   r8     s    
	zArrow._get_tzinfoc          	   C   sm   t  | t  r | j St  | t  r) | Sy  t |  } |  j |  j SWn t d j |    Yn Xd  S)Nz/'{0}' not recognized as a timestamp or datetime)r{   r   r   rg   r.   rO   r   )r'   exprr   r   r   r9     s    zArrow._get_datetimec             C   s   | |  j  k r% | d j |  d f S| d k r5 d S| d k rE d Sd	 j |  j  d d g d d g  } t d
 j | |    d  S)Nz{0}srK   rX   rj   rY   rk   rl   r   z, z>range/span over frame {0} not supported. Supported frames: {1})weekweeks)r   r   rK   )quarterzquarters)r   zmonthsr   )r=   r   joinrr   )r'   r`   Z	supportedr   r   r   r7     s    &zArrow._get_framesc             C   s[   | d  k r4 | d  k r' t  d   n  |  j | f S| d  k rM | t j f S| | f Sd  S)Nz#one of 'end' or 'limit' is required)	Exceptionr   sysmaxsize)r'   r@   rA   r   r   r   r:     s    zArrow._get_iteration_paramsc          	   C   s2   y t  |   SWn t d j |     Yn Xd  S)Nz!cannot parse '{0}' as a timestamp)rg   rO   r   )r-   r   r   r   r+     s    zArrow._get_timestamp_from_input)JrQ   
__module____qualname____doc__r   
resolutionr=   rn   r\   r"   classmethodr*   r#   r,   r.   r/   r1   r2   rG   rJ   rM   rS   rR   rV   rW   rb   propertyr   setterrc   r-   rh   ri   r$   rz   r|   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   r   r   r%   r   r   r   rf   r   ry   r   rZ   rT   r   r   r   staticmethodr8   r9   r7   r:   r+   r   r   r   r   r      s   	G2-
6/%6m

		r   )r   
__future__r   r   r   r   Zdateutilr   r   Zdateutil.relativedeltar   mathr   rd   r   ro   arrowr	   r
   r   r   r^   r   r/   minr   r   r   r   r   <module>   s    "    