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