2013-03-24 55 views
3

我正在使用apache thrift开发服务,我需要定义时间段。日期有效(YYYY-mm-dd),时间应该完全省略(HH:ii:ss)。我找不到任何特定的日期/日期节俭数据类型,所以我在考虑两种方法:如何在apache节俭中表示日期类型

  1. 更复杂

    int year, 
    int month, 
    int day, 
    
  2. 不太复杂,但包括日部分时间,这我不不需要。

    int timestamp 
    

是否有一个共同的储蓄方法来表示日期(时间)类型?

回答

5

我不认为在Thrift IDL上存在日期表示形式。我们在项目中使用这种符号。

typedef string Timestamp; 

然后使用后续模型符号这就需要一个时间戳使用这样

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; 

    } 

字符串使得它更容易使用JODA Operations

- 编辑 -

我不不知道你打算存储什么时间戳。例如,如果要计算当前实例发生的事务并将其存储到该节俭对象中,则可以通过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); 
+0

还没有听说过JODA,但它似乎很有前途。所以我通过节俭传递一个字符串,然后使用JODA库处理它? – ducin 2013-03-26 08:18:49

+0

我编辑了答案,你可以做这样的事情来存储时间戳。 – Venki 2013-03-26 13:38:09

+0

我必须自己尝试,但这个JODA似乎提供了节俭中缺少的功能,谢谢! – ducin 2013-03-26 18:24:59

2

我不知道一些特定的格式首选项。我通常使用十进制编码的日期表示法,因为它非常紧凑且易于理解,如下所示:20130325

+1

嗯,我喜欢这样表示。只是为了确保 - 它是一个整数还是一个字符串? – ducin 2013-03-26 07:55:18

+1

@tkoomzaaskz原因的整数。 'i32'节俭的数据类型。 – Wildfire 2013-03-26 17:06:43

1

您可以创建一个结构来表示你ThriftDate:

struct ThriftDate { 
    1: i32 year; 
    2: i32 month; 
    3: i32 day; 
}