Beispiel #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 in (None, 0):
        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)
Beispiel #2
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 in (None, 0):
        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)
Beispiel #3
0
    def set_tzo(self, value):
        """
        Set the timezone offset from UTC.

        :param 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:])
Beispiel #4
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:])
Beispiel #5
0
    def set_utc(self, value):
        """
        Set the time in UTC.

        :param 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)
        self._set_sub_text('utc', value)
Beispiel #6
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)
Beispiel #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
Beispiel #8
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
Beispiel #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
Beispiel #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