Пример #1
0
def process_data_timestamp(data, current_datetime=None):
    if is_float(data['timestamp']):
        try:
            data['timestamp'] = datetime.fromtimestamp(float(
                data['timestamp']))
        except Exception:
            raise InvalidTimestamp('Invalid value for timestamp: %r' %
                                   data['timestamp'])
    elif not isinstance(data['timestamp'], datetime):
        if '.' in data['timestamp']:
            format = '%Y-%m-%dT%H:%M:%S.%f'
        else:
            format = '%Y-%m-%dT%H:%M:%S'
        if 'Z' in data['timestamp']:
            # support UTC market, but not other timestamps
            format += 'Z'
        try:
            data['timestamp'] = datetime.strptime(data['timestamp'], format)
        except Exception:
            raise InvalidTimestamp('Invalid value for timestamp: %r' %
                                   data['timestamp'])

    if current_datetime is None:
        current_datetime = datetime.now()

    if data['timestamp'] > current_datetime + timedelta(minutes=1):
        raise InvalidTimestamp('Invalid value for timestamp (in future): %r' %
                               data['timestamp'])

    if data['timestamp'] < current_datetime - timedelta(days=30):
        raise InvalidTimestamp('Invalid value for timestamp (too old): %r' %
                               data['timestamp'])

    return data
Пример #2
0
def process_data_timestamp(data):
    if is_float(data['timestamp']):
        try:
            data['timestamp'] = datetime.fromtimestamp(float(
                data['timestamp']))
        except Exception:
            logger.exception('Failed reading timestamp')
            del data['timestamp']
    elif not isinstance(data['timestamp'], datetime):
        if '.' in data['timestamp']:
            format = '%Y-%m-%dT%H:%M:%S.%f'
        else:
            format = '%Y-%m-%dT%H:%M:%S'
        if 'Z' in data['timestamp']:
            # support UTC market, but not other timestamps
            format += 'Z'
        try:
            data['timestamp'] = datetime.strptime(data['timestamp'], format)
        except Exception:
            raise InvalidTimestamp('Invalid value for timestamp: %r' %
                                   data['timestamp'])

    return data