How to set google.protobuf.Timestamp to null?
Asked Answered
U

3

6

How to represent a null timestamp in Google ProtoBuf?

I am reading DateTime's from a postgreSQL database--some of which are null--into protobuf TimeStamps.

message test {
  google.protobuf.TimestampValue TransactionTime =1;
}

Unfortunately, there is no such animal as google.protobuf.TimestampValue.

Any help is most appreciated.

TIA

Ulland answered 12/5, 2022 at 3:35 Comment(4)
It should be google.protobuf.Timestamp, please check this link: #3575216Tiaratibbetts
@Tiaratibbetts Yes...but how to handle null value for Timestamp?Ulland
thats interesting, I think you'll have to handle this logic in your application like: instead of using timestamp you can use string and then convert it to timestamp at application end and when the value is null just send empty string ''Tiaratibbetts
@Tiaratibbetts is correct. IIUC you cannot use null. Protobuf messages are either present (possibly default) valued or optional but they can't be null.Kingcraft
M
6

As @JayantSeth pointed out, you should use google.protobuf.Timestamp.

Since Protobuf forbids to set field to null, you could use a default value to present null in your application. There are two fields in message Timestamp:

message Timestamp {
  // Represents seconds of UTC time since Unix epoch
  // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
  // 9999-12-31T23:59:59Z inclusive.
  int64 seconds = 1;

  // Non-negative fractions of a second at nanosecond resolution. Negative
  // second values with fractions must still have non-negative nanos values
  // that count forward in time. Must be from 0 to 999,999,999
  // inclusive.
  int32 nanos = 2;
}

The default value of type int64 and int32 both are 0. Therefore, if some value read from Postgres is null, then you could set it to 0.

Timestamp timestamp;
timestamp.set_seconds(0);
timestamp.set_nanos(0);

And in your application, you could treat timestamp(0) as null.

Mountainous answered 18/5, 2022 at 14:51 Comment(2)
Great answer. However, one of my timestamps is for a person's birthdate. How would I manage a birthdate of 1970-01-01 (which I actually do have) ? Thanks.Ulland
Setting timestamp to default value(e.g. 0) is one way to go, you could also set timestamp to another value to represent null, which depends on application logic. In your case, you are using timestamp to handle birthdate(I guess), you could use max value of int32 as default value, which is 2038-01-19(of course it still depends on your application to check if it's acceptable)Mountainous
W
1

I agree with @ramsay's answer. In Python, a good way to check for null would be to check either the seconds or nanoseconds field as such:

from google.protobuf import timestamp_pb2
timestamp = timestamp_pb2.Timestamp()
 
if timestamp.seconds:
    # A valid non-null date was passed
else:
    # The date field was left empty; act accordingly.
Waggoner answered 16/11, 2022 at 23:32 Comment(0)
C
1

Just omit sending the value. Then you can use hasTransactionTime to check if it was set. Fields that are messages like timestamp can be omitted and that can be detected in code.

Use this proto:

message test {
  google.protobuf.Timestamp TransactionTime = 1;
}
Chester answered 2/6, 2023 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.