157d335a2cc604d77f1eab4c25fdce7a42b46880
[anna.git] / include / anna / diameter / codec / basetypes / Time.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_diameter_codec_basetypes_Time_hpp
10 #define anna_diameter_codec_basetypes_Time_hpp
11
12
13 // Local
14 #include <anna/diameter/codec/basetypes/OctetString.hpp>
15
16 #include <anna/config/defines.hpp>
17 //#include <anna.time.Date.h> // TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970
18
19
20 namespace anna {
21
22 namespace diameter {
23
24 namespace codec {
25
26 class Avp;
27
28 namespace basetypes {
29
30 /**
31 * Diameter Time container (NTP timestamp)
32 */
33 class Time : OctetString {
34
35   U32 a_ntpTimestamp; // NTP
36
37
38   // Only for derived diameter type:
39   void updateBasic() throw(anna::RuntimeException);
40   void setPrintableString(const char * printableString) throw(anna::RuntimeException);
41
42
43 public:
44
45
46   // Class-specific ////////////////////////////////////////////////////////////////////////////////////
47   //
48   /**
49   * Type of timestamps (NTP since 0h on 1 January 1900 UTC, UNIX since 0h on 1 January 1970 UTC)
50   */
51   struct Timestamp {
52     enum _v {
53       NTP = 0,
54       UNIX = 1
55     };
56
57     /**
58     * Timestamp type description
59     * @param v Timestamp type
60     * @return Timestamp description
61     */
62     static const char* asText(const Timestamp::_v v) throw() { // anna_declare_enum is not safe, because labels don't have to match a sequence
63       if(v == Timestamp::NTP) return "NTP";
64       else if(v == Timestamp::UNIX) return "UNIX";
65
66       return NULL;
67     }
68   };
69
70   /**
71   * Gets the timestamp value
72   *
73   * @param timestampType Timestamp reference (NTP/Unix)
74   *
75   * @return timestamp value
76   */
77   U32 getTimestamp(Timestamp::_v timestampType = Timestamp::NTP) const throw();
78
79   /**
80   * Sets the timestamp value
81   *
82   * @param timestamp timestamp value
83   * @param timestampType Timestamp reference (NTP/Unix)
84   */
85   void setTimestamp(const U32& timestamp, Timestamp::_v timestampType = Timestamp::NTP) throw();
86   //
87   //////////////////////////////////////////////////////////////////////////////////////////////////////
88
89   // gets
90
91   std::string getFormatName() const throw() { return "Time"; }
92
93
94   // helpers
95
96   std::string asPrintableString() throw(anna::RuntimeException) {
97     return anna::functions::asString(a_ntpTimestamp);
98   }
99
100   std::string asString() throw(anna::RuntimeException);
101
102
103   // sets
104
105   void decode(const char* buffer, const int size) throw(anna::RuntimeException);
106
107
108   // exports /////////////////////////////
109   using AvpData::getSize;
110   using AvpData::code;
111   //using OctetString::asPrintableString;
112   using AvpData::asDataBlockString;
113   //using OctetString::asString;
114   using AvpData::asHexString;
115   //using OctetString::decode;
116   using AvpData::fromPrintableString;
117   using AvpData::fromHexString;
118 };
119
120 }
121 }
122 }
123 }
124
125 #endif
126