Exemplo n.º 1
0
def time(hour=None, min=None, sec=None, micro=None, offset=None, obj=False):
    """
    Create a time only timestamp for the given instant.

    Unspecified components default to their current counterparts.

    Arguments:
        hour   -- Integer value of the hour.
        min    -- Integer value of the number of minutes.
        sec    -- Integer value of the number of seconds.
        micro  -- Integer value of the number of microseconds.
        offset -- Either a positive or negative number of seconds
                  to offset from UTC to match a desired timezone,
                  or a tzinfo object.
        obj    -- If True, return the time object instead
                  of a formatted string. Defaults to False.
    """
    now = dt.datetime.utcnow()
    if hour is None:
        hour = now.hour
    if min is None:
        min = now.minute
    if sec is None:
        sec = now.second
    if micro is None:
        micro = now.microsecond
    if offset is None:
        offset = tzutc()
    elif not isinstance(offset, dt.tzinfo):
        offset = tzoffset(None, offset)
    value = dt.time(hour, min, sec, micro, offset)
    if obj:
        return value
    return format_time(value)
Exemplo n.º 2
0
    def set_tzo(self, value):
        """
        Set the timezone offset from UTC.

        Arguments:
            value -- Either a tzinfo object or the number of
                     seconds (positive or negative) to offset.
        """
        time = xep_0082.time(offset=value)
        if xep_0082.parse(time).tzinfo == tzutc():
            self._set_sub_text('tzo', 'Z')
        else:
            self._set_sub_text('tzo', time[-6:])
Exemplo n.º 3
0
    def set_tzo(self, value):
        """
        Set the timezone offset from UTC.

        Arguments:
            value -- Either a tzinfo object or the number of
                     seconds (positive or negative) to offset.
        """
        time = xep_0082.time(offset=value)
        if xep_0082.parse(time).tzinfo == tzutc():
            self._set_sub_text('tzo', 'Z')
        else:
            self._set_sub_text('tzo', time[-6:])
Exemplo n.º 4
0
    def set_utc(self, value):
        """
        Set the time in UTC.

        Arguments:
            value -- A datetime object or properly formatted
                     string equivalent.
        """
        date = value
        if not isinstance(value, dt.datetime):
            date = xep_0082.parse(value)
        date = date.astimezone(tzutc())
        value = xep_0082.format_datetime(date)[:-1]
        self._set_sub_text('utc', value)
Exemplo n.º 5
0
    def set_utc(self, value):
        """
        Set the time in UTC.

        Arguments:
            value -- A datetime object or properly formatted
                     string equivalent.
        """
        date = value
        if not isinstance(value, dt.datetime):
            date = xep_0082.parse(value)
        date = date.astimezone(tzutc())
        value = xep_0082.format_datetime(date)[:-1]
        self._set_sub_text('utc', value)
Exemplo n.º 6
0
def format_datetime(time_obj):
    """
    Return a formatted string version of a datetime object.

    Format:
        YYYY-MM-DDThh:mm:ss[.sss]TZD

    arguments:
        time_obj -- A datetime object.
    """
    timestamp = time_obj.isoformat("T")
    if time_obj.tzinfo == tzutc():
        timestamp = timestamp[:-6]
        return "%sZ" % timestamp
    return timestamp
Exemplo n.º 7
0
def format_datetime(time_obj):
    """
    Return a formatted string version of a datetime object.

    Format:
        YYYY-MM-DDThh:mm:ss[.sss]TZD

    arguments:
        time_obj -- A datetime object.
    """
    timestamp = time_obj.isoformat('T')
    if time_obj.tzinfo == tzutc():
        timestamp = timestamp[:-6]
        return '%sZ' % timestamp
    return timestamp
Exemplo n.º 8
0
def datetime(year=None, month=None, day=None, hour=None,
             min=None, sec=None, micro=None, offset=None,
             separators=True, obj=False):
    """
    Create a datetime timestamp for the given instant.

    Unspecified components default to their current counterparts.

    Arguments:
        year   -- Integer value of the year (4 digits)
        month  -- Integer value of the month
        day    -- Integer value of the day of the month.
        hour   -- Integer value of the hour.
        min    -- Integer value of the number of minutes.
        sec    -- Integer value of the number of seconds.
        micro  -- Integer value of the number of microseconds.
        offset -- Either a positive or negative number of seconds
                  to offset from UTC to match a desired timezone,
                  or a tzinfo object.
        obj    -- If True, return the datetime object instead
                  of a formatted string. Defaults to False.
    """
    now = dt.datetime.utcnow()
    if year is None:
        year = now.year
    if month is None:
        month = now.month
    if day is None:
        day = now.day
    if hour is None:
        hour = now.hour
    if min is None:
        min = now.minute
    if sec is None:
        sec = now.second
    if micro is None:
        micro = now.microsecond
    if offset is None:
        offset = tzutc()
    elif not isinstance(offset, dt.tzinfo):
        offset = tzoffset(None, offset)

    value = dt.datetime(year, month, day, hour,
                       min, sec, micro, offset)
    if obj:
        return value 
    return format_datetime(value)
Exemplo n.º 9
0
def format_time(time_obj):
    """
    Return a formatted string version of a time object.

    format:
        hh:mm:ss[.sss][TZD]

    arguments:
        time_obj -- A time or datetime object.
    """
    if isinstance(time_obj, dt.datetime):
        time_obj = time_obj.timetz()
    timestamp = time_obj.isoformat()
    if time_obj.tzinfo == tzutc():
        timestamp = timestamp[:-6]
        return "%sZ" % timestamp
    return timestamp
Exemplo n.º 10
0
def format_time(time_obj):
    """
    Return a formatted string version of a time object.

    format:
        hh:mm:ss[.sss][TZD]

    arguments:
        time_obj -- A time or datetime object.
    """
    if isinstance(time_obj, dt.datetime):
        time_obj = time_obj.timetz()
    timestamp = time_obj.isoformat()
    if time_obj.tzinfo == tzutc():
        timestamp = timestamp[:-6]
        return '%sZ' % timestamp
    return timestamp
Exemplo n.º 11
0
def datetime(year=None, month=None, day=None, hour=None,
             min=None, sec=None, micro=None, offset=None,
             separators=True):
    """
    Create a datetime timestamp for the given instant.

    Unspecified components default to their current counterparts.

    Arguments:
        year   -- Integer value of the year (4 digits)
        month  -- Integer value of the month
        day    -- Integer value of the day of the month.
        hour   -- Integer value of the hour.
        min    -- Integer value of the number of minutes.
        sec    -- Integer value of the number of seconds.
        micro  -- Integer value of the number of microseconds.
        offset -- Either a positive or negative number of seconds
                  to offset from UTC to match a desired timezone,
                  or a tzinfo object.
    """
    now = dt.datetime.utcnow()
    if year is None:
        year = now.year
    if month is None:
        month = now.month
    if day is None:
        day = now.day
    if hour is None:
        hour = now.hour
    if min is None:
        min = now.minute
    if sec is None:
        sec = now.second
    if micro is None:
        micro = now.microsecond
    if offset is None:
        offset = tzutc()
    elif not isinstance(offset, dt.tzinfo):
        offset = tzoffset(None, offset)

    date = dt.datetime(year, month, day, hour,
                       min, sec, micro, offset)
    return format_datetime(date)