Xerces-C++ 3.2.5
XMLDateTime.hpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id$
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_XML_DATETIME_HPP)
23#define XERCESC_INCLUDE_GUARD_XML_DATETIME_HPP
24
31
32#include <time.h>
33
35
36class XSValue;
37
39{
40public:
41
43 {
44 CentYear = 0,
50 MiliSecond , //not to be used directly
52 TOTAL_SIZE
53 };
54
56 {
57 UTC_UNKNOWN = 0,
58 UTC_STD , // set in parse() or normalize()
59 UTC_POS , // set in parse()
60 UTC_NEG // set in parse()
61 };
62
63 // -----------------------------------------------------------------------
64 // ctors and dtor
65 // -----------------------------------------------------------------------
66
68 XMLDateTime(const XMLCh* const,
70 XMLDateTime(time_t epoch, bool duration,
73
74 inline void setBuffer(const XMLCh* const);
75
76 // -----------------------------------------------------------------------
77 // Copy ctor and Assignment operators
78 // -----------------------------------------------------------------------
79
81
83
84 // -----------------------------------------------------------------------
85 // Implementation of Abstract Interface
86 // -----------------------------------------------------------------------
87
88 virtual XMLCh* getRawData() const;
89
90 virtual const XMLCh* getFormattedString() const;
91
92 virtual int getSign() const;
93
94 // -----------------------------------------------------------------------
95 // Canonical Representation
96 // -----------------------------------------------------------------------
97
99
101
103
104 // -----------------------------------------------------------------------
105 // parsers
106 // -----------------------------------------------------------------------
107
108 void parseDateTime(); //DateTime
109
110 void parseDate(); //Date
111
112 void parseTime(); //Time
113
114 void parseDay(); //gDay
115
116 void parseMonth(); //gMonth
117
118 void parseYear(); //gYear
119
120 void parseMonthDay(); //gMonthDay
121
122 void parseYearMonth(); //gYearMonth
123
124 void parseDuration(); //duration
125
126 // -----------------------------------------------------------------------
127 // Comparison
128 // -----------------------------------------------------------------------
129 static int compare(const XMLDateTime* const
130 , const XMLDateTime* const);
131
132 static int compare(const XMLDateTime* const
133 , const XMLDateTime* const
134 , bool );
135
136 static int compareOrder(const XMLDateTime* const
137 , const XMLDateTime* const);
138
139 int getYear() const {return fValue[CentYear];}
140 int getMonth() const {return fValue[Month];}
141 int getDay() const {return fValue[Day];}
142 int getHour() const {return fValue[Hour];}
143 int getMinute() const {return fValue[Minute];}
144 int getSecond() const {return fValue[Second];}
145 time_t getEpoch(bool duration=false) const;
146
147 /***
148 * Support for Serialization/De-serialization
149 ***/
150 DECL_XSERIALIZABLE(XMLDateTime)
151
152private:
153
154 // -----------------------------------------------------------------------
155 // Constant data
156 // -----------------------------------------------------------------------
157 //
158
159 enum timezoneIndex
160 {
161 hh = 0,
162 mm ,
163 TIMEZONE_ARRAYSIZE
164 };
165
166 // -----------------------------------------------------------------------
167 // Comparison
168 // -----------------------------------------------------------------------
169 static int compareResult(int
170 , int
171 , bool);
172
173 static void addDuration(XMLDateTime* pDuration
174 , const XMLDateTime* const pBaseDate
175 , int index);
176
177
178 static int compareResult(const XMLDateTime* const
179 , const XMLDateTime* const
180 , bool
181 , int);
182
183 static inline int getRetVal(int, int);
184
185 // -----------------------------------------------------------------------
186 // helper
187 // -----------------------------------------------------------------------
188
189 inline void reset();
190
191 inline void assertBuffer() const;
192
193 inline void copy(const XMLDateTime&);
194
195 // allow multiple parsing
196 inline bool initParser();
197
198 inline bool isNormalized() const;
199
200 // -----------------------------------------------------------------------
201 // scaners
202 // -----------------------------------------------------------------------
203
204 void getDate();
205
206 void getTime();
207
208 void getYearMonth();
209
210 void getTimeZone(const XMLSize_t);
211
212 void parseTimeZone();
213
214 // -----------------------------------------------------------------------
215 // locator and converter
216 // -----------------------------------------------------------------------
217
218 int findUTCSign(const XMLSize_t start);
219
220 int indexOf(const XMLSize_t start
221 , const XMLSize_t end
222 , const XMLCh ch) const;
223
224 int parseInt(const XMLSize_t start
225 , const XMLSize_t end) const;
226
227 int parseIntYear(const XMLSize_t end) const;
228
229 double parseMiliSecond(const XMLSize_t start
230 , const XMLSize_t end) const;
231
232 // -----------------------------------------------------------------------
233 // validator and normalizer
234 // -----------------------------------------------------------------------
235
236 void validateDateTime() const;
237
238 void normalize();
239
240 void fillString(XMLCh*& ptr, int value, XMLSize_t expLen) const;
241
242 int fillYearString(XMLCh*& ptr, int value) const;
243
244 void searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
245
246 // -----------------------------------------------------------------------
247 // Unimplemented operator ==
248 // -----------------------------------------------------------------------
249 bool operator==(const XMLDateTime& toCompare) const;
250
251
252 // -----------------------------------------------------------------------
253 // Private data members
254 //
255 // fValue[]
256 // object representation of date time.
257 //
258 // fTimeZone[]
259 // temporary storage for normalization
260 //
261 // fStart, fEnd
262 // pointers to the portion of fBuffer being parsed
263 //
264 // fBuffer
265 // raw data to be parsed, own it.
266 //
267 // -----------------------------------------------------------------------
268
269 int fValue[TOTAL_SIZE];
270 int fTimeZone[TIMEZONE_ARRAYSIZE];
271 XMLSize_t fStart;
272 XMLSize_t fEnd;
273 XMLSize_t fBufferMaxLen;
274
275 double fMilliSecond;
276 bool fHasTime;
277
278 XMLCh* fBuffer;
279 MemoryManager* fMemoryManager;
280
281 friend class XSValue;
282};
283
284inline void XMLDateTime::setBuffer(const XMLCh* const aString)
285{
286 reset();
287
288 fEnd = XMLString::stringLen(aString);
289
290 for (; fEnd > 0; fEnd--)
291 {
292 if (!XMLChar1_0::isWhitespace(aString[fEnd - 1]))
293 break;
294 }
295
296 if (fEnd > 0) {
297
298 if (fEnd > fBufferMaxLen)
299 {
300 fMemoryManager->deallocate(fBuffer);
301 fBufferMaxLen = fEnd + 8;
302 fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
303 }
304
305 memcpy(fBuffer, aString, (fEnd) * sizeof(XMLCh));
306 fBuffer[fEnd] = '\0';
307 }
308}
309
310inline void XMLDateTime::reset()
311{
312 for ( int i=0; i < TOTAL_SIZE; i++ )
313 fValue[i] = 0;
314
315 fMilliSecond = 0;
316 fHasTime = false;
317 fTimeZone[hh] = fTimeZone[mm] = 0;
318 fStart = fEnd = 0;
319
320 if (fBuffer)
321 *fBuffer = 0;
322}
323
324inline void XMLDateTime::copy(const XMLDateTime& rhs)
325{
326 for ( int i = 0; i < TOTAL_SIZE; i++ )
327 fValue[i] = rhs.fValue[i];
328
329 fMilliSecond = rhs.fMilliSecond;
330 fHasTime = rhs.fHasTime;
331 fTimeZone[hh] = rhs.fTimeZone[hh];
332 fTimeZone[mm] = rhs.fTimeZone[mm];
333 fStart = rhs.fStart;
334 fEnd = rhs.fEnd;
335
336 if (fEnd > 0)
337 {
338 if (fEnd > fBufferMaxLen)
339 {
340 fMemoryManager->deallocate(fBuffer);//delete[] fBuffer;
341 fBufferMaxLen = rhs.fBufferMaxLen;
342 fBuffer = (XMLCh*) fMemoryManager->allocate((fBufferMaxLen+1) * sizeof(XMLCh));
343 }
344
345 memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
346 }
347}
348
349inline bool XMLDateTime::initParser()
350{
351 if (!fBuffer || fBuffer[0] == chNull)
352 return false;
353
354 fStart = 0; // to ensure scan from the very first beginning
355 // in case the pointer is updated accidentally by
356 // someone else.
357 return true;
358}
359
360inline bool XMLDateTime::isNormalized() const
361{
362 return ( fValue[utc] == UTC_STD ? true : false );
363}
364
365inline int XMLDateTime::getRetVal(int c1, int c2)
366{
367 if ((c1 == LESS_THAN && c2 == GREATER_THAN) ||
368 (c1 == GREATER_THAN && c2 == LESS_THAN) )
369 {
370 return INDETERMINATE;
371 }
372
373 return ( c1 != INDETERMINATE ) ? c1 : c2;
374}
375
377
378#endif
XERCES_CPP_NAMESPACE_BEGIN const XMLCh chNull
Definition XMLUniDefs.hpp:37
#define XERCES_CPP_NAMESPACE_BEGIN
Definition XercesDefs.hpp:112
#define XMLUTIL_EXPORT
Definition XercesDefs.hpp:162
#define XERCES_CPP_NAMESPACE_END
Definition XercesDefs.hpp:113
uint16_t XMLCh
Definition Xerces_autoconf_config.hpp:120
size_t XMLSize_t
Definition Xerces_autoconf_config.hpp:112
Configurable memory manager.
Definition MemoryManager.hpp:40
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
virtual void deallocate(void *p)=0
This method deallocates memory.
static bool isWhitespace(const XMLCh toCheck)
Definition XMLChar.hpp:242
Definition XMLDateTime.hpp:39
int getHour() const
Definition XMLDateTime.hpp:142
void parseDay()
void parseYear()
void parseDate()
void parseMonth()
XMLDateTime(MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
XMLCh * getDateTimeCanonicalRepresentation(MemoryManager *const memMgr) const
static int compare(const XMLDateTime *const, const XMLDateTime *const, bool)
utcType
Definition XMLDateTime.hpp:56
@ UTC_POS
Definition XMLDateTime.hpp:59
@ UTC_STD
Definition XMLDateTime.hpp:58
int getMinute() const
Definition XMLDateTime.hpp:143
XMLDateTime & operator=(const XMLDateTime &)
int getDay() const
Definition XMLDateTime.hpp:141
void parseDateTime()
int getMonth() const
Definition XMLDateTime.hpp:140
void parseDuration()
static int compare(const XMLDateTime *const, const XMLDateTime *const)
virtual int getSign() const
Returns the sign of this number.
void setBuffer(const XMLCh *const)
Definition XMLDateTime.hpp:284
virtual XMLCh * getRawData() const
Return string representation of the decimal value.
void parseTime()
XMLCh * getDateCanonicalRepresentation(MemoryManager *const memMgr) const
int getYear() const
Definition XMLDateTime.hpp:139
time_t getEpoch(bool duration=false) const
XMLDateTime(const XMLDateTime &)
valueIndex
Definition XMLDateTime.hpp:43
@ MiliSecond
Definition XMLDateTime.hpp:50
@ Hour
Definition XMLDateTime.hpp:47
@ Day
Definition XMLDateTime.hpp:46
@ Minute
Definition XMLDateTime.hpp:48
@ utc
Definition XMLDateTime.hpp:51
@ Second
Definition XMLDateTime.hpp:49
@ TOTAL_SIZE
Definition XMLDateTime.hpp:52
@ Month
Definition XMLDateTime.hpp:45
int getSecond() const
Definition XMLDateTime.hpp:144
XMLCh * getTimeCanonicalRepresentation(MemoryManager *const memMgr) const
static int compareOrder(const XMLDateTime *const, const XMLDateTime *const)
void parseYearMonth()
void parseMonthDay()
XMLDateTime(const XMLCh *const, MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
XMLDateTime(time_t epoch, bool duration, MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
virtual const XMLCh * getFormattedString() const
Return the original and converted value of the original data.
Definition XMLNumber.hpp:31
@ GREATER_THAN
Definition XMLNumber.hpp:38
@ INDETERMINATE
Definition XMLNumber.hpp:39
@ LESS_THAN
Definition XMLNumber.hpp:36
static MemoryManager * fgMemoryManager
The configurable memory manager.
Definition PlatformUtils.hpp:121
static XMLSize_t stringLen(const char *const src)
Get the length of the string.
Definition XSValue.hpp:33