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