7

I'm developing a service using apache thrift and I need to define periods of time. Dates are significant (YYYY-mm-dd) and time should be totally omitted (HH:ii:ss). I couldn't find any specific date/datetime thrift data type so I'm thinking about two approaches:

  1. more complex

    int year,
    int month,
    int day,
    
  2. less complex but includes time of day part which I don't need.

    int timestamp
    

Is there a common thrift approach to represent date(time) types?

ducin
  • 25,621
  • 41
  • 157
  • 256

3 Answers3

7

I don't think there is a date representation on Thrift IDL. We use this notation for our projects.

typedef string Timestamp

then use that notation on subsequent model which needs a timestamp usage like this

struct blah{

    /**
    * TODO:list what notation this dateTime represents. eg ISO-8601
    * or if its in the format like YYYY-mm-DD you mentioned.
    */
    1:Timestamp dateTime;

    }

String makes it easier to use JODA Operations

--EDIT--

I don't know what timestamp you intend to store. For instance if you want to calculate current instance a transaction has occurred and store it into that thrift object, you can do this with Joda.

    String timestamp = new DateTime().toString("YYYY-MM-dd"); //2013-03-26 This will be string value generated. It will convert the current time to format you seek to output.
    //Use the generated thrift object.
    Blah newblah = new Blah();
    blah.setDateTime(timestamp);
Aides
  • 3,643
  • 5
  • 23
  • 39
Venki
  • 1,419
  • 5
  • 28
  • 38
  • Haven't heard about JODA, but it seems promising. So I pass just a string through thrift and then process it using JODA libraries? – ducin Mar 26 '13 at 08:18
  • i have edited the answer you can do something like that to store the timestamp. – Venki Mar 26 '13 at 13:38
  • I'll have to try it myself, but this JODA seems to provide features missing in thrift, thank you! – ducin Mar 26 '13 at 18:24
3

I'm not aware of some specific format preference. I usually use decimal encoded date representation, since it's quite compact and human-readable, like this: 20130325

Wildfire
  • 6,358
  • 2
  • 34
  • 50
1

You could create a structure to represent you ThriftDate:

struct ThriftDate {
  1: i32 year;
  2: i32 month;
  3: i32 day;
}
Sergey T
  • 161
  • 1
  • 6