def process_timestamp(value, current_datetime=None): if is_float(value): try: value = datetime.fromtimestamp(float(value)) except Exception: raise InvalidTimestamp(EventError.INVALID_DATA) elif not isinstance(value, datetime): # all timestamps are in UTC, but the marker is optional if value.endswith('Z'): value = value[:-1] if '.' in value: # Python doesn't support long microsecond values # https://github.com/getsentry/sentry/issues/1610 ts_bits = value.split('.', 1) value = '%s.%s' % (ts_bits[0], ts_bits[1][:2]) fmt = '%Y-%m-%dT%H:%M:%S.%f' else: fmt = '%Y-%m-%dT%H:%M:%S' try: value = datetime.strptime(value, fmt) except Exception: raise InvalidTimestamp(EventError.INVALID_DATA) if current_datetime is None: current_datetime = datetime.now() if value > current_datetime + ALLOWED_FUTURE_DELTA: raise InvalidTimestamp(EventError.FUTURE_TIMESTAMP) if value < current_datetime - timedelta(days=30): raise InvalidTimestamp(EventError.PAST_TIMESTAMP) return float(value.strftime('%s'))
def process_timestamp(value, current_datetime=None): if is_float(value): try: value = datetime.fromtimestamp(float(value)) except Exception: raise InvalidTimestamp('Invalid value for timestamp: %r' % value) elif not isinstance(value, datetime): # all timestamps are in UTC, but the marker is optional if value.endswith('Z'): value = value[:-1] if '.' in value: # Python doesn't support long microsecond values # https://github.com/getsentry/sentry/issues/1610 ts_bits = value.split('.', 1) value = '%s.%s' % (ts_bits[0], ts_bits[1][:2]) fmt = '%Y-%m-%dT%H:%M:%S.%f' else: fmt = '%Y-%m-%dT%H:%M:%S' try: value = datetime.strptime(value, fmt) except Exception: raise InvalidTimestamp('Invalid value for timestamp: %r' % value) if current_datetime is None: current_datetime = datetime.now() if value > current_datetime + timedelta(minutes=1): raise InvalidTimestamp('Invalid value for timestamp (in future): %r' % value) if value < current_datetime - timedelta(days=30): raise InvalidTimestamp('Invalid value for timestamp (too old): %r' % value) return float(value.strftime('%s'))
def _process_data_timestamp(self, data, current_datetime=None): value = data['timestamp'] if not value: del data['timestamp'] return data elif is_float(value): try: value = datetime.fromtimestamp(float(value)) except Exception: raise InvalidTimestamp( 'Invalid value for timestamp: %r' % data['timestamp']) elif not isinstance(value, datetime): # all timestamps are in UTC, but the marker is optional if value.endswith('Z'): value = value[:-1] if '.' in value: # Python doesn't support long microsecond values # https://github.com/getsentry/sentry/issues/1610 ts_bits = value.split('.', 1) value = '%s.%s' % (ts_bits[0], ts_bits[1][:2]) fmt = '%Y-%m-%dT%H:%M:%S.%f' else: fmt = '%Y-%m-%dT%H:%M:%S' try: value = datetime.strptime(value, fmt) except Exception: raise InvalidTimestamp( 'Invalid value for timestamp: %r' % data['timestamp']) if current_datetime is None: current_datetime = datetime.now() if value > current_datetime + timedelta(minutes=1): raise InvalidTimestamp( 'Invalid value for timestamp (in future): %r' % value) if value < current_datetime - timedelta(days=30): raise InvalidTimestamp( 'Invalid value for timestamp (too old): %r' % value) data['timestamp'] = float(value.strftime('%s')) return data