Remove dynamic exceptions
[anna.git] / source / diameter / codec / basetypes / Time.cpp
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 // Local
10 #include <anna/diameter/codec/basetypes/Time.hpp>
11
12 #include <anna/time/Date.hpp> // TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970
13
14
15 //------------------------------------------------------------------------------
16 //---------------------------------------------------------- Time::updateBasic()
17 //------------------------------------------------------------------------------
18 void anna::diameter::codec::basetypes::Time::updateBasic() noexcept(false) {
19   std::string result;
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);
25 }
26
27
28 //------------------------------------------------------------------------------
29 //--------------------------------------------------- Time::setPrintableString()
30 //------------------------------------------------------------------------------
31 void anna::diameter::codec::basetypes::Time::setPrintableString(const char * printableString) noexcept(false) {
32   setTimestamp(atoi(printableString));
33 }
34
35
36 //------------------------------------------------------------------------------
37 //------------------------------------------------------------- Time::asString()
38 //------------------------------------------------------------------------------
39 std::string anna::diameter::codec::basetypes::Time::asString() noexcept(false) {
40   std::string result = asPrintableString();
41   anna::time::Date date;
42   date.storeNtp(a_ntpTimestamp);
43   result += " <"; result += date.asString(); result += ">";
44   return result;
45 }
46
47
48 //------------------------------------------------------------------------------
49 //--------------------------------------------------------------- Time::decode()
50 //------------------------------------------------------------------------------
51 void anna::diameter::codec::basetypes::Time::decode(const char* buffer, const int size) noexcept(false) {
52   if(!buffer)
53     throw anna::RuntimeException("Time::decode | Null Buffer provided", ANNA_FILE_LOCATION);
54
55   if(size != 4)
56     throw anna::RuntimeException("Time::decode | Buffer length must be 4 bytes", ANNA_FILE_LOCATION);
57
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);
64 }
65
66
67 //------------------------------------------------------------------------------
68 //--------------------------------------------------------- Time::getTimestamp()
69 //------------------------------------------------------------------------------
70 anna::U32 anna::diameter::codec::basetypes::Time::getTimestamp(Timestamp::_v timestampType) const {
71   return ((timestampType == Timestamp::NTP) ? a_ntpTimestamp : (a_ntpTimestamp - TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970));
72 }
73
74
75 //------------------------------------------------------------------------------
76 //--------------------------------------------------------- Time::setTimestamp()
77 //------------------------------------------------------------------------------
78 void anna::diameter::codec::basetypes::Time::setTimestamp(const U32& timestamp, Timestamp::_v timestampType) {
79   a_ntpTimestamp = (timestampType == Timestamp::NTP) ? timestamp : (timestamp + TIMESTAMP_OFFSET_NTP1900_OVER_UNIX1970);
80   updateBasic();
81 }
82