def test_from_utc_datetime(self): self.assertEqual( Timestamp.from_utc_datetime(datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)), Timestamp(0)) with self.assertRaisesRegex(ValueError, r'UTC'): Timestamp.from_utc_datetime(datetime.datetime(1970, 1, 1))
def test_from_utc_datetime(self): self.assertEqual( Timestamp.from_utc_datetime(datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)), Timestamp(0)) with self.assertRaisesRegexp(ValueError, r'UTC'): Timestamp.from_utc_datetime(datetime.datetime(1970, 1, 1))
def process(self, element): """ Parse the timestamp and add it to the datapoints """ t = element[1][timestamp_column][0] timestamp = datetime.fromtimestamp(t, pytz.utc) unix_timestamp = Timestamp.from_utc_datetime(timestamp) yield TimestampedValue(element, unix_timestamp)
def _get_element(message): parsed_message = PubsubMessage._from_message(message) if (timestamp_attribute and timestamp_attribute in parsed_message.attributes): rfc3339_or_milli = parsed_message.attributes[timestamp_attribute] try: timestamp = Timestamp(micros=int(rfc3339_or_milli) * 1000) except ValueError: try: timestamp = Timestamp.from_rfc3339(rfc3339_or_milli) except ValueError as e: raise ValueError('Bad timestamp value: %s' % e) else: if message.publish_time is None: raise ValueError('No publish time present in message: %s' % message) timestamp = Timestamp.from_utc_datetime(message.publish_time) return timestamp, parsed_message