First commit
[anna.git] / source / diameter / codec / basetypes / Time.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Local
38 #include <anna/diameter/codec/basetypes/Time.hpp>
39
40 #include <anna/time/Date.hpp> // TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970
41
42
43 //------------------------------------------------------------------------------
44 //---------------------------------------------------------- Time::updateBasic()
45 //------------------------------------------------------------------------------
46 void anna::diameter::codec::basetypes::Time::updateBasic() throw(anna::RuntimeException) {
47   std::string result;
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);
53 }
54
55
56 //------------------------------------------------------------------------------
57 //--------------------------------------------------- Time::setPrintableString()
58 //------------------------------------------------------------------------------
59 void anna::diameter::codec::basetypes::Time::setPrintableString(const char * printableString) throw(anna::RuntimeException) {
60   setTimestamp(atoi(printableString));
61 }
62
63
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 += ">";
72   return result;
73 }
74
75
76 //------------------------------------------------------------------------------
77 //--------------------------------------------------------------- Time::decode()
78 //------------------------------------------------------------------------------
79 void anna::diameter::codec::basetypes::Time::decode(const char* buffer, const int size) throw(anna::RuntimeException) {
80   if(!buffer)
81     throw anna::RuntimeException("Time::decode | Null Buffer provided", ANNA_FILE_LOCATION);
82
83   if(size != 4)
84     throw anna::RuntimeException("Time::decode | Buffer length must be 4 bytes", ANNA_FILE_LOCATION);
85
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);
92 }
93
94
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));
100 }
101
102
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);
108   updateBasic();
109 }
110