First commit
[anna.git] / include / anna / dbms / TimeStamp.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_dbms_TimeStamp_hpp
38 #define anna_dbms_TimeStamp_hpp
39
40 #include <anna/dbms/Date.hpp>
41
42 namespace anna {
43
44 namespace dbms {
45
46 /**
47    Tipo de datos que permite trabajar con el tipo de dato 'TimeStamp' de un gestor de base de
48    datos generico.
49
50    El tipo de dato TimeStamp contiene la información suficiente para representar una fecha
51    incluyendo la hora del día.
52 */
53 class TimeStamp : public Date {
54 public:
55   /**
56      Constructor.
57      \param isNulleable Indica si el dato puede tomar valores nulos.
58      \param format Formato usado para interpretar los datos de esta fecha, en los metodos Date::getCStringValue y
59      Date::setValue (const char*) y Date::setValue (const std::string&). Sigue la especificacion:
60
61      \code
62       %a     Replaced by the localeâs abbreviated weekday name. [ tm_wday]
63
64       %A     Replaced by the localeâs full weekday name. [ tm_wday]
65
66       %b     Replaced by the localeâs abbreviated month name. [ tm_mon]
67
68       %B     Replaced by the localeâs full month name. [ tm_mon]
69
70       %c     Replaced  by  the  locale's  appropriate date and time representation.  (See the Base Definitions volume of
71              IEEE Std 1003.1-2001, <time.h>.)
72
73       %C     Replaced by the year divided by 100 and truncated to an integer, as a decimal number [00,99]. [ tm_year]
74
75       %d     Replaced by the day of the month as a decimal number [01,31]. [ tm_mday]
76
77       %D     Equivalent to %m / %d / %y . [ tm_mon, tm_mday, tm_year]
78
79       %e     Replaced by the day of the month as a decimal number [1,31]; a single digit  is  preceded  by  a  space.  [
80              tm_mday]
81
82       %F     Equivalent to %Y - %m - %d (the ISO 8601:2000 standard date format). [ tm_year, tm_mon, tm_mday]
83
84       %g     Replaced  by  the  last 2 digits of the week-based year (see below) as a decimal number [00,99]. [ tm_year,
85              tm_wday, tm_yday]
86
87       %G     Replaced by the week-based year (see below) as a decimal number (for example, 1977).  [  tm_year,  tm_wday,
88              tm_yday]
89
90       %h     Equivalent to %b . [ tm_mon]
91
92       %H     Replaced by the hour (24-hour clock) as a decimal number [00,23].  [ tm_hour]
93
94       %I     Replaced by the hour (12-hour clock) as a decimal number [01,12].  [ tm_hour]
95
96       %j     Replaced by the day of the year as a decimal number [001,366]. [ tm_yday]
97
98       %m     Replaced by the month as a decimal number [01,12]. [ tm_mon]
99
100       %M     Replaced by the minute as a decimal number [00,59]. [ tm_min]
101
102       %n     Replaced by a <newline>.
103
104       %p     Replaced by the locale's equivalent of either a.m. or p.m. [ tm_hour]
105
106       %r     Replaced  by the time in a.m. and p.m. notation;    in the POSIX locale this shall be equivalent to %I : %M
107              : %S %p .  [ tm_hour, tm_min, tm_sec]
108
109       %R     Replaced by the time in 24-hour notation ( %H : %M ).  [ tm_hour, tm_min]
110
111       %S     Replaced by the second as a decimal number [00,60]. [ tm_sec]
112
113       %t     Replaced by a <tab>.
114
115       %T     Replaced by the time ( %H : %M : %S ). [ tm_hour, tm_min, tm_sec]
116
117       %u     Replaced by the weekday as a decimal number [1,7], with 1 representing Monday. [ tm_wday]
118
119       %U     Replaced by the week number of the year as a decimal number [00,53].  The first Sunday of  January  is  the
120              first day of week 1; days in the new year before this are in week 0. [ tm_year, tm_wday, tm_yday]
121
122       %V     Replaced  by the week number of the year (Monday as the first day of the week) as a decimal number [01,53].
123              If the week containing 1 January has four or more days in the new year, then it is considered week 1.  Oth-
124              erwise,  it  is  the  last week of the previous year, and the next week is week 1. Both January 4th and the
125              first Thursday of January are always in week 1. [ tm_year, tm_wday, tm_yday]
126
127       %w     Replaced by the weekday as a decimal number [0,6], with 0 representing Sunday. [ tm_wday]
128
129       %W     Replaced by the week number of the year as a decimal number [00,53].  The first Monday of  January  is  the
130              first day of week 1; days in the new year before this are in week 0. [ tm_year, tm_wday, tm_yday]
131
132       %x     Replaced   by   the  locale's  appropriate  date  representation.  (See  the  Base  Definitions  volume  of
133              IEEE Std 1003.1-2001, <time.h>.)
134
135       %X     Replaced  by  the  locale's  appropriate  time  representation.  (See  the  Base  Definitions   volume   of
136              IEEE Std 1003.1-2001, <time.h>.)
137
138       %y     Replaced by the last two digits of the year as a decimal number [00,99].  [ tm_year]
139
140       %Y     Replaced by the year as a decimal number (for example, 1997). [ tm_year]
141
142       %z     Replaced  by  the offset from UTC in the ISO 8601:2000 standard format ( +hhmm or -hhmm ), or by no charac-
143              ters if no timezone is determinable. For example, "-0430" means 4 hours 30  minutes  behind  UTC  (west  of
144              Greenwich).    If tm_isdst is zero, the standard time offset is used. If tm_isdst is greater than zero, the
145              daylight savings time offset is used. If tm_isdst is negative, no characters are returned.  [ tm_isdst]
146
147       %Z     Replaced by the timezone name or abbreviation, or  by  no  bytes  if  no  timezone  information  exists.  [
148              tm_isdst]
149
150       %%     Replaced by % .
151      \endcode
152
153      Para obtener más informacion sobre la espeficacion de formato \em man \em strftime (p.e.).
154
155      Para poder obtener la parte fraccionaria en la salida del metodo #getCStringValue hay que indicar el literal \em %%d. Por ejemplo:
156
157      \code
158      TimeStamp oneTime (false, "%T.%%d")
159      \endcode
160    */
161   explicit TimeStamp(const bool isNulleable = false, const char* format = NULL)  :
162     Date(Data::Type::TimeStamp, isNulleable, format),
163     a_fractionalSecond(0)
164   {;}
165
166   /**
167    * Devuelve la parte fraccionaria de los segundos asociados a este objeto.
168    * \return La parte fraccionaria de los segundos asociados a este objeto.
169    */
170   int getFractionalSecond() const throw() { return a_fractionalSecond; }
171
172   /**
173    * Establece la parte fraccionaria de los segundos de este objeto.
174    * \param fsec Parte fraccionaria de los segundos.
175    */
176   void setFractionalSecond(const int fsec) throw() { a_fractionalSecond = fsec; }
177
178   /**
179    * Interpreta el contenido de la fecha y lo transfiere al buffer.
180    * \return El buffer que contiene esta fecha interpretada con el formato indicado en el contructor.
181    *  \warning El resultado sera NULL en caso de no poder interpretar correctamente la fecha.
182    */
183   virtual const char* getCStringValue() const throw();
184
185   /**
186      Operador de copia.
187      \param timeStamp Fecha de la que copiar.
188      \return La instancia de esta fecha.
189      \warning Solo copia el contenido de la fecha recibida, no cambia el formato de interpretacion de la fecha origen.
190   */
191   TimeStamp& operator = (const TimeStamp& timeStamp) throw(RuntimeException) {
192     Date::operator= (timeStamp);
193     a_fractionalSecond = timeStamp.a_fractionalSecond;
194     return *this;
195   }
196
197   /**
198      Operador de copia.
199      \param date Fecha de la que copiar.
200      \return La instancia de esta fecha.
201      \warning Solo copia el contenido de la fecha recibida, no cambia el formato de interpretacion de la fecha origen.
202   */
203   TimeStamp& operator = (const Date& date) throw(RuntimeException) { Date::operator= (date); a_fractionalSecond = 0; return *this; }
204
205 private:
206   char a_anotherBuffer [MaxDateSize + 1];
207   int a_fractionalSecond;
208
209   void do_clear() throw() { a_fractionalSecond = 0; a_anotherBuffer [0] = 0; }
210 };
211
212 }
213 }
214
215 #endif
216