Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol Buffer: How to define Date type?

I'm trynig to write a proto file that has a Date field which is not defined as a type into Protocol buffer.

I have read the following post but I couldn't figure out a proper solution that suits me : What the best ways to use decimals and datetimes with protocol buffers?.

I'm trying to convert the proto file to a java .

like image 663
Echo Avatar asked Oct 30 '11 16:10

Echo


2 Answers

My answer in the linked post relates mainly to protobuf-net; however, since you are coming at this from java I would recommend: keep it simple.

For dates, I would suggest just using the time (perhaps milliseconds) into an epoch (1 Jan 1970 is traditional). For times, just the size in that same unit (milliseconds etc). For decimal, maybe use fixed point simply by scaling - so maybe treat 1.05 as the long 1050 and assert always exactly 3dp (hence fixed point).

This is simple and pragmatic, and covers most common scenarios without making things complicated.

like image 108
Marc Gravell Avatar answered Oct 04 '22 07:10

Marc Gravell


I'm not sold on this idea, but I'm really not sold on the idea of storing dates (which aren't instants in time) as a timestamp, so here's my suggestion.

Convert your date into a human-readable integer (e.g. 2014-11-3 becomes 20141103) and store this integer value. It contains exactly the data you need, is simple to create and parse, and takes up minimal space. Additionally, it is ordered and has a one-to-one mapping of dates to valid values (granted, invalid numbers are possible, such as 20149999, but these are easy to detect). In contrast, there are approximately 86400 valid timestamps that represent each day.

NB: There is a discussion on DBA SE criticizing this method of date storage, but in that context a specialized date type exists, which obviously isn't the case here.

like image 32
dimo414 Avatar answered Oct 04 '22 06:10

dimo414