Python google.protobuf.timestamp_pb2.Timestamp() Examples

The following are 30 code examples of google.protobuf.timestamp_pb2.Timestamp(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.protobuf.timestamp_pb2 , or try the search function .
Example #1
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_remove_reservation_failures(get_reservation, get_processor):
    name = 'projects/proj/processors/p0/reservations/rid'
    now = int(datetime.datetime.now().timestamp())
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=now + 10),
        end_time=Timestamp(seconds=now + 3610),
        whitelisted_users=['dstrain@google.com'],
    )
    get_reservation.return_value = result
    get_processor.return_value = None

    # no processor
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())
    with pytest.raises(ValueError):
        processor.remove_reservation('rid')

    # No freeze period defined
    processor = cg.EngineProcessor('proj', 'p0', EngineContext(),
                                   qtypes.QuantumProcessor())
    with pytest.raises(ValueError):
        processor.remove_reservation('rid') 
Example #2
Source File: engine_timeslot_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_from_to_proto_plain():
    slot = enums.QuantumTimeSlot.TimeSlotType.RESERVATION
    proto = qtypes.QuantumTimeSlot(
        processor_name='potofgold',
        start_time=Timestamp(seconds=1500000000),
        end_time=Timestamp(seconds=1500010000),
        slot_type=slot,
    )
    time_slot = cg.EngineTimeSlot(
        processor_id='potofgold',
        start_time=datetime.datetime.fromtimestamp(1500000000),
        end_time=datetime.datetime.fromtimestamp(1500010000),
        slot_type=slot,
    )
    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
    assert actual_from_proto == time_slot
    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
    assert actual_to_proto == proto 
Example #3
Source File: engine_timeslot_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_from_to_proto_reservation():
    slot = enums.QuantumTimeSlot.TimeSlotType.RESERVATION
    proto = qtypes.QuantumTimeSlot(
        processor_name='potofgold',
        start_time=Timestamp(seconds=1500000000),
        end_time=Timestamp(seconds=1500010000),
        slot_type=slot,
        reservation_config=qtypes.QuantumTimeSlot.ReservationConfig(
            project_id='super_secret_quantum'),
    )
    time_slot = cg.EngineTimeSlot(
        processor_id='potofgold',
        start_time=datetime.datetime.fromtimestamp(1500000000),
        end_time=datetime.datetime.fromtimestamp(1500010000),
        slot_type=slot,
        project_id='super_secret_quantum',
    )
    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
    assert actual_from_proto == time_slot
    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
    assert actual_to_proto == proto 
Example #4
Source File: engine_timeslot_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_from_to_proto_maintenance():
    slot = enums.QuantumTimeSlot.TimeSlotType.MAINTENANCE
    proto = qtypes.QuantumTimeSlot(
        processor_name='potofgold',
        start_time=Timestamp(seconds=1500020000),
        end_time=Timestamp(seconds=1500040000),
        slot_type=slot,
        maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig(
            title='Testing',
            description='Testing some new configuration.',
        ),
    )
    time_slot = cg.EngineTimeSlot(
        processor_id='potofgold',
        start_time=datetime.datetime.fromtimestamp(1500020000),
        end_time=datetime.datetime.fromtimestamp(1500040000),
        slot_type=slot,
        maintenance_title='Testing',
        maintenance_description='Testing some new configuration.',
    )
    actual_from_proto = cg.EngineTimeSlot.from_proto(proto)
    assert actual_from_proto == time_slot
    actual_to_proto = cg.EngineTimeSlot.to_proto(time_slot)
    assert actual_to_proto == proto 
Example #5
Source File: engine_client_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_get_reservation(client_constructor):
    grpc_client = setup_mock_(client_constructor)
    name = 'projects/proj/processors/processor0/reservations/papar-party-44'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000002000),
        whitelisted_users=['jeff@google.com'],
    )
    grpc_client.get_quantum_reservation.return_value = result

    client = EngineClient()
    assert (client.get_reservation('proj', 'processor0',
                                   'papar-party-44') == result)
    kwargs = grpc_client.get_quantum_reservation.call_args[1]
    assert kwargs == {
        'name': name,
    } 
Example #6
Source File: engine_client_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_delete_reservation(client_constructor):
    grpc_client = setup_mock_(client_constructor)
    name = 'projects/proj/processors/processor0/reservations/papar-party-44'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000002000),
        whitelisted_users=['jeff@google.com'],
    )
    grpc_client.delete_quantum_reservation.return_value = result

    client = EngineClient()
    assert (client.delete_reservation('proj', 'processor0',
                                      'papar-party-44') == result)
    kwargs = grpc_client.delete_quantum_reservation.call_args[1]
    assert kwargs == {
        'name': name,
    } 
Example #7
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_create_reservation(create_reservation):
    name = 'projects/proj/processors/p0/reservations/psherman-wallaby-way'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000003600),
        whitelisted_users=['dstrain@google.com'],
    )
    create_reservation.return_value = result
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())
    assert processor.create_reservation(
        datetime.datetime.fromtimestamp(1000000000),
        datetime.datetime.fromtimestamp(1000003600), ['dstrain@google.com'])
    create_reservation.assert_called_once_with(
        'proj', 'p0', datetime.datetime.fromtimestamp(1000000000),
        datetime.datetime.fromtimestamp(1000003600), ['dstrain@google.com']) 
Example #8
Source File: engine_client_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_cancel_reservation(client_constructor):
    grpc_client = setup_mock_(client_constructor)
    name = 'projects/proj/processors/processor0/reservations/papar-party-44'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000002000),
        whitelisted_users=['jeff@google.com'],
    )
    grpc_client.cancel_quantum_reservation.return_value = result

    client = EngineClient()
    assert (client.cancel_reservation('proj', 'processor0',
                                      'papar-party-44') == result)
    kwargs = grpc_client.cancel_quantum_reservation.call_args[1]
    assert kwargs == {
        'name': name,
    } 
Example #9
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_remove_reservation_delete(delete_reservation, get_reservation):
    name = 'projects/proj/processors/p0/reservations/rid'
    now = int(datetime.datetime.now().timestamp())
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=now + 20000),
        end_time=Timestamp(seconds=now + 23610),
        whitelisted_users=['dstrain@google.com'],
    )
    get_reservation.return_value = result
    delete_reservation.return_value = result
    processor = cg.EngineProcessor(
        'proj', 'p0', EngineContext(),
        qtypes.QuantumProcessor(schedule_frozen_period=Duration(seconds=10000)))
    assert processor.remove_reservation('rid') == result
    delete_reservation.assert_called_once_with('proj', 'p0', 'rid') 
Example #10
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_remove_reservation_cancel(cancel_reservation, get_reservation):
    name = 'projects/proj/processors/p0/reservations/rid'
    now = int(datetime.datetime.now().timestamp())
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=now + 10),
        end_time=Timestamp(seconds=now + 3610),
        whitelisted_users=['dstrain@google.com'],
    )
    get_reservation.return_value = result
    cancel_reservation.return_value = result
    processor = cg.EngineProcessor(
        'proj', 'p0', EngineContext(),
        qtypes.QuantumProcessor(schedule_frozen_period=Duration(seconds=10000)))
    assert processor.remove_reservation('rid') == result
    cancel_reservation.assert_called_once_with('proj', 'p0', 'rid') 
Example #11
Source File: engine_client_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_create_reservation(client_constructor):
    grpc_client = setup_mock_(client_constructor)
    start = datetime.datetime.fromtimestamp(1000000000)
    end = datetime.datetime.fromtimestamp(1000003600)
    users = ['jeff@google.com']
    result = qtypes.QuantumReservation(
        name='projects/proj/processors/processor0/reservations/papar-party-44',
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000003600),
        whitelisted_users=users,
    )
    grpc_client.create_quantum_reservation.return_value = result

    client = EngineClient()
    assert client.create_reservation('proj', 'processor0', start, end,
                                     users) == result
    assert grpc_client.create_quantum_reservation.call_count == 1
    kwargs = grpc_client.create_quantum_reservation.call_args[1]
    # The outgoing argument will not have the resource name
    result.name = ''
    assert kwargs == {
        'parent': 'projects/proj/processors/processor0',
        'quantum_reservation': result
    } 
Example #12
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_update_reservation(update_reservation):
    name = 'projects/proj/processors/p0/reservations/rid'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000003600),
        whitelisted_users=['dstrain@google.com'],
    )
    start = datetime.datetime.fromtimestamp(1000000000)
    end = datetime.datetime.fromtimestamp(1000003600)
    update_reservation.return_value = result
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())
    assert processor.update_reservation('rid', start, end,
                                        ['dstrain@google.com']) == result
    update_reservation.assert_called_once_with(
        'proj',
        'p0',
        'rid',
        start=start,
        end=end,
        whitelisted_users=['dstrain@google.com']) 
Example #13
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 6 votes vote down vote up
def test_get_schedule_filter_by_time_slot(list_time_slots):
    results = [
        qtypes.QuantumTimeSlot(
            processor_name='potofgold',
            start_time=Timestamp(seconds=1000020000),
            end_time=Timestamp(seconds=1000040000),
            slot_type=qenums.QuantumTimeSlot.TimeSlotType.MAINTENANCE,
            maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig(
                title='Testing',
                description='Testing some new configuration.',
            ),
        )
    ]
    list_time_slots.return_value = results
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())

    assert processor.get_schedule(
        datetime.datetime.fromtimestamp(1000000000),
        datetime.datetime.fromtimestamp(1000050000),
        qenums.QuantumTimeSlot.TimeSlotType.MAINTENANCE) == results
    list_time_slots.assert_called_once_with(
        'proj', 'p0', 'start_time < 1000050000 AND end_time > 1000000000 AND ' +
        'time_slot_type = MAINTENANCE') 
Example #14
Source File: bqh.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _to_bq_value(value, field_desc):
  if field_desc.enum_type:
    # Enums are stored as strings.
    enum_val = field_desc.enum_type.values_by_number.get(value)
    if not enum_val:
      raise ValueError('Invalid value %r for enum type %s' % (
          value, field_desc.enum_type.full_name))
    return enum_val.name
  elif isinstance(value, duration_pb2.Duration):
    return value.ToTimedelta().total_seconds()
  elif isinstance(value, struct_pb2.Struct):
    # Structs are stored as JSONPB strings,
    # see https://bit.ly/chromium-bq-struct
    return json_format.MessageToJson(value)
  elif isinstance(value, timestamp_pb2.Timestamp):
    return value.ToDatetime().isoformat()
  elif isinstance(value, message_pb.Message):
    return message_to_dict(value)
  else:
    return value 
Example #15
Source File: bqh.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _to_bq_value(value, field_desc):
  if field_desc.enum_type:
    # Enums are stored as strings.
    enum_val = field_desc.enum_type.values_by_number.get(value)
    if not enum_val:
      raise ValueError('Invalid value %r for enum type %s' % (
          value, field_desc.enum_type.full_name))
    return enum_val.name
  elif isinstance(value, duration_pb2.Duration):
    return value.ToTimedelta().total_seconds()
  elif isinstance(value, struct_pb2.Struct):
    # Structs are stored as JSONPB strings,
    # see https://bit.ly/chromium-bq-struct
    return json_format.MessageToJson(value)
  elif isinstance(value, timestamp_pb2.Timestamp):
    return value.ToDatetime().isoformat()
  elif isinstance(value, message_pb.Message):
    return message_to_dict(value)
  else:
    return value 
Example #16
Source File: bqh.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _to_bq_value(value, field_desc):
  if field_desc.enum_type:
    # Enums are stored as strings.
    enum_val = field_desc.enum_type.values_by_number.get(value)
    if not enum_val:
      raise ValueError('Invalid value %r for enum type %s' % (
          value, field_desc.enum_type.full_name))
    return enum_val.name
  elif isinstance(value, duration_pb2.Duration):
    return value.ToTimedelta().total_seconds()
  elif isinstance(value, struct_pb2.Struct):
    # Structs are stored as JSONPB strings,
    # see https://bit.ly/chromium-bq-struct
    return json_format.MessageToJson(value)
  elif isinstance(value, timestamp_pb2.Timestamp):
    return value.ToDatetime().isoformat()
  elif isinstance(value, message_pb.Message):
    return message_to_dict(value)
  else:
    return value 
Example #17
Source File: test_message.py    From python-pubsub with Apache License 2.0 5 votes vote down vote up
def create_message(data, ack_id="ACKID", delivery_attempt=0, ordering_key="", **attrs):
    with mock.patch.object(time, "time") as time_:
        time_.return_value = RECEIVED_SECONDS
        msg = message.Message(
            message=types.PubsubMessage(
                attributes=attrs,
                data=data,
                message_id="message_id",
                publish_time=timestamp_pb2.Timestamp(
                    seconds=PUBLISHED_SECONDS, nanos=PUBLISHED_MICROS * 1000
                ),
                ordering_key=ordering_key,
            ),
            ack_id=ack_id,
            delivery_attempt=delivery_attempt,
            request_queue=queue.Queue(),
        )
        return msg 
Example #18
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def test_get_schedule(list_time_slots):
    results = [
        qtypes.QuantumTimeSlot(
            processor_name='potofgold',
            start_time=Timestamp(seconds=1000020000),
            end_time=Timestamp(seconds=1000040000),
            slot_type=qenums.QuantumTimeSlot.TimeSlotType.MAINTENANCE,
            maintenance_config=qtypes.QuantumTimeSlot.MaintenanceConfig(
                title='Testing',
                description='Testing some new configuration.',
            ),
        ),
        qtypes.QuantumTimeSlot(
            processor_name='potofgold',
            start_time=Timestamp(seconds=1000010000),
            end_time=Timestamp(seconds=1000020000),
            slot_type=qenums.QuantumTimeSlot.TimeSlotType.RESERVATION,
            reservation_config=qtypes.QuantumTimeSlot.ReservationConfig(
                project_id='super_secret_quantum'),
        )
    ]
    list_time_slots.return_value = results
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())
    assert processor.get_schedule(
        datetime.datetime.fromtimestamp(1000000000),
        datetime.datetime.fromtimestamp(1000050000)) == results
    list_time_slots.assert_called_once_with(
        'proj', 'p0', 'start_time < 1000050000 AND end_time > 1000000000') 
Example #19
Source File: well_known_types_test.py    From go2mapillary with GNU General Public License v3.0 5 votes vote down vote up
def testInvalidTimestamp(self):
    message = timestamp_pb2.Timestamp()
    self.assertRaisesRegexp(
        ValueError,
        'time data \'10000-01-01T00:00:00\' does not match'
        ' format \'%Y-%m-%dT%H:%M:%S\'',
        message.FromJsonString, '10000-01-01T00:00:00.00Z')
    self.assertRaisesRegexp(
        well_known_types.ParseError,
        'nanos 0123456789012 more than 9 fractional digits.',
        message.FromJsonString,
        '1970-01-01T00:00:00.0123456789012Z')
    self.assertRaisesRegexp(
        well_known_types.ParseError,
        (r'Invalid timezone offset value: \+08.'),
        message.FromJsonString,
        '1972-01-01T01:00:00.01+08',)
    self.assertRaisesRegexp(
        ValueError,
        'year is out of range',
        message.FromJsonString,
        '0000-01-01T00:00:00Z')
    message.seconds = 253402300800
    self.assertRaisesRegexp(
        OverflowError,
        'date value out of range',
        message.ToJsonString) 
Example #20
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def test_delete_reservation(delete_reservation):
    name = 'projects/proj/processors/p0/reservations/rid'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000003600),
        whitelisted_users=['dstrain@google.com'],
    )
    delete_reservation.return_value = result
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())
    assert processor._delete_reservation('rid') == result
    delete_reservation.assert_called_once_with('proj', 'p0', 'rid') 
Example #21
Source File: engine_processor_test.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def test_get_reservation(get_reservation):
    name = 'projects/proj/processors/p0/reservations/rid'
    result = qtypes.QuantumReservation(
        name=name,
        start_time=Timestamp(seconds=1000000000),
        end_time=Timestamp(seconds=1000003600),
        whitelisted_users=['dstrain@google.com'],
    )
    get_reservation.return_value = result
    processor = cg.EngineProcessor('proj', 'p0', EngineContext())
    assert processor.get_reservation('rid') == result
    get_reservation.assert_called_once_with('proj', 'p0', 'rid') 
Example #22
Source File: well_known_types_test.py    From go2mapillary with GNU General Public License v3.0 5 votes vote down vote up
def testDatetimeConverison(self):
    message = timestamp_pb2.Timestamp()
    dt = datetime(1970, 1, 1)
    message.FromDatetime(dt)
    self.assertEqual(dt, message.ToDatetime())

    message.FromMilliseconds(1999)
    self.assertEqual(datetime(1970, 1, 1, 0, 0, 1, 999000),
                     message.ToDatetime()) 
Example #23
Source File: well_known_types_test.py    From go2mapillary with GNU General Public License v3.0 5 votes vote down vote up
def testTimestampSerializeAndParse(self):
    message = timestamp_pb2.Timestamp()
    # Generated output should contain 3, 6, or 9 fractional digits.
    message.seconds = 0
    message.nanos = 0
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00Z')
    message.nanos = 10000000
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00.010Z')
    message.nanos = 10000
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00.000010Z')
    message.nanos = 10
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00.000000010Z')
    # Test min timestamps.
    message.seconds = -62135596800
    message.nanos = 0
    self.CheckTimestampConversion(message, '0001-01-01T00:00:00Z')
    # Test max timestamps.
    message.seconds = 253402300799
    message.nanos = 999999999
    self.CheckTimestampConversion(message, '9999-12-31T23:59:59.999999999Z')
    # Test negative timestamps.
    message.seconds = -1
    self.CheckTimestampConversion(message, '1969-12-31T23:59:59.999999999Z')

    # Parsing accepts an fractional digits as long as they fit into nano
    # precision.
    message.FromJsonString('1970-01-01T00:00:00.1Z')
    self.assertEqual(0, message.seconds)
    self.assertEqual(100000000, message.nanos)
    # Parsing accpets offsets.
    message.FromJsonString('1970-01-01T00:00:00-08:00')
    self.assertEqual(8 * 3600, message.seconds)
    self.assertEqual(0, message.nanos) 
Example #24
Source File: well_known_types_test.py    From go2mapillary with GNU General Public License v3.0 5 votes vote down vote up
def CheckTimestampConversion(self, message, text):
    self.assertEqual(text, message.ToJsonString())
    parsed_message = timestamp_pb2.Timestamp()
    parsed_message.FromJsonString(text)
    self.assertEqual(message, parsed_message) 
Example #25
Source File: test_system.py    From python-firestore with Apache License 2.0 5 votes vote down vote up
def test_document_delete(client, cleanup):
    document_id = "deleted" + UNIQUE_RESOURCE_ID
    document = client.document("here-to-be", document_id)
    # Add to clean-up before API request (in case ``create()`` fails).
    cleanup(document.delete)
    document.create({"not": "much"})

    # 1. Call ``delete()`` with invalid (in the past) "last timestamp" option.
    snapshot1 = document.get()
    timestamp_pb = timestamp_pb2.Timestamp(
        seconds=snapshot1.update_time.nanos - 3600, nanos=snapshot1.update_time.nanos
    )
    option1 = client.write_option(last_update_time=timestamp_pb)
    with pytest.raises(FailedPrecondition):
        document.delete(option=option1)

    # 2. Call ``delete()`` with invalid (in future) "last timestamp" option.
    timestamp_pb = timestamp_pb2.Timestamp(
        seconds=snapshot1.update_time.nanos + 3600, nanos=snapshot1.update_time.nanos
    )
    option2 = client.write_option(last_update_time=timestamp_pb)
    with pytest.raises(FailedPrecondition):
        document.delete(option=option2)

    # 3. Actually ``delete()`` the document.
    delete_time3 = document.delete()

    # 4. ``delete()`` again, even though we know the document is gone.
    delete_time4 = document.delete()
    assert_timestamp_less(delete_time3, delete_time4) 
Example #26
Source File: well_known_types_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testInvalidTimestamp(self):
    message = timestamp_pb2.Timestamp()
    self.assertRaisesRegexp(
        ValueError,
        'time data \'10000-01-01T00:00:00\' does not match'
        ' format \'%Y-%m-%dT%H:%M:%S\'',
        message.FromJsonString, '10000-01-01T00:00:00.00Z')
    self.assertRaisesRegexp(
        well_known_types.ParseError,
        'nanos 0123456789012 more than 9 fractional digits.',
        message.FromJsonString,
        '1970-01-01T00:00:00.0123456789012Z')
    self.assertRaisesRegexp(
        well_known_types.ParseError,
        (r'Invalid timezone offset value: \+08.'),
        message.FromJsonString,
        '1972-01-01T01:00:00.01+08',)
    self.assertRaisesRegexp(
        ValueError,
        'year is out of range',
        message.FromJsonString,
        '0000-01-01T00:00:00Z')
    message.seconds = 253402300800
    self.assertRaisesRegexp(
        OverflowError,
        'date value out of range',
        message.ToJsonString) 
Example #27
Source File: well_known_types_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testDatetimeConverison(self):
    message = timestamp_pb2.Timestamp()
    dt = datetime(1970, 1, 1)
    message.FromDatetime(dt)
    self.assertEqual(dt, message.ToDatetime())

    message.FromMilliseconds(1999)
    self.assertEqual(datetime(1970, 1, 1, 0, 0, 1, 999000),
                     message.ToDatetime()) 
Example #28
Source File: well_known_types_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testTimestampSerializeAndParse(self):
    message = timestamp_pb2.Timestamp()
    # Generated output should contain 3, 6, or 9 fractional digits.
    message.seconds = 0
    message.nanos = 0
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00Z')
    message.nanos = 10000000
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00.010Z')
    message.nanos = 10000
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00.000010Z')
    message.nanos = 10
    self.CheckTimestampConversion(message, '1970-01-01T00:00:00.000000010Z')
    # Test min timestamps.
    message.seconds = -62135596800
    message.nanos = 0
    self.CheckTimestampConversion(message, '0001-01-01T00:00:00Z')
    # Test max timestamps.
    message.seconds = 253402300799
    message.nanos = 999999999
    self.CheckTimestampConversion(message, '9999-12-31T23:59:59.999999999Z')
    # Test negative timestamps.
    message.seconds = -1
    self.CheckTimestampConversion(message, '1969-12-31T23:59:59.999999999Z')

    # Parsing accepts an fractional digits as long as they fit into nano
    # precision.
    message.FromJsonString('1970-01-01T00:00:00.1Z')
    self.assertEqual(0, message.seconds)
    self.assertEqual(100000000, message.nanos)
    # Parsing accpets offsets.
    message.FromJsonString('1970-01-01T00:00:00-08:00')
    self.assertEqual(8 * 3600, message.seconds)
    self.assertEqual(0, message.nanos) 
Example #29
Source File: http_converter.py    From lightstep-tracer-python with MIT License 5 votes vote down vote up
def create_span_record(self, span, guid):
        span_context = SpanContext(trace_id=span.context.trace_id,
                                   span_id=span.context.span_id)
        seconds, nanos = util._time_to_seconds_nanos(span.start_time)
        span_record = Span(span_context=span_context,
                           operation_name=util._coerce_str(span.operation_name),
                           start_timestamp=Timestamp(seconds=seconds, nanos=nanos),
                           duration_micros=int(util._time_to_micros(span.duration)))
        if span.parent_id is not None:
            reference = span_record.references.add()
            reference.relationship=Reference.CHILD_OF
            reference.span_context.span_id=span.parent_id

        return span_record 
Example #30
Source File: well_known_types_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def CheckTimestampConversion(self, message, text):
    self.assertEqual(text, message.ToJsonString())
    parsed_message = timestamp_pb2.Timestamp()
    parsed_message.FromJsonString(text)
    self.assertEqual(message, parsed_message)