1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
10 #include <anna/diameter/codec/basetypes/Time.hpp>
12 #include <anna/time/Date.hpp> // TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970
15 //------------------------------------------------------------------------------
16 //---------------------------------------------------------- Time::updateBasic()
17 //------------------------------------------------------------------------------
18 void anna::diameter::codec::basetypes::Time::updateBasic() throw(anna::RuntimeException) {
20 result += (char)(a_ntpTimestamp >> 24);
21 result += (char)(a_ntpTimestamp >> 16);
22 result += (char)(a_ntpTimestamp >> 8);
23 result += (char)a_ntpTimestamp;
24 OctetString::setValue(result);
28 //------------------------------------------------------------------------------
29 //--------------------------------------------------- Time::setPrintableString()
30 //------------------------------------------------------------------------------
31 void anna::diameter::codec::basetypes::Time::setPrintableString(const char * printableString) throw(anna::RuntimeException) {
32 setTimestamp(atoi(printableString));
36 //------------------------------------------------------------------------------
37 //------------------------------------------------------------- Time::asString()
38 //------------------------------------------------------------------------------
39 std::string anna::diameter::codec::basetypes::Time::asString() throw(anna::RuntimeException) {
40 std::string result = asPrintableString();
41 anna::time::Date date;
42 date.storeNtp(a_ntpTimestamp);
43 result += " <"; result += date.asString(); result += ">";
48 //------------------------------------------------------------------------------
49 //--------------------------------------------------------------- Time::decode()
50 //------------------------------------------------------------------------------
51 void anna::diameter::codec::basetypes::Time::decode(const char* buffer, const int size) throw(anna::RuntimeException) {
53 throw anna::RuntimeException("Time::decode | Null Buffer provided", ANNA_FILE_LOCATION);
56 throw anna::RuntimeException("Time::decode | Buffer length must be 4 bytes", ANNA_FILE_LOCATION);
58 a_ntpTimestamp = (((U32)buffer[0] << 24) & 0xFF000000) +
59 (((U32)buffer[1] << 16) & 0x00FF0000) +
60 (((U32)buffer[2] << 8) & 0x0000FF00) +
61 ((U32)buffer[3] & 0x000000FF);
62 // Base class decode()
63 OctetString::decode(buffer, size);
67 //------------------------------------------------------------------------------
68 //--------------------------------------------------------- Time::getTimestamp()
69 //------------------------------------------------------------------------------
70 anna::U32 anna::diameter::codec::basetypes::Time::getTimestamp(Timestamp::_v timestampType) const throw() {
71 return ((timestampType == Timestamp::NTP) ? a_ntpTimestamp : (a_ntpTimestamp - TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970));
75 //------------------------------------------------------------------------------
76 //--------------------------------------------------------- Time::setTimestamp()
77 //------------------------------------------------------------------------------
78 void anna::diameter::codec::basetypes::Time::setTimestamp(const U32& timestamp, Timestamp::_v timestampType) throw() {
79 a_ntpTimestamp = (timestampType == Timestamp::NTP) ? timestamp : (timestamp + TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970);