First commit
[anna.git] / include / anna / core / util / Second.hpp
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 #ifndef anna_core_util_Second_hpp
38 #define anna_core_util_Second_hpp
39
40 #include <string>
41
42 #include <anna/config/defines.hpp>
43 #include <anna/core/RuntimeException.hpp>
44
45 namespace anna {
46
47 class Millisecond;
48 class Microsecond;
49
50 /**
51  * Clase para modelar la unidad temporal segundos.
52  */
53 class Second {
54 public:
55 #ifdef __anna64__
56   typedef Integer64 type_t;
57 #else
58   typedef int type_t;
59 #endif
60
61   /**
62      Tamaño de la memoria reservada que debe tener la variable usada para guardar
63      el resultado de convertir el 'time' en texto.
64
65      @see #asDateTime
66   */
67   static const int DateTimeSizeString = 21;
68
69   /**
70    * Constructor
71    */
72   Second() : a_value(0) {;}
73
74   /**
75    * Constructor.
76    * \param value Valor inicial de esta instancia.
77    */
78   explicit Second(const type_t value) : a_value(value) {;}
79
80   /**
81    * Constructor copia.
82    * \param other Instancia de la que copiar.
83    */
84   Second(const Second& other) : a_value(other.a_value) {;}
85
86   /**
87    * Constructor copia.
88    * \param other Instancia de la que copiar.
89    */
90   Second(const Millisecond& other);
91
92   /**
93    * Constructor copia.
94    * \param other Instancia de la que copiar.
95    */
96   Second(const Microsecond& other);
97
98   /**
99    * Conversor a numérico.
100    * \return El valor asociado a esta instancia.
101    */
102   operator type_t () const throw() { return a_value; }
103
104   /**
105    * \internal
106    */
107   type_t& refValue() throw() { return a_value; }
108
109   Second& operator= (const type_t other) throw() { a_value = other; return *this; }
110
111   Second& operator= (const Second& other) throw() { a_value = other.a_value; return *this; }
112
113   Second& operator= (const Millisecond& other) throw();
114
115   Second& operator= (const Microsecond& other) throw();
116
117   bool operator== (const Second& other) const throw() { return a_value == other.a_value; }
118
119   bool operator== (const Millisecond& other) const throw();
120
121   bool operator== (const Microsecond& other) const throw();
122
123   bool operator!= (const Second& other) const throw() { return a_value != other.a_value; }
124
125   bool operator!= (const Millisecond& other) const throw();
126
127   bool operator!= (const Microsecond& other) const throw();
128
129   bool operator> (const Second& other) const throw() { return a_value > other.a_value; }
130
131   bool operator> (const Millisecond& other) const throw();
132
133   bool operator> (const Microsecond& other) const throw();
134
135   bool operator< (const Second& other) const throw() { return a_value < other.a_value; }
136
137   bool operator< (const Millisecond& other) const throw();
138
139   bool operator< (const Microsecond& other) const throw();
140
141   bool operator>= (const Second& other) const throw() { return a_value >= other.a_value; }
142
143   bool operator>= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
144
145   bool operator>= (const Microsecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
146
147   bool operator<= (const Second& other) const throw() { return a_value <= other.a_value; }
148
149   bool operator<= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
150
151   bool operator<= (const Microsecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
152
153   /**
154    * Devuelve el valor asociado a esta instancia.
155    * \return el valor asociado a esta instancia.
156    */
157   type_t getValue() const throw() { return a_value; }
158
159   /**
160      Devuelve una cadena con la hora en formato 'dd/mm/yyyy hh:mm:ss'.
161      @param format Indicador de formato.
162      @return Un literal con la hora en el formato 'dd/mm/yyyy hh:mm:ss'.
163
164      \see man strftime
165   */
166   std::string asDateTime(const char* format = "%d/%0m/%Y %T") const throw();
167
168   /**
169       Devuelve una cadena con la hora en formato 'dd/mm/yyyy hh:mm:ss'.
170
171       @param format Indicador de formato.
172       @param result Puntero donde vamos a guardar el resultado de la conversin.
173       Debe tener espacio reservado para contener #DateTimeSizeString caracteres.
174
175       \see man strftime
176    */
177   const char* asDateTime(char* result, const char* format = "%d/%0m/%Y %T") const throw();
178
179   /**
180    * Devuelve una cadena con el valor de esta instancia y las unidades "sec".
181    * \return una cadena con el valor de esta instancia y las unidades "sec".
182    */
183   std::string asString() const throw();
184
185   /**
186    * Devuelve la hora actual de sistema expresada en segundos transcurridos desde el 1 de Enero de 1970
187    * \return la hora actual de sistema expresada en segundos transcurridos desde el 1 de Enero de 1970
188    */
189   static Second getTime() throw();
190
191   /**
192    * Devuelve la hora actual de sistema expresada en segundos transcurridos desde el 1 de Enero de 1970 aplicando
193    * las correciones correspondientes a la hora local.
194    * \return la hora actual de sistema expresada en segundos transcurridos desde el 1 de Enero de 1970
195    */
196   static Second getLocalTime() throw();
197
198   /**
199    * Obtiene los microsegundos del valor contenido en la cadena recibida como parámetro.
200    * \param value Cadena que contiene los microsegundos habrá sido obtenida con #asString.
201    * \return los microsegundos del valor contenido en la cadena recibida como parámetro.
202    */
203   static Second fromString(const std::string& value) throw(RuntimeException);
204
205 private:
206   type_t a_value;
207
208   friend class Millisecond;
209   friend class Microsecond;
210
211   friend class Second operator + (const Second& left, const Second& right) throw();
212   friend class Second operator - (const Second& left, const Second& right) throw();
213   friend class Second operator / (const Second& left, const Second& right) throw();
214   friend class Second operator / (const Second& left, const int right) throw();
215   friend class Second operator / (const Second& left, const unsigned int right) throw();
216 };
217
218 inline Second operator + (const Second& left, const Second& right)
219 throw() {
220   return Second(left.a_value + right.a_value);
221 }
222
223 inline Second operator - (const Second& left, const Second& right)
224 throw() {
225   return Second(left.a_value - right.a_value);
226 }
227
228 inline Second operator / (const Second& left, const Second& right)
229 throw() {
230   return Second(left.a_value / right.a_value);
231 }
232
233 inline Second operator / (const Second& left, const int right)
234 throw() {
235   return Second(left.a_value / right);
236 }
237
238 inline Second operator / (const Second& left, const unsigned int right)
239 throw() {
240   return Second(left.a_value / right);
241 }
242
243 }
244
245 #endif