1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 #include <anna/diameter/codec/basetypes/Time.hpp>
40 #include <anna/time/Date.hpp> // TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970
43 //------------------------------------------------------------------------------
44 //---------------------------------------------------------- Time::updateBasic()
45 //------------------------------------------------------------------------------
46 void anna::diameter::codec::basetypes::Time::updateBasic() throw(anna::RuntimeException) {
48 result += (char)(a_ntpTimestamp >> 24);
49 result += (char)(a_ntpTimestamp >> 16);
50 result += (char)(a_ntpTimestamp >> 8);
51 result += (char)a_ntpTimestamp;
52 OctetString::setValue(result);
56 //------------------------------------------------------------------------------
57 //--------------------------------------------------- Time::setPrintableString()
58 //------------------------------------------------------------------------------
59 void anna::diameter::codec::basetypes::Time::setPrintableString(const char * printableString) throw(anna::RuntimeException) {
60 setTimestamp(atoi(printableString));
64 //------------------------------------------------------------------------------
65 //------------------------------------------------------------- Time::asString()
66 //------------------------------------------------------------------------------
67 std::string anna::diameter::codec::basetypes::Time::asString() throw(anna::RuntimeException) {
68 std::string result = asPrintableString();
69 anna::time::Date date;
70 date.storeNtp(a_ntpTimestamp);
71 result += " <"; result += date.asString(); result += ">";
76 //------------------------------------------------------------------------------
77 //--------------------------------------------------------------- Time::decode()
78 //------------------------------------------------------------------------------
79 void anna::diameter::codec::basetypes::Time::decode(const char* buffer, const int size) throw(anna::RuntimeException) {
81 throw anna::RuntimeException("Time::decode | Null Buffer provided", ANNA_FILE_LOCATION);
84 throw anna::RuntimeException("Time::decode | Buffer length must be 4 bytes", ANNA_FILE_LOCATION);
86 a_ntpTimestamp = (((U32)buffer[0] << 24) & 0xFF000000) +
87 (((U32)buffer[1] << 16) & 0x00FF0000) +
88 (((U32)buffer[2] << 8) & 0x0000FF00) +
89 ((U32)buffer[3] & 0x000000FF);
90 // Base class decode()
91 OctetString::decode(buffer, size);
95 //------------------------------------------------------------------------------
96 //--------------------------------------------------------- Time::getTimestamp()
97 //------------------------------------------------------------------------------
98 anna::U32 anna::diameter::codec::basetypes::Time::getTimestamp(Timestamp::_v timestampType) const throw() {
99 return ((timestampType == Timestamp::NTP) ? a_ntpTimestamp : (a_ntpTimestamp - TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970));
103 //------------------------------------------------------------------------------
104 //--------------------------------------------------------- Time::setTimestamp()
105 //------------------------------------------------------------------------------
106 void anna::diameter::codec::basetypes::Time::setTimestamp(const U32& timestamp, Timestamp::_v timestampType) throw() {
107 a_ntpTimestamp = (timestampType == Timestamp::NTP) ? timestamp : (timestamp + TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970);