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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * Manipulation of XML date/time data.
28 * This is mostly copied from Xerces-C, but they don't seem inclined to produce a usable
29 * class, so I had to incorporate my own version of it for now. I can't inherit it
30 * since the fields I need are private.
35 #include "util/DateTime.h"
44 #include <xercesc/util/Janitor.hpp>
46 using namespace xmltooling;
47 using namespace xercesc;
51 // constants used to process raw data (fBuffer)
53 // [-]{CCYY-MM-DD}'T'{HH:MM:SS.MS}['Z']
57 static const XMLCh DURATION_STARTER = chLatin_P; // 'P'
58 static const XMLCh DURATION_Y = chLatin_Y; // 'Y'
59 static const XMLCh DURATION_M = chLatin_M; // 'M'
60 static const XMLCh DURATION_D = chLatin_D; // 'D'
61 static const XMLCh DURATION_H = chLatin_H; // 'H'
62 static const XMLCh DURATION_S = chLatin_S; // 'S'
64 static const XMLCh DATE_SEPARATOR = chDash; // '-'
65 static const XMLCh TIME_SEPARATOR = chColon; // ':'
66 static const XMLCh TIMEZONE_SEPARATOR = chColon; // ':'
67 static const XMLCh DATETIME_SEPARATOR = chLatin_T; // 'T'
68 static const XMLCh MILISECOND_SEPARATOR = chPeriod; // '.'
70 static const XMLCh UTC_STD_CHAR = chLatin_Z; // 'Z'
71 static const XMLCh UTC_POS_CHAR = chPlus; // '+'
72 static const XMLCh UTC_NEG_CHAR = chDash; // '-'
74 static const XMLCh UTC_SET[] = {UTC_STD_CHAR //"Z+-"
79 static const int YMD_MIN_SIZE = 10; // CCYY-MM-DD
80 static const int YMONTH_MIN_SIZE = 7; // CCYY_MM
81 static const int TIME_MIN_SIZE = 8; // hh:mm:ss
82 static const int TIMEZONE_SIZE = 5; // hh:mm
83 static const int DAY_SIZE = 5; // ---DD
84 //static const int MONTH_SIZE = 6; // --MM--
85 static const int MONTHDAY_SIZE = 7; // --MM-DD
86 static const int NOT_FOUND = -1;
88 //define constants to be used in assigning default values for
89 //all date/time excluding duration
90 static const int YEAR_DEFAULT = 2000;
91 static const int MONTH_DEFAULT = 01;
92 static const int DAY_DEFAULT = 15;
94 // order-relation on duration is a partial order. The dates below are used to
95 // for comparison of 2 durations, based on the fact that
96 // duration x and y is x<=y iff s+x<=s+y
97 // see 3.2.6 duration W3C schema datatype specs
99 // the dates are in format: {CCYY,MM,DD, H, S, M, MS, timezone}
100 const int DateTime::DATETIMES[][TOTAL_SIZE] =
102 {1696, 9, 1, 0, 0, 0, 0, UTC_STD},
103 {1697, 2, 1, 0, 0, 0, 0, UTC_STD},
104 {1903, 3, 1, 0, 0, 0, 0, UTC_STD},
105 {1903, 7, 1, 0, 0, 0, 0, UTC_STD}
108 // ---------------------------------------------------------------------------
110 // ---------------------------------------------------------------------------
111 static inline int fQuotient(int a, int b)
113 div_t div_result = div(a, b);
114 return div_result.quot;
117 static inline int fQuotient(int temp, int low, int high)
119 return fQuotient(temp - low, high - low);
122 static inline int mod(int a, int b, int quotient)
124 return (a - quotient*b) ;
127 static inline int modulo (int temp, int low, int high)
129 //modulo(a - low, high - low) + low
132 return (mod (a, b, fQuotient(a, b)) + low) ;
135 static inline bool isLeapYear(int year)
137 return((year%4 == 0) && ((year%100 != 0) || (year%400 == 0)));
140 static int maxDayInMonthFor(int year, int month)
143 if ( month == 4 || month == 6 || month == 9 || month == 11 )
149 if ( isLeapYear(year) )
161 // ---------------------------------------------------------------------------
162 // static methods : for duration
163 // ---------------------------------------------------------------------------
165 * Compares 2 given durations. (refer to W3C Schema Datatypes "3.2.6 duration")
167 * 3.2.6.2 Order relation on duration
169 * In general, the order-relation on duration is a partial order since there is no
170 * determinate relationship between certain durations such as one month (P1M) and 30 days (P30D).
171 * The order-relation of two duration values x and y is x < y iff s+x < s+y for each qualified
172 * dateTime s in the list below.
174 * These values for s cause the greatest deviations in the addition of dateTimes and durations
177 int DateTime::compare(const DateTime* const pDate1
178 , const DateTime* const pDate2
181 //REVISIT: this is unoptimazed vs of comparing 2 durations
182 // Algorithm is described in 3.2.6.2 W3C Schema Datatype specs
185 int resultA, resultB = XMLDateTime::INDETERMINATE;
187 //try and see if the objects are equal
188 if ( (resultA = compareOrder(pDate1, pDate2)) == XMLDateTime::EQUAL)
189 return XMLDateTime::EQUAL;
191 //long comparison algorithm is required
192 DateTime tempA, *pTempA = &tempA;
193 DateTime tempB, *pTempB = &tempB;
195 addDuration(pTempA, pDate1, 0);
196 addDuration(pTempB, pDate2, 0);
197 resultA = compareOrder(pTempA, pTempB);
198 if ( resultA == XMLDateTime::INDETERMINATE )
199 return XMLDateTime::INDETERMINATE;
201 addDuration(pTempA, pDate1, 1);
202 addDuration(pTempB, pDate2, 1);
203 resultB = compareOrder(pTempA, pTempB);
204 resultA = compareResult(resultA, resultB, strict);
205 if ( resultA == XMLDateTime::INDETERMINATE )
206 return XMLDateTime::INDETERMINATE;
208 addDuration(pTempA, pDate1, 2);
209 addDuration(pTempB, pDate2, 2);
210 resultB = compareOrder(pTempA, pTempB);
211 resultA = compareResult(resultA, resultB, strict);
212 if ( resultA == XMLDateTime::INDETERMINATE )
213 return XMLDateTime::INDETERMINATE;
215 addDuration(pTempA, pDate1, 3);
216 addDuration(pTempB, pDate2, 3);
217 resultB = compareOrder(pTempA, pTempB);
218 resultA = compareResult(resultA, resultB, strict);
225 // Form a new DateTime with duration and baseDate array
231 void DateTime::addDuration(DateTime* fNewDate
232 , const DateTime* const fDuration
237 //REVISIT: some code could be shared between normalize() and this method,
238 // however is it worth moving it? The structures are different...
242 //add months (may be modified additionaly below)
243 int temp = DATETIMES[index][Month] + fDuration->fValue[Month];
244 fNewDate->fValue[Month] = modulo(temp, 1, 13);
245 int carry = fQuotient(temp, 1, 13);
247 //add years (may be modified additionaly below)
248 fNewDate->fValue[CentYear] =
249 DATETIMES[index][CentYear] + fDuration->fValue[CentYear] + carry;
252 temp = DATETIMES[index][Second] + fDuration->fValue[Second];
253 carry = fQuotient (temp, 60);
254 fNewDate->fValue[Second] = mod(temp, 60, carry);
257 temp = DATETIMES[index][Minute] + fDuration->fValue[Minute] + carry;
258 carry = fQuotient(temp, 60);
259 fNewDate->fValue[Minute] = mod(temp, 60, carry);
262 temp = DATETIMES[index][Hour] + fDuration->fValue[Hour] + carry;
263 carry = fQuotient(temp, 24);
264 fNewDate->fValue[Hour] = mod(temp, 24, carry);
266 fNewDate->fValue[Day] =
267 DATETIMES[index][Day] + fDuration->fValue[Day] + carry;
271 temp = maxDayInMonthFor(fNewDate->fValue[CentYear], fNewDate->fValue[Month]);
272 if ( fNewDate->fValue[Day] < 1 )
273 { //original fNewDate was negative
274 fNewDate->fValue[Day] +=
275 maxDayInMonthFor(fNewDate->fValue[CentYear], fNewDate->fValue[Month]-1);
278 else if ( fNewDate->fValue[Day] > temp )
280 fNewDate->fValue[Day] -= temp;
288 temp = fNewDate->fValue[Month] + carry;
289 fNewDate->fValue[Month] = modulo(temp, 1, 13);
290 fNewDate->fValue[CentYear] += fQuotient(temp, 1, 13);
293 //fNewDate->fValue[utc] = UTC_STD_CHAR;
294 fNewDate->fValue[utc] = UTC_STD;
297 int DateTime::compareResult(int resultA
302 if ( resultB == XMLDateTime::INDETERMINATE )
304 return XMLDateTime::INDETERMINATE;
306 else if ( (resultA != resultB) &&
309 return XMLDateTime::INDETERMINATE;
311 else if ( (resultA != resultB) &&
314 if ( (resultA != XMLDateTime::EQUAL) &&
315 (resultB != XMLDateTime::EQUAL) )
317 return XMLDateTime::INDETERMINATE;
321 return (resultA != XMLDateTime::EQUAL)? resultA : resultB;
329 // ---------------------------------------------------------------------------
330 // static methods : for others
331 // ---------------------------------------------------------------------------
332 int DateTime::compare(const DateTime* const pDate1
333 , const DateTime* const pDate2)
336 if (pDate1->fValue[utc] == pDate2->fValue[utc])
338 return DateTime::compareOrder(pDate1, pDate2);
343 if ( pDate1->isNormalized())
345 c1 = compareResult(pDate1, pDate2, false, UTC_POS);
346 c2 = compareResult(pDate1, pDate2, false, UTC_NEG);
347 return getRetVal(c1, c2);
349 else if ( pDate2->isNormalized())
351 c1 = compareResult(pDate1, pDate2, true, UTC_POS);
352 c2 = compareResult(pDate1, pDate2, true, UTC_NEG);
353 return getRetVal(c1, c2);
356 return XMLDateTime::INDETERMINATE;
359 int DateTime::compareResult(const DateTime* const pDate1
360 , const DateTime* const pDate2
364 DateTime tmpDate = (set2Left ? *pDate1 : *pDate2);
366 tmpDate.fTimeZone[hh] = 14;
367 tmpDate.fTimeZone[mm] = 0;
368 tmpDate.fValue[utc] = utc_type;
371 return (set2Left? DateTime::compareOrder(&tmpDate, pDate2) :
372 DateTime::compareOrder(pDate1, &tmpDate));
375 int DateTime::compareOrder(const DateTime* const lValue
376 , const DateTime* const rValue)
377 //, MemoryManager* const memMgr)
380 // If any of the them is not normalized() yet,
381 // we need to do something here.
383 DateTime lTemp = *lValue;
384 DateTime rTemp = *rValue;
389 for ( int i = 0 ; i < TOTAL_SIZE; i++ )
391 if ( lTemp.fValue[i] < rTemp.fValue[i] )
393 return XMLDateTime::LESS_THAN;
395 else if ( lTemp.fValue[i] > rTemp.fValue[i] )
397 return XMLDateTime::GREATER_THAN;
403 if ( lTemp.fMiliSecond < rTemp.fMiliSecond )
405 return XMLDateTime::LESS_THAN;
407 else if ( lTemp.fMiliSecond > rTemp.fMiliSecond )
409 return XMLDateTime::GREATER_THAN;
413 return XMLDateTime::EQUAL;
416 // ---------------------------------------------------------------------------
418 // ---------------------------------------------------------------------------
419 DateTime::~DateTime()
435 DateTime::DateTime(const XMLCh* const aString)
446 DateTime::DateTime(time_t epoch, bool duration)
460 time_t days = epoch / 86400;
462 time_t hours = epoch / 3600;
464 time_t minutes = epoch / 60;
466 s << "P" << days << "DT" << hours << "H" << minutes << "M" << epoch << "S";
467 auto_ptr_XMLCh timeptr(s.str().c_str());
468 setBuffer(timeptr.get());
471 #ifndef HAVE_GMTIME_R
472 struct tm* ptime=gmtime(&epoch);
475 struct tm* ptime=gmtime_r(&epoch,&res);
478 strftime(timebuf,32,"%Y-%m-%dT%H:%M:%SZ",ptime);
479 auto_ptr_XMLCh timeptr(timebuf);
480 setBuffer(timeptr.get());
484 // -----------------------------------------------------------------------
485 // Copy ctor and Assignment operators
486 // -----------------------------------------------------------------------
488 DateTime::DateTime(const DateTime &toCopy)
495 DateTime& DateTime::operator=(const DateTime& rhs)
504 // -----------------------------------------------------------------------
505 // Implementation of Abstract Interface
506 // -----------------------------------------------------------------------
509 // We may simply return the handle to fBuffer
511 const XMLCh* DateTime::getRawData() const
518 const XMLCh* DateTime::getFormattedString() const
523 int DateTime::getSign() const
528 time_t DateTime::getEpoch(bool duration) const
531 time_t epoch = getSecond() + (60 * getMinute()) + (3600 * getHour()) + (86400 * getDay());
533 epoch += (((365 * 4) + 1)/48 * 86400);
535 epoch += 365.25 * 86400;
536 return getSign()!=UTC_NEG ? epoch : -epoch;
540 t.tm_sec=getSecond();
541 t.tm_min=getMinute();
544 t.tm_mon=getMonth()-1;
545 t.tm_year=getYear()-1900;
547 #if defined(HAVE_TIMEGM)
550 // Windows, and hopefully most others...?
551 return mktime(&t) - timezone;
556 // ---------------------------------------------------------------------------
558 // ---------------------------------------------------------------------------
561 // [-]{CCYY-MM-DD}'T'{HH:MM:SS.MS}[TimeZone]
563 void DateTime::parseDateTime()
568 //fStart is supposed to point to 'T'
569 if (fBuffer[fStart++] != DATETIME_SEPARATOR)
570 throw XMLParserException("Invalid separator between date and time.");
579 // [-]{CCYY-MM-DD}[TimeZone]
581 void DateTime::parseDate()
590 void DateTime::parseTime()
594 // time initialize to default values
595 fValue[CentYear]= YEAR_DEFAULT;
596 fValue[Month] = MONTH_DEFAULT;
597 fValue[Day] = DAY_DEFAULT;
610 void DateTime::parseDay()
614 if (fBuffer[0] != DATE_SEPARATOR ||
615 fBuffer[1] != DATE_SEPARATOR ||
616 fBuffer[2] != DATE_SEPARATOR )
618 throw XMLParserException("Invalid character in date.");
622 fValue[CentYear] = YEAR_DEFAULT;
623 fValue[Month] = MONTH_DEFAULT;
624 fValue[Day] = parseInt(fStart+3, fStart+5);
626 if ( DAY_SIZE < fEnd )
628 int sign = findUTCSign(DAY_SIZE);
631 throw XMLParserException("Invalid character in date.");
644 // {--MM--}[TimeZone]
648 void DateTime::parseMonth()
652 if (fBuffer[0] != DATE_SEPARATOR ||
653 fBuffer[1] != DATE_SEPARATOR )
655 throw XMLParserException("Invalid character in date.");
659 fValue[CentYear] = YEAR_DEFAULT;
660 fValue[Day] = DAY_DEFAULT;
661 fValue[Month] = parseInt(2, 4);
663 // REVISIT: allow both --MM and --MM-- now.
664 // need to remove the following lines to disallow --MM--
665 // when the errata is officially in the rec.
667 if ( fEnd >= fStart+2 && fBuffer[fStart] == DATE_SEPARATOR && fBuffer[fStart+1] == DATE_SEPARATOR )
673 // parse TimeZone if any
677 int sign = findUTCSign(fStart);
680 throw XMLParserException("Invalid character in date.");
693 //[-]{CCYY}[TimeZone]
696 void DateTime::parseYear()
700 // skip the first '-' and search for timezone
702 int sign = findUTCSign((fBuffer[0] == chDash) ? 1 : 0);
704 if (sign == NOT_FOUND)
706 fValue[CentYear] = parseIntYear(fEnd);
710 fValue[CentYear] = parseIntYear(sign);
715 fValue[Month] = MONTH_DEFAULT;
716 fValue[Day] = DAY_DEFAULT; //java is 1
723 //{--MM-DD}[TimeZone]
726 void DateTime::parseMonthDay()
730 if (fBuffer[0] != DATE_SEPARATOR ||
731 fBuffer[1] != DATE_SEPARATOR ||
732 fBuffer[4] != DATE_SEPARATOR )
734 throw XMLParserException("Invalid character in date.");
739 fValue[CentYear] = YEAR_DEFAULT;
740 fValue[Month] = parseInt(2, 4);
741 fValue[Day] = parseInt(5, 7);
743 if ( MONTHDAY_SIZE < fEnd )
745 int sign = findUTCSign(MONTHDAY_SIZE);
748 throw XMLParserException("Invalid character in date.");
760 void DateTime::parseYearMonth()
766 fValue[Day] = DAY_DEFAULT;
774 //PnYn MnDTnH nMnS: -P1Y2M3DT10H30M
776 // [-]{'P'{[n'Y'][n'M'][n'D']['T'][n'H'][n'M'][n'S']}}
778 // Note: the n above shall be >= 0
779 // if no time element found, 'T' shall be absent
781 void DateTime::parseDuration()
785 // must start with '-' or 'P'
787 XMLCh c = fBuffer[fStart++];
788 if ( (c != DURATION_STARTER) &&
791 throw XMLParserException("Invalid character in time.");
794 // 'P' must ALWAYS be present in either case
795 if ( (c == chDash) &&
796 (fBuffer[fStart++]!= DURATION_STARTER ))
798 throw XMLParserException("Invalid character in time.");
802 //date[utc]=(c=='-')?'-':0;
803 //fValue[utc] = UTC_STD;
804 fValue[utc] = (fBuffer[0] == chDash? UTC_NEG : UTC_STD);
806 int negate = ( fBuffer[0] == chDash ? -1 : 1);
809 // No negative value is allowed after 'P'
811 // eg P-1234, invalid
813 if (indexOf(fStart, fEnd, chDash) != NOT_FOUND)
815 throw XMLParserException("Invalid character in time.");
818 //at least one number and designator must be seen after P
819 bool designator = false;
821 int endDate = indexOf(fStart, fEnd, DATETIME_SEPARATOR);
822 if ( endDate == NOT_FOUND )
824 endDate = fEnd; // 'T' absent
828 int end = indexOf(fStart, endDate, DURATION_Y);
829 if ( end != NOT_FOUND )
832 fValue[CentYear] = negate * parseInt(fStart, end);
837 end = indexOf(fStart, endDate, DURATION_M);
838 if ( end != NOT_FOUND )
841 fValue[Month] = negate * parseInt(fStart, end);
846 end = indexOf(fStart, endDate, DURATION_D);
847 if ( end != NOT_FOUND )
850 fValue[Day] = negate * parseInt(fStart,end);
855 if ( (fEnd == endDate) && // 'T' absent
856 (fStart != fEnd) ) // something after Day
858 throw XMLParserException("Invalid character in time.");
861 if ( fEnd != endDate ) // 'T' present
863 //scan hours, minutes, seconds
867 end = indexOf(++fStart, fEnd, DURATION_H);
868 if ( end != NOT_FOUND )
871 fValue[Hour] = negate * parseInt(fStart, end);
876 end = indexOf(fStart, fEnd, DURATION_M);
877 if ( end != NOT_FOUND )
880 fValue[Minute] = negate * parseInt(fStart, end);
885 end = indexOf(fStart, fEnd, DURATION_S);
886 if ( end != NOT_FOUND )
889 int mlsec = indexOf (fStart, end, MILISECOND_SEPARATOR);
892 * Schema Errata: E2-23
893 * at least one digit must follow the decimal point if it appears.
894 * That is, the value of the seconds component must conform
895 * to the following pattern: [0-9]+(.[0-9]+)?
897 if ( mlsec != NOT_FOUND )
900 * make usure there is something after the '.' and before the end.
902 if ( mlsec+1 == end )
904 throw XMLParserException("Invalid character in time.");
907 fValue[Second] = negate * parseInt(fStart, mlsec);
908 fMiliSecond = negate * parseMiliSecond(mlsec+1, end);
912 fValue[Second] = negate * parseInt(fStart,end);
919 // no additional data should appear after last item
920 // P1Y1M1DT is illigal value as well
921 if ( (fStart != fEnd) ||
922 fBuffer[--fStart] == DATETIME_SEPARATOR )
924 throw XMLParserException("Invalid character in time.");
930 throw XMLParserException("Invalid character in time.");
935 // ---------------------------------------------------------------------------
937 // ---------------------------------------------------------------------------
942 // Note: CCYY could be more than 4 digits
943 // Assuming fStart point to the beginning of the Date Section
944 // fStart updated to point to the position right AFTER the second 'D'
945 // Since the lenght of CCYY might be variable, we can't check format upfront
947 void DateTime::getDate()
950 // Ensure enough chars in buffer
951 if ( (fStart+YMD_MIN_SIZE) > fEnd)
952 throw XMLParserException("Date/time string not complete.");
954 getYearMonth(); // Scan YearMonth and
955 // fStart point to the next '-'
957 if (fBuffer[fStart++] != DATE_SEPARATOR)
959 throw XMLParserException("CCYY-MM must be followed by '-' sign.");
962 fValue[Day] = parseInt(fStart, fStart+2);
963 fStart += 2 ; //fStart points right after the Day
969 // hh:mm:ss[.msssss]['Z']
970 // hh:mm:ss[.msssss][['+'|'-']hh:mm]
973 // Note: Assuming fStart point to the beginning of the Time Section
974 // fStart updated to point to the position right AFTER the second 's'
977 void DateTime::getTime()
980 // Ensure enough chars in buffer
981 if ( (fStart+TIME_MIN_SIZE) > fEnd)
982 throw XMLParserException("Incomplete Time Format.");
984 // check (fixed) format first
985 if ((fBuffer[fStart + 2] != TIME_SEPARATOR) ||
986 (fBuffer[fStart + 5] != TIME_SEPARATOR) )
988 throw XMLParserException("Error in parsing time.");
992 // get hours, minute and second
994 fValue[Hour] = parseInt(fStart + 0, fStart + 2);
995 fValue[Minute] = parseInt(fStart + 3, fStart + 5);
996 fValue[Second] = parseInt(fStart + 6, fStart + 8);
999 // to see if any ms and/or utc part after that
1003 //find UTC sign if any
1004 int sign = findUTCSign(fStart);
1007 int milisec = (fBuffer[fStart] == MILISECOND_SEPARATOR)? fStart : NOT_FOUND;
1008 if ( milisec != NOT_FOUND )
1010 fStart++; // skip the '.'
1011 // make sure we have some thing between the '.' and fEnd
1014 throw XMLParserException("ms should be present once '.' is present.");
1017 if ( sign == NOT_FOUND )
1019 fMiliSecond = parseMiliSecond(fStart, fEnd); //get ms between '.' and fEnd
1024 fMiliSecond = parseMiliSecond(fStart, sign); //get ms between UTC sign and fEnd
1027 else if(sign == 0 || sign != fStart)
1029 throw XMLParserException("Seconds has more than 2 digits.");
1032 //parse UTC time zone (hh:mm)
1042 // Note: CCYY could be more than 4 digits
1043 // fStart updated to point AFTER the second 'M' (probably meet the fEnd)
1045 void DateTime::getYearMonth()
1048 // Ensure enough chars in buffer
1049 if ( (fStart+YMONTH_MIN_SIZE) > fEnd)
1050 throw XMLParserException("Incomplete YearMonth Format.");
1052 // skip the first leading '-'
1053 int start = ( fBuffer[0] == chDash ) ? fStart + 1 : fStart;
1056 // search for year separator '-'
1058 int yearSeparator = indexOf(start, fEnd, DATE_SEPARATOR);
1059 if ( yearSeparator == NOT_FOUND)
1060 throw XMLParserException("Year separator is missing or misplaced.");
1062 fValue[CentYear] = parseIntYear(yearSeparator);
1063 fStart = yearSeparator + 1; //skip the '-' and point to the first M
1066 //gonna check we have enough byte for month
1068 if ((fStart + 2) > fEnd )
1069 throw XMLParserException("No month in buffer.");
1071 fValue[Month] = parseInt(fStart, yearSeparator + 3);
1072 fStart += 2; //fStart points right after the MONTH
1077 void DateTime::parseTimeZone()
1079 if ( fStart < fEnd )
1081 int sign = findUTCSign(fStart);
1084 throw XMLParserException("Error in month parsing.");
1099 // Note: Assuming fStart points to the beginning of TimeZone section
1100 // fStart updated to meet fEnd
1102 void DateTime::getTimeZone(const int sign)
1105 if ( fBuffer[sign] == UTC_STD_CHAR )
1107 if ((sign + 1) != fEnd )
1109 throw XMLParserException("Error in parsing time zone.");
1116 // otherwise, it has to be this format
1121 if ( ( ( sign + TIMEZONE_SIZE + 1) != fEnd ) ||
1122 ( fBuffer[sign + 3] != TIMEZONE_SEPARATOR ) )
1124 throw XMLParserException("Error in parsing time zone.");
1127 fTimeZone[hh] = parseInt(sign+1, sign+3);
1128 fTimeZone[mm] = parseInt(sign+4, fEnd);
1133 // ---------------------------------------------------------------------------
1134 // Validator and normalizer
1135 // ---------------------------------------------------------------------------
1138 * If timezone present - normalize dateTime [E Adding durations to dateTimes]
1140 * @param date CCYY-MM-DDThh:mm:ss+03
1141 * @return CCYY-MM-DDThh:mm:ssZ
1143 void DateTime::normalize()
1146 if ((fValue[utc] == UTC_UNKNOWN) ||
1147 (fValue[utc] == UTC_STD) )
1150 int negate = (fValue[utc] == UTC_POS)? -1: 1;
1153 int temp = fValue[Minute] + negate * fTimeZone[mm];
1154 int carry = fQuotient(temp, 60);
1155 fValue[Minute] = mod(temp, 60, carry);
1158 temp = fValue[Hour] + negate * fTimeZone[hh] + carry;
1159 carry = fQuotient(temp, 24);
1160 fValue[Hour] = mod(temp, 24, carry);
1162 fValue[Day] += carry;
1166 temp = maxDayInMonthFor(fValue[CentYear], fValue[Month]);
1167 if (fValue[Day] < 1)
1169 fValue[Day] += maxDayInMonthFor(fValue[CentYear], fValue[Month] - 1);
1172 else if ( fValue[Day] > temp )
1174 fValue[Day] -= temp;
1182 temp = fValue[Month] + carry;
1183 fValue[Month] = modulo(temp, 1, 13);
1184 fValue[CentYear] += fQuotient(temp, 1, 13);
1187 // set to normalized
1188 fValue[utc] = UTC_STD;
1193 void DateTime::validateDateTime() const
1196 //REVISIT: should we throw an exception for not valid dates
1197 // or reporting an error message should be sufficient?
1198 if ( fValue[CentYear] == 0 )
1200 throw XMLParserException("The year \"0000\" is an illegal year value");
1203 if ( fValue[Month] < 1 ||
1204 fValue[Month] > 12 )
1206 throw XMLParserException("The month must have values 1 to 12");
1210 if ( fValue[Day] > maxDayInMonthFor( fValue[CentYear], fValue[Month]) ||
1213 throw XMLParserException("The day must have values 1 to 31");
1217 if ((fValue[Hour] < 0) ||
1218 (fValue[Hour] > 24) ||
1219 ((fValue[Hour] == 24) && ((fValue[Minute] !=0) ||
1220 (fValue[Second] !=0) ||
1221 (fMiliSecond !=0))))
1223 throw XMLParserException("Hour must have values 0-23");
1227 if ( fValue[Minute] < 0 ||
1228 fValue[Minute] > 59 )
1230 throw XMLParserException("Minute must have values 0-59");
1234 if ( fValue[Second] < 0 ||
1235 fValue[Second] > 60 )
1237 throw XMLParserException("Second must have values 0-60");
1240 //validate time-zone hours
1241 if ( (abs(fTimeZone[hh]) > 14) ||
1242 ((abs(fTimeZone[hh]) == 14) && (fTimeZone[mm] != 0)) )
1244 throw XMLParserException("Time zone should have range -14..+14");
1247 //validate time-zone minutes
1248 if ( abs(fTimeZone[mm]) > 59 )
1250 throw XMLParserException("Minute must have values 0-59");
1256 // -----------------------------------------------------------------------
1257 // locator and converter
1258 // -----------------------------------------------------------------------
1259 int DateTime::indexOf(const int start, const int end, const XMLCh ch) const
1261 for ( int i = start; i < end; i++ )
1262 if ( fBuffer[i] == ch )
1268 int DateTime::findUTCSign (const int start)
1271 for ( int index = start; index < fEnd; index++ )
1273 pos = XMLString::indexOf(UTC_SET, fBuffer[index]);
1274 if ( pos != NOT_FOUND)
1276 fValue[utc] = pos+1; // refer to utcType, there is 1 diff
1286 // start: starting point in fBuffer
1287 // end: ending point in fBuffer (exclusive)
1288 // fStart NOT updated
1290 int DateTime::parseInt(const int start, const int end) const
1292 unsigned int retVal = 0;
1293 for (int i=start; i < end; i++) {
1295 if (fBuffer[i] < chDigit_0 || fBuffer[i] > chDigit_9)
1296 throw XMLParserException("Invalid non-numeric characters.");
1298 retVal = (retVal * 10) + (unsigned int) (fBuffer[i] - chDigit_0);
1301 return (int) retVal;
1306 // start: pointing to the first digit after the '.'
1307 // end: pointing to one position after the last digit
1308 // fStart NOT updated
1310 double DateTime::parseMiliSecond(const int start, const int end) const
1313 unsigned int miliSecLen = (end-1) - (start-1) + 1; //to include the '.'
1314 XMLCh* miliSecData = new XMLCh[miliSecLen + 1];
1315 ArrayJanitor<XMLCh> janMili(miliSecData);
1316 XMLString::copyNString(miliSecData, &(fBuffer[start-1]), miliSecLen);
1317 *(miliSecData + miliSecLen) = chNull;
1319 auto_ptr_char nptr(miliSecData);
1320 size_t strLen = strlen(nptr.get());
1324 //printf("milisec=<%s>\n", nptr);
1326 double retVal = strtod(nptr.get(), &endptr);
1328 // check if all chars are valid char
1329 if ( (endptr - nptr.get()) != strLen)
1330 throw XMLParserException("Invalid non-numeric characters.");
1332 // we don't check underflow occurs since
1333 // nothing we can do about it.
1340 // Note: start from fStart
1342 // fStart NOT updated
1344 int DateTime::parseIntYear(const int end) const
1346 // skip the first leading '-'
1347 int start = ( fBuffer[0] == chDash ) ? fStart + 1 : fStart;
1349 int length = end - start;
1352 throw XMLParserException("Year must have 'CCYY' format");
1354 else if (length > 4 &&
1355 fBuffer[start] == chDigit_0)
1357 throw XMLParserException("Leading zeros are required if the year value would otherwise have fewer than four digits; otherwise they are forbidden.");
1360 bool negative = (fBuffer[0] == chDash);
1361 int yearVal = parseInt((negative ? 1 : 0), end);
1362 return ( negative ? (-1) * yearVal : yearVal );
1368 * 3.2.7.2 Canonical representation
1370 * Except for trailing fractional zero digits in the seconds representation,
1371 * '24:00:00' time representations, and timezone (for timezoned values),
1372 * the mapping from literals to values is one-to-one. Where there is more
1373 * than one possible representation, the canonical representation is as follows:
1374 * redundant trailing zero digits in fractional-second literals are prohibited.
1375 * An hour representation of '24' is prohibited. Timezoned values are canonically
1376 * represented by appending 'Z' to the nontimezoned representation. (All
1377 * timezoned dateTime values are UTC.)
1379 * .'24:00:00' -> '00:00:00'
1380 * .milisecond: trailing zeros removed
1384 XMLCh* DateTime::getDateTimeCanonicalRepresentation() const
1386 XMLCh *miliStartPtr, *miliEndPtr;
1387 searchMiliSeconds(miliStartPtr, miliEndPtr);
1388 size_t miliSecondsLen = miliEndPtr - miliStartPtr;
1390 XMLCh* retBuf = new XMLCh[21 + miliSecondsLen + 2];
1391 XMLCh* retPtr = retBuf;
1393 // (-?) cc+yy-mm-dd'T'hh:mm:ss'Z' ('.'s+)?
1396 int additionalLen = fillYearString(retPtr, CentYear);
1397 if(additionalLen != 0)
1399 // very bad luck; have to resize the buffer...
1400 XMLCh *tmpBuf = new XMLCh[additionalLen+21+miliSecondsLen +2];
1401 XMLString::moveChars(tmpBuf, retBuf, 4+additionalLen);
1402 retPtr = tmpBuf+(retPtr-retBuf);
1406 *retPtr++ = DATE_SEPARATOR;
1407 fillString(retPtr, Month, 2);
1408 *retPtr++ = DATE_SEPARATOR;
1409 fillString(retPtr, Day, 2);
1410 *retPtr++ = DATETIME_SEPARATOR;
1412 fillString(retPtr, Hour, 2);
1413 if (fValue[Hour] == 24)
1415 *(retPtr - 2) = chDigit_0;
1416 *(retPtr - 1) = chDigit_0;
1418 *retPtr++ = TIME_SEPARATOR;
1419 fillString(retPtr, Minute, 2);
1420 *retPtr++ = TIME_SEPARATOR;
1421 fillString(retPtr, Second, 2);
1425 *retPtr++ = chPeriod;
1426 XMLString::copyNString(retPtr, miliStartPtr, miliSecondsLen);
1427 retPtr += miliSecondsLen;
1430 *retPtr++ = UTC_STD_CHAR;
1439 * . either the time zone must be omitted or,
1440 * if present, the time zone must be Coordinated Universal Time (UTC) indicated by a "Z".
1442 * . Additionally, the canonical representation for midnight is 00:00:00.
1445 XMLCh* DateTime::getTimeCanonicalRepresentation() const
1447 XMLCh *miliStartPtr, *miliEndPtr;
1448 searchMiliSeconds(miliStartPtr, miliEndPtr);
1449 size_t miliSecondsLen = miliEndPtr - miliStartPtr;
1451 XMLCh* retBuf = new XMLCh[10 + miliSecondsLen + 2];
1452 XMLCh* retPtr = retBuf;
1454 // 'hh:mm:ss'Z' ('.'s+)?
1458 fillString(retPtr, Hour, 2);
1459 if (fValue[Hour] == 24)
1461 *(retPtr - 2) = chDigit_0;
1462 *(retPtr - 1) = chDigit_0;
1464 *retPtr++ = TIME_SEPARATOR;
1465 fillString(retPtr, Minute, 2);
1466 *retPtr++ = TIME_SEPARATOR;
1467 fillString(retPtr, Second, 2);
1471 *retPtr++ = chPeriod;
1472 XMLString::copyNString(retPtr, miliStartPtr, miliSecondsLen);
1473 retPtr += miliSecondsLen;
1476 *retPtr++ = UTC_STD_CHAR;
1482 void DateTime::fillString(XMLCh*& ptr, valueIndex ind, int expLen) const
1484 XMLCh strBuffer[16];
1485 assert(expLen < 16);
1486 XMLString::binToText(fValue[ind], strBuffer, expLen, 10);
1487 int actualLen = (int) XMLString::stringLen(strBuffer);
1489 //append leading zeros
1490 for (i = 0; i < expLen - actualLen; i++)
1495 for (i = 0; i < actualLen; i++)
1497 *ptr++ = strBuffer[i];
1502 int DateTime::fillYearString(XMLCh*& ptr, valueIndex ind) const
1504 XMLCh strBuffer[16];
1505 // let's hope we get no years of 15 digits...
1506 XMLString::binToText(fValue[ind], strBuffer, 15, 10);
1507 int actualLen = (int) XMLString::stringLen(strBuffer);
1508 // don't forget that years can be negative...
1509 int negativeYear = 0;
1510 if(strBuffer[0] == chDash)
1512 *ptr++ = strBuffer[0];
1516 //append leading zeros
1517 for (i = 0; i < 4 - actualLen+negativeYear; i++)
1522 for (i = negativeYear; i < actualLen; i++)
1524 *ptr++ = strBuffer[i];
1533 * .check if the rawData has the mili second component
1534 * .capture the substring
1537 void DateTime::searchMiliSeconds(XMLCh*& miliStartPtr, XMLCh*& miliEndPtr) const
1539 miliStartPtr = miliEndPtr = 0;
1541 int milisec = XMLString::indexOf(fBuffer, MILISECOND_SEPARATOR);
1545 miliStartPtr = fBuffer + milisec + 1;
1546 miliEndPtr = miliStartPtr;
1549 if ((*miliEndPtr < chDigit_0) || (*miliEndPtr > chDigit_9))
1555 //remove trailing zeros
1556 while( *(miliEndPtr - 1) == chDigit_0)
1562 void DateTime::setBuffer(const XMLCh* const aString)
1565 fEnd = (int) xercesc::XMLString::stringLen(aString);
1567 if (fEnd > fBufferMaxLen) {
1569 fBufferMaxLen = fEnd + 8;
1570 fBuffer = new XMLCh[fBufferMaxLen+1];
1572 memcpy(fBuffer, aString, (fEnd+1) * sizeof(XMLCh));
1576 void DateTime::reset()
1578 for ( int i=0; i < xercesc::XMLDateTime::TOTAL_SIZE; i++ )
1583 fTimeZone[hh] = fTimeZone[mm] = 0;
1590 void DateTime::copy(const DateTime& rhs)
1592 for ( int i = 0; i < xercesc::XMLDateTime::TOTAL_SIZE; i++ )
1593 fValue[i] = rhs.fValue[i];
1595 fMiliSecond = rhs.fMiliSecond;
1596 fHasTime = rhs.fHasTime;
1597 fTimeZone[hh] = rhs.fTimeZone[hh];
1598 fTimeZone[mm] = rhs.fTimeZone[mm];
1599 fStart = rhs.fStart;
1603 if (fEnd > fBufferMaxLen) {
1605 fBufferMaxLen = rhs.fBufferMaxLen;
1606 fBuffer = new XMLCh[fBufferMaxLen+1];
1608 memcpy(fBuffer, rhs.fBuffer, (fEnd+1) * sizeof(XMLCh));
1612 void DateTime::initParser()
1614 fStart = 0; // to ensure scan from the very first beginning
1615 // in case the pointer is updated accidentally by someone else.
1618 bool DateTime::isNormalized() const
1620 return (fValue[xercesc::XMLDateTime::utc] == xercesc::XMLDateTime::UTC_STD ? true : false);
1623 int DateTime::getRetVal(int c1, int c2)
1625 if ((c1 == xercesc::XMLDateTime::LESS_THAN && c2 == xercesc::XMLDateTime::GREATER_THAN) ||
1626 (c1 == xercesc::XMLDateTime::GREATER_THAN && c2 == xercesc::XMLDateTime::LESS_THAN))
1627 return xercesc::XMLDateTime::INDETERMINATE;
1629 return (c1 != xercesc::XMLDateTime::INDETERMINATE) ? c1 : c2;