Looking for the best way to send/recieve DateTime objects using GRPC Java

5,774 views
Skip to first unread message

nicholas...@lucidworks.com

unread,
Oct 27, 2017, 9:31:43 PM10/27/17
to grpc.io
I would like to know the best way to send DateTime objects to and from a GRPC service.

Should I just be sending a string to/from GRPC?

I see there is a Timestamp object but it seems to be not what I'm looking for, I think?

Thanks

nicholas...@lucidworks.com

unread,
Oct 27, 2017, 9:43:56 PM10/27/17
to grpc.io
LocalDateTime ldt = the date time object;
Timestamp.newBuilder().setSeconds(ldt.getSecond())
.setNanos(ldt.getNano()))
.build()


Would that work?

nevil...@gmail.com

unread,
Oct 29, 2017, 5:14:12 AM10/29/17
to grpc.io
Hey,

have you cnsidered sending milliseconds since Epoch?

Extract the time from a Date object: Long dt = new Date().getTime();

I haven't used the Timestamp message, so I don't know how to use that.

Carl Mastrangelo

unread,
Oct 30, 2017, 8:18:41 PM10/30/17
to grpc.io
If you are using Protobuf, there is the  google.protobuf.Timestamp message type that you can use to send it.

Evan Jones

unread,
Oct 31, 2017, 10:23:17 AM10/31/17
to grpc.io
I second the recommendation for using Timestamp. If you interact with other languages, some of the libraries provide some some nice helper methods to make working with it fairly straightforward. If you use any Google Cloud APIs, you will end up dealing with Timestamps at some point, so you might as well have fewer unique time types in your system.

Nicholas DiPiazza

unread,
Oct 31, 2017, 12:24:25 PM10/31/17
to Evan Jones, grpc.io
Is this code snippet ok?

LocalDateTime ldt = the date time object we want to send over grpc;

Timestamp.newBuilder().setSeconds(ldt.getSecond())
      .setNanos(ldt.getNano()))
    .build()

Is there a helper method to make it easier?


--
You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/R9_oOEeEWsc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/13a1b845-d921-4323-bc3a-4c84df999d39%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Carl Mastrangelo

unread,
Oct 31, 2017, 1:25:16 PM10/31/17
to grpc.io
It looks like you are using Java.  In that case, Protobuf provides a helper library to create these, such as fromMillies:




On Tuesday, October 31, 2017 at 9:24:25 AM UTC-7, Nicholas DiPiazza wrote:
Is this code snippet ok?

LocalDateTime ldt = the date time object we want to send over grpc;

Timestamp.newBuilder().setSeconds(ldt.getSecond())
      .setNanos(ldt.getNano()))
    .build()

Is there a helper method to make it easier?

On Tue, Oct 31, 2017 at 9:23 AM, Evan Jones <evan....@bluecore.com> wrote:
I second the recommendation for using Timestamp. If you interact with other languages, some of the libraries provide some some nice helper methods to make working with it fairly straightforward. If you use any Google Cloud APIs, you will end up dealing with Timestamps at some point, so you might as well have fewer unique time types in your system.


On Monday, October 30, 2017 at 8:18:41 PM UTC-4, Carl Mastrangelo wrote:
If you are using Protobuf, there is the  google.protobuf.Timestamp message type that you can use to send it.



On Friday, October 27, 2017 at 6:31:43 PM UTC-7, nicholas...@lucidworks.com wrote:
I would like to know the best way to send DateTime objects to and from a GRPC service.

Should I just be sending a string to/from GRPC?

I see there is a Timestamp object but it seems to be not what I'm looking for, I think?

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/R9_oOEeEWsc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grpc-io+u...@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

Evan Jones

unread,
Oct 31, 2017, 2:00:54 PM10/31/17
to Nicholas DiPiazza, grpc.io
A LocalDateTime does not have a time zone, and getSecond() only returns the seconds part of the "human broken down time", so it isn't the same. A Timestamp represents an instant in time, and the seconds part is seconds since the epoch. You probably need something like (I did not even attempt to compile this):

long epochSeconds = ldt.toEpochSeconds(ZoneOffset.UTC);

I believe the nanoseconds part looks correct. If you just want the current time, you can use:


timestamp = Timestamps.fromMillis(System.currentTimeMillis());



Hope that helps!

Evan


Selcuk Bozdag

unread,
Oct 31, 2017, 2:13:08 PM10/31/17
to grpc.io
Hi,

You can use epoch milliseconds. If you're working with LocalDateTime for instance:

localDateTime.atZone(ZoneId.systemDefault()) .toInstant().toEpochMilli()

would get you something you need I think.

In proto file define it as a long data. Hope it helps.

Nicholas DiPiazza

unread,
Oct 31, 2017, 2:38:49 PM10/31/17
to Selcuk Bozdag, grpc.io

Great!!! That’s what I was looking for. Thanks for spelling it out to me it was not at all obvious what ZoneId to use

--

You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/R9_oOEeEWsc/unsubscribe.

To unsubscribe from this group and all its topics, send an email to grpc-io+u...@googlegroups.com.


To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

Reply all
Reply to author
Forward
0 new messages