1 // ANNA - Anna is Not 'N' 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
37 #ifndef anna_core_util_Second_hpp
38 #define anna_core_util_Second_hpp
42 #include <anna/config/defines.hpp>
43 #include <anna/core/RuntimeException.hpp>
51 * Clase para modelar la unidad temporal segundos.
56 typedef Integer64 type_t;
62 Tamaño de la memoria reservada que debe tener la variable usada para guardar
63 el resultado de convertir el 'time' en texto.
67 static const int DateTimeSizeString = 21;
72 Second() : a_value(0) {;}
76 * \param value Valor inicial de esta instancia.
78 explicit Second(const type_t value) : a_value(value) {;}
82 * \param other Instancia de la que copiar.
84 Second(const Second& other) : a_value(other.a_value) {;}
88 * \param other Instancia de la que copiar.
90 Second(const Millisecond& other);
94 * \param other Instancia de la que copiar.
96 Second(const Microsecond& other);
99 * Conversor a numérico.
100 * \return El valor asociado a esta instancia.
102 operator type_t () const throw() { return a_value; }
107 type_t& refValue() throw() { return a_value; }
109 Second& operator= (const type_t other) throw() { a_value = other; return *this; }
111 Second& operator= (const Second& other) throw() { a_value = other.a_value; return *this; }
113 Second& operator= (const Millisecond& other) throw();
115 Second& operator= (const Microsecond& other) throw();
117 bool operator== (const Second& other) const throw() { return a_value == other.a_value; }
119 bool operator== (const Millisecond& other) const throw();
121 bool operator== (const Microsecond& other) const throw();
123 bool operator!= (const Second& other) const throw() { return a_value != other.a_value; }
125 bool operator!= (const Millisecond& other) const throw();
127 bool operator!= (const Microsecond& other) const throw();
129 bool operator> (const Second& other) const throw() { return a_value > other.a_value; }
131 bool operator> (const Millisecond& other) const throw();
133 bool operator> (const Microsecond& other) const throw();
135 bool operator< (const Second& other) const throw() { return a_value < other.a_value; }
137 bool operator< (const Millisecond& other) const throw();
139 bool operator< (const Microsecond& other) const throw();
141 bool operator>= (const Second& other) const throw() { return a_value >= other.a_value; }
143 bool operator>= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
145 bool operator>= (const Microsecond& other) const throw() { return (operator==(other) == true) ? true : operator>(other); }
147 bool operator<= (const Second& other) const throw() { return a_value <= other.a_value; }
149 bool operator<= (const Millisecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
151 bool operator<= (const Microsecond& other) const throw() { return (operator==(other) == true) ? true : operator<(other); }
154 * Devuelve el valor asociado a esta instancia.
155 * \return el valor asociado a esta instancia.
157 type_t getValue() const throw() { return a_value; }
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'.
166 std::string asDateTime(const char* format = "%d/%0m/%Y %T") const throw();
169 Devuelve una cadena con la hora en formato 'dd/mm/yyyy hh:mm:ss'.
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.
177 const char* asDateTime(char* result, const char* format = "%d/%0m/%Y %T") const throw();
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".
183 std::string asString() const throw();
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
189 static Second getTime() throw();
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
196 static Second getLocalTime() throw();
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.
203 static Second fromString(const std::string& value) throw(RuntimeException);
208 friend class Millisecond;
209 friend class Microsecond;
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();
218 inline Second operator + (const Second& left, const Second& right)
220 return Second(left.a_value + right.a_value);
223 inline Second operator - (const Second& left, const Second& right)
225 return Second(left.a_value - right.a_value);
228 inline Second operator / (const Second& left, const Second& right)
230 return Second(left.a_value / right.a_value);
233 inline Second operator / (const Second& left, const int right)
235 return Second(left.a_value / right);
238 inline Second operator / (const Second& left, const unsigned int right)
240 return Second(left.a_value / right);