Exemple #1
0
def normalized_time(time):
    """
    Takes a string representation of a time value, validates and parses
    it and returns a JSON-friendly string representation of the normalized
    time.

    It rounds down a date to the first of the month.

    It takes any date greater than 60 days into the past
    or in the future and sets it to the current date.
    """
    now = util.utcnow()
    if not time:
        time = None

    try:
        time = iso8601.parse_date(time)
    except (iso8601.ParseError, TypeError):
        time = now
    else:
        # don't accept future time values or
        # time values more than 60 days in the past
        min_time = now - timedelta(days=60)
        if time > now or time < min_time:
            time = now
    # cut down the time to a monthly resolution
    time = time.date().replace(day=1)
    return encode_datetime(time)
Exemple #2
0
    def deserialize(self, node, cstructdict):
        if not cstructdict:
            return null

        # print 'DateTimeRange deserialize:', cstructdict

        ret = []


        # for (key,cstruct) in cstructdict.items():
        for cstruct in cstructdict:


            try:
                result = iso8601.parse_date(
                    cstruct, default_timezone=self.default_tzinfo)
            except (iso8601.ParseError, TypeError) as e:
                try:
                    year, month, day = map(int, cstruct.split('-', 2))
                    result = datetime.datetime(year, month, day,
                                               tzinfo=self.default_tzinfo)
                except Exception as e:
                    raise Invalid(node, _(self.err_template,
                                          mapping={'val':cstruct, 'err':e}))

            # ret[key] = result
            ret.append(result)

        return ret
Exemple #3
0
    def new_from_dict(cls, dict_):
        '''
            Custom new_from_dict() functionality for SpatialRelease
        '''
        if ('release_time' in dict_ and not isinstance(dict_['release_time'], datetime)):
            dict_['release_time'] = iso8601.parse_date(dict_['release_time'],
                                                       default_timezone=None)

        return super(SpatialRelease, cls).new_from_dict(dict_)
Exemple #4
0
    def new_from_dict(cls, dict_):
        '''
            Custom new_from_dict() functionality for SpatialRelease
        '''
        if ('release_time' in dict_
                and not isinstance(dict_['release_time'], datetime)):
            dict_['release_time'] = iso8601.parse_date(dict_['release_time'],
                                                       default_timezone=None)

        return super(SpatialRelease, cls).new_from_dict(dict_)
Exemple #5
0
 def deserialize(self, value, mapping, node, model):
     try:
         result = iso8601.parse_date(value, default_timezone=self.default_tzinfo)
     except (iso8601.ParseError, TypeError):
         try:
             year, month, day = map(int, value.split('-', 2))
             result = datetime.datetime(year, month, day,
                                        tzinfo=self.default_tzinfo)
         except Exception:
             raise Invalid('SchemaNode is not a datetime', node)
     return result
Exemple #6
0
def process_time(measure, utcnow, utcmin):
    try:
        measure['time'] = iso8601.parse_date(measure['time'])
    except (iso8601.ParseError, TypeError):
        if measure['time']:  # pragma: no cover
            # ignore debug log for empty values
            pass
        measure['time'] = utcnow
    else:
        # don't accept future time values or
        # time values more than 60 days in the past
        if measure['time'] > utcnow or measure['time'] < utcmin:
            measure['time'] = utcnow
    return measure
Exemple #7
0
def process_time(measure, utcnow, utcmin):
    try:
        measure['time'] = iso8601.parse_date(measure['time'])
    except (iso8601.ParseError, TypeError):
        if measure['time']:  # pragma: no cover
            # ignore debug log for empty values
            pass
        measure['time'] = utcnow
    else:
        # don't accept future time values or
        # time values more than 60 days in the past
        if measure['time'] > utcnow or measure['time'] < utcmin:
            measure['time'] = utcnow
    # cut down the time to a monthly resolution
    measure['time'] = measure['time'].date().replace(day=1)
    return measure
def normalized_time(time):
    """
    Takes a string representation of a time value, validates and parses
    it and returns a JSON-friendly string representation of the normalized
    time.
    """
    now = util.utcnow()
    if not time:
        time = None

    try:
        time = iso8601.parse_date(time)
    except (iso8601.ParseError, TypeError):
        time = now
    else:
        # don't accept future time values or
        # time values more than 60 days in the past
        min_time = now - timedelta(days=60)
        if time > now or time < min_time:
            time = now
    # cut down the time to a monthly resolution
    time = time.date().replace(day=1)
    return encode_datetime(time)
Exemple #9
0
def decode_datetime(obj):
    try:
        return iso8601.parse_date(obj)
    except (iso8601.ParseError, TypeError):
        return datetime.utcnow().replace(tzinfo=iso8601.UTC)
Exemple #10
0
def decode_datetime(obj):
    try:
        return iso8601.parse_date(obj)
    except (iso8601.ParseError, TypeError):  # pragma: no cover
        return util.utcnow()
Exemple #11
0
def decode_datetime(obj):
    try:
        return iso8601.parse_date(obj)
    except (iso8601.ParseError, TypeError):  # pragma: no cover
        return util.utcnow()
Exemple #12
0
def decode_datetime(obj):
    try:
        return iso8601.parse_date(obj)
    except (iso8601.ParseError, TypeError):
        return datetime.utcnow().replace(tzinfo=iso8601.UTC)