File: SAMLDateTime.h

package info (click to toggle)
opensaml 1.1a-2%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 2,364 kB
  • ctags: 1,771
  • sloc: cpp: 10,614; sh: 8,329; makefile: 199; ansic: 23
file content (217 lines) | stat: -rw-r--r-- 6,953 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * $Id: SAMLDateTime.h,v 1.1 2005/01/29 00:16:10 cantor Exp $
 */

/*
 * This is mostly copied from Xerces-C, but they don't seem inclined to produce a usable
 * class, so I had to incorporate my own version of it for now. I can't inherit it
 * since the fields I need are private.
 */

#ifndef _XML_DATETIME_H
#define _XML_DATETIME_H

#include <xercesc/util/XMLDateTime.hpp>

namespace saml
{
    class SAML_EXPORTS SAMLDateTime
    {
    public:
        SAMLDateTime();
        SAMLDateTime(const XMLCh* const);
        SAMLDateTime(time_t epoch);
        SAMLDateTime(const SAMLDateTime&);
        SAMLDateTime& operator=(const SAMLDateTime&);
        ~SAMLDateTime();
    
        inline void setBuffer(const XMLCh* const);
    
        const XMLCh* getRawData() const;
        const XMLCh* getFormattedString() const;
        int getSign() const;
    
        XMLCh* getDateTimeCanonicalRepresentation() const;
        XMLCh* getTimeCanonicalRepresentation() const;
    
        void parseDateTime();
        void parseDate();
        void parseTime();
        void parseDay();
        void parseMonth();
        void parseYear();
        void parseMonthDay();
        void parseYearMonth();
        void parseDuration();
    
        static int compare(const SAMLDateTime* const, const SAMLDateTime* const);
        static int compare(const SAMLDateTime* const, const SAMLDateTime* const, bool);
        static int compareOrder(const SAMLDateTime* const, const SAMLDateTime* const);                                    
    
        int getYear() const {return fValue[CentYear];}
        int getMonth() const {return fValue[Month];}
        int getDay() const {return fValue[Day];}
        int getHour() const {return fValue[Hour];}
        int getMinute() const {return fValue[Minute];}
        int getSecond() const {return fValue[Second];}
        time_t getEpoch() const;
    
    private:
        enum valueIndex {
            CentYear   = 0,
            Month      ,
            Day        ,
            Hour       ,
            Minute     ,
            Second     ,
            MiliSecond ,  //not to be used directly
            utc        ,
            TOTAL_SIZE
        };
    
        enum utcType {
            UTC_UNKNOWN = 0,
            UTC_STD        ,          // set in parse() or normalize()
            UTC_POS        ,          // set in parse()
            UTC_NEG                   // set in parse()
        };
    
        enum timezoneIndex {
            hh = 0,
            mm ,
            TIMEZONE_ARRAYSIZE
        };
    
        static int compareResult(int, int, bool);
        static void addDuration(SAMLDateTime* pDuration, const SAMLDateTime* const pBaseDate, int index);
        static int compareResult(const SAMLDateTime* const, const SAMLDateTime* const, bool, int);
        static inline int getRetVal(int, int);
    
        inline void reset();
        //inline void assertBuffer() const;
        inline void copy(const SAMLDateTime&);
        
        inline void initParser();
        inline bool isNormalized() const;
    
        void getDate();
        void getTime();
        void getYearMonth();
        void getTimeZone(const int);
        void parseTimeZone();
    
        int findUTCSign(const int start);
        int indexOf(const int start, const int end, const XMLCh ch) const;
        int parseInt(const int start, const int end) const;
        int parseIntYear(const int end) const;
        double parseMiliSecond(const int start, const int end) const;
    
        void validateDateTime() const;
        void normalize();
        void fillString(XMLCh*& ptr, valueIndex ind, int expLen) const;
        int  fillYearString(XMLCh*& ptr, valueIndex ind) const;
        void searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const;
    
    	bool operator==(const SAMLDateTime& toCompare) const;
    
        static const int DATETIMES[][TOTAL_SIZE];
        int fValue[TOTAL_SIZE];
        int fTimeZone[TIMEZONE_ARRAYSIZE];
        int fStart;
        int fEnd;
        int fBufferMaxLen;
        XMLCh* fBuffer;
    
        double fMiliSecond;
        bool fHasTime;
    };

    inline void SAMLDateTime::setBuffer(const XMLCh* const aString)
    {
        reset();
        fEnd = XMLString::stringLen(aString);
        if (fEnd > 0) {
            if (fEnd > fBufferMaxLen) {
                delete[] fBuffer;
                fBufferMaxLen = fEnd + 8;
                fBuffer = new XMLCh[fBufferMaxLen+1];
            }
            memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh));
        }
    }
    
    inline void SAMLDateTime::reset()
    {
        for ( int i=0; i < XMLDateTime::TOTAL_SIZE; i++ )
            fValue[i] = 0;
    
        fMiliSecond   = 0;
        fHasTime      = false;
        fTimeZone[hh] = fTimeZone[mm] = 0;
        fStart = fEnd = 0;
    
        if (fBuffer)
            *fBuffer = 0;
    }
    
    inline void SAMLDateTime::copy(const SAMLDateTime& rhs)
    {
        for ( int i = 0; i < XMLDateTime::TOTAL_SIZE; i++ )
            fValue[i] = rhs.fValue[i];
    
        fMiliSecond   = rhs.fMiliSecond;
        fHasTime      = rhs.fHasTime;
        fTimeZone[hh] = rhs.fTimeZone[hh];
        fTimeZone[mm] = rhs.fTimeZone[mm];
        fStart = rhs.fStart;
        fEnd   = rhs.fEnd;
    
        if (fEnd > 0) {
            if (fEnd > fBufferMaxLen) {
                delete[] fBuffer;
                fBufferMaxLen = rhs.fBufferMaxLen;
                fBuffer = new XMLCh[fBufferMaxLen+1];
            }
            memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
        }
    }
    
    inline void SAMLDateTime::initParser()
    {
        fStart = 0;   // to ensure scan from the very first beginning
                      // in case the pointer is updated accidentally by someone else.
    }
    
    inline bool SAMLDateTime::isNormalized() const
    {
        return (fValue[XMLDateTime::utc] == XMLDateTime::UTC_STD ? true : false);
    }
    
    inline int SAMLDateTime::getRetVal(int c1, int c2)
    {
        if ((c1 == XMLDateTime::LESS_THAN && c2 == XMLDateTime::GREATER_THAN) ||
            (c1 == XMLDateTime::GREATER_THAN && c2 == XMLDateTime::LESS_THAN))
            return XMLDateTime::INDETERMINATE;
    
        return (c1 != XMLDateTime::INDETERMINATE) ? c1 : c2;
    }

}

#endif