Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / util / DateTime.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file xmltooling/util/DateTime.h
23  *
24  * Manipulation of XML date/time data.
25  */
26
27 #ifndef __xmltool_datetime_h__
28 #define __xmltool_datetime_h__
29
30 #include <xmltooling/base.h>
31
32 #if defined (_MSC_VER)
33     #pragma warning( push )
34     #pragma warning( disable : 4244 )
35 #endif
36
37 #include <ctime>
38 #include <xercesc/util/XMLDateTime.hpp>
39
40 #if defined (_MSC_VER)
41     #pragma warning( pop )
42 #endif
43
44 namespace xmltooling
45 {
46     /**
47      * Class for manipulating XML date/time information.
48      *
49      * This is mostly copied from Xerces-C, but they haven't produced a usable date/time
50      * class, so we had to incorporate a version of it for now. It can't be inherited
51      * since the fields needed are private.
52      */
53     class XMLTOOL_API DateTime
54     {
55     public:
56         /// @cond OFF
57         DateTime();
58         DateTime(const XMLCh* const);
59         DateTime(time_t epoch, bool duration=false);
60         DateTime(const DateTime&);
61         DateTime& operator=(const DateTime&);
62         ~DateTime();
63
64         void setBuffer(const XMLCh* const);
65
66         const XMLCh* getRawData() const;
67         const XMLCh* getFormattedString() const;
68         int getSign() const;
69
70         XMLCh* getDateTimeCanonicalRepresentation() const;
71         XMLCh* getTimeCanonicalRepresentation() const;
72
73         void parseDateTime();
74         void parseDate();
75         void parseTime();
76         void parseDay();
77         void parseMonth();
78         void parseYear();
79         void parseMonthDay();
80         void parseYearMonth();
81         void parseDuration();
82
83         static int compare(const DateTime* const, const DateTime* const);
84         static int compare(const DateTime* const, const DateTime* const, bool);
85         static int compareOrder(const DateTime* const, const DateTime* const);
86
87         int getYear() const {return fValue[CentYear];}
88         int getMonth() const {return fValue[Month];}
89         int getDay() const {return fValue[Day];}
90         int getHour() const {return fValue[Hour];}
91         int getMinute() const {return fValue[Minute];}
92         int getSecond() const {return fValue[Second];}
93         time_t getEpoch(bool duration=false) const;
94
95         /// @endcond
96     private:
97         enum valueIndex {
98             CentYear   = 0,
99             Month      ,
100             Day        ,
101             Hour       ,
102             Minute     ,
103             Second     ,
104             MiliSecond ,  //not to be used directly
105             utc        ,
106             TOTAL_SIZE
107         };
108
109         enum utcType {
110             UTC_UNKNOWN = 0,
111             UTC_STD        ,          // set in parse() or normalize()
112             UTC_POS        ,          // set in parse()
113             UTC_NEG                   // set in parse()
114         };
115
116         enum timezoneIndex {
117             hh = 0,
118             mm ,
119             TIMEZONE_ARRAYSIZE
120         };
121
122         static int compareResult(int, int, bool);
123         static void addDuration(DateTime* pDuration, const DateTime* const pBaseDate, int index);
124         static int compareResult(const DateTime* const, const DateTime* const, bool, int);
125         static int getRetVal(int, int);
126
127         void reset();
128         //inline void assertBuffer() const;
129         void copy(const DateTime&);
130
131         void initParser();
132         bool isNormalized() const;
133
134         void getDate();
135         void getTime();
136         void getYearMonth();
137         void getTimeZone(const int);
138         void parseTimeZone();
139
140         int findUTCSign(const int start);
141         int indexOf(const int start, const int end, const XMLCh ch) const;
142         int parseInt(const int start, const int end) const;
143         int parseIntYear(const int end) const;
144         double parseMiliSecond(const int start, const int end) const;
145
146         void validateDateTime() const;
147         void normalize();
148         void fillString(XMLCh*& ptr, valueIndex ind, int expLen) const;
149         int  fillYearString(XMLCh*& ptr, valueIndex ind) const;
150         void searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
151
152         bool operator==(const DateTime& toCompare) const;
153
154         static const int DATETIMES[][TOTAL_SIZE];
155         int fValue[TOTAL_SIZE];
156         int fTimeZone[TIMEZONE_ARRAYSIZE];
157         int fStart;
158         int fEnd;
159         int fBufferMaxLen;
160         XMLCh* fBuffer;
161
162         double fMiliSecond;
163         bool fHasTime;
164     };
165 }
166
167 #endif /* __xmltool_datetime_h__ */