1 /* 2 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef DateComponents_h 32 #define DateComponents_h 33 34 #include <limits> 35 #include <wtf/Forward.h> 36 #include <wtf/unicode/Unicode.h> 37 38 namespace WebCore { 39 40 // A DateComponents instance represents one of the following date and time combinations: 41 // * Month type: year-month 42 // * Date type: year-month-day 43 // * Week type: year-week 44 // * Time type: hour-minute-second-millisecond 45 // * DateTime or DateTimeLocal type: year-month-day hour-minute-second-millisecond 46 class DateComponents { 47 public: DateComponents()48 DateComponents() 49 : m_millisecond(0) 50 , m_second(0) 51 , m_minute(0) 52 , m_hour(0) 53 , m_monthDay(0) 54 , m_month(0) 55 , m_year(0) 56 , m_week(0) 57 , m_type(Invalid) 58 { 59 } 60 millisecond()61 int millisecond() const { return m_millisecond; } second()62 int second() const { return m_second; } minute()63 int minute() const { return m_minute; } hour()64 int hour() const { return m_hour; } monthDay()65 int monthDay() const { return m_monthDay; } month()66 int month() const { return m_month; } fullYear()67 int fullYear() const { return m_year; } week()68 int week() const { return m_week; } 69 70 enum SecondFormat { 71 None, // Suppress the second part and the millisecond part if they are 0. 72 Second, // Always show the second part, and suppress the millisecond part if it is 0. 73 Millisecond // Always show the second part and the millisecond part. 74 }; 75 76 // Returns an ISO 8601 representation for this instance. 77 // The format argument is valid for DateTime, DateTimeLocal, and Time types. 78 String toString(SecondFormat format = None) const; 79 80 // parse*() and setMillisecondsSince*() functions are initializers for an 81 // DateComponents instance. If these functions return false, the instance 82 // might be invalid. 83 84 // The following six functions parse the input 'src' whose length is 85 // 'length', and updates some fields of this instance. The parsing starts at 86 // src[start] and examines characters before src[length]. 87 // 'src' must be non-null. The 'src' string doesn't need to be 88 // null-terminated. 89 // The functions return true if the parsing succeeds, and set 'end' to the 90 // next index after the last consumed. Extra leading characters cause parse 91 // failures, and the trailing extra characters don't cause parse failures. 92 93 // Sets year and month. 94 bool parseMonth(const UChar* src, unsigned length, unsigned start, unsigned& end); 95 // Sets year, month and monthDay. 96 bool parseDate(const UChar* src, unsigned length, unsigned start, unsigned& end); 97 // Sets year and week. 98 bool parseWeek(const UChar* src, unsigned length, unsigned start, unsigned& end); 99 // Sets hour, minute, second and millisecond. 100 bool parseTime(const UChar* src, unsigned length, unsigned start, unsigned& end); 101 // Sets year, month, monthDay, hour, minute, second and millisecond. 102 bool parseDateTimeLocal(const UChar* src, unsigned length, unsigned start, unsigned& end); 103 // Sets year, month, monthDay, hour, minute, second and millisecond, and adjusts timezone. 104 bool parseDateTime(const UChar* src, unsigned length, unsigned start, unsigned& end); 105 106 // The following setMillisecondsSinceEpochFor*() functions take 107 // the number of milliseconds since 1970-01-01 00:00:00.000 UTC as 108 // the argument, and update all fields for the corresponding 109 // DateComponents type. The functions return true if it succeeds, and 110 // false if they fail. 111 112 // For Date type. Updates m_year, m_month and m_monthDay. 113 bool setMillisecondsSinceEpochForDate(double ms); 114 // For DateTime type. Updates m_year, m_month, m_monthDay, m_hour, m_minute, m_second and m_millisecond. 115 bool setMillisecondsSinceEpochForDateTime(double ms); 116 // For DateTimeLocal type. Updates m_year, m_month, m_monthDay, m_hour, m_minute, m_second and m_millisecond. 117 bool setMillisecondsSinceEpochForDateTimeLocal(double ms); 118 // For Month type. Updates m_year and m_month. 119 bool setMillisecondsSinceEpochForMonth(double ms); 120 // For Week type. Updates m_year and m_week. 121 bool setMillisecondsSinceEpochForWeek(double ms); 122 123 // For Time type. Updates m_hour, m_minute, m_second and m_millisecond. 124 bool setMillisecondsSinceMidnight(double ms); 125 126 // Another initializer for Month type. Updates m_year and m_month. 127 bool setMonthsSinceEpoch(double months); 128 129 // Returns the number of milliseconds from 1970-01-01 00:00:00 UTC. 130 // For a DateComponents initialized with parseDateTimeLocal(), 131 // millisecondsSinceEpoch() returns a value for UTC timezone. 132 double millisecondsSinceEpoch() const; 133 // Returns the number of months from 1970-01. 134 // Do not call this for types other than Month. 135 double monthsSinceEpoch() const; invalidMilliseconds()136 static inline double invalidMilliseconds() { return std::numeric_limits<double>::quiet_NaN(); } 137 138 // Minimum and maxmimum limits for setMillisecondsSince*(), 139 // setMonthsSinceEpoch(), millisecondsSinceEpoch(), and monthsSinceEpoch(). minimumDate()140 static inline double minimumDate() { return -62135596800000.0; } // 0001-01-01T00:00Z minimumDateTime()141 static inline double minimumDateTime() { return -62135596800000.0; } // ditto. minimumMonth()142 static inline double minimumMonth() { return (1 - 1970) * 12.0 + 1 - 1; } // 0001-01 minimumTime()143 static inline double minimumTime() { return 0; } // 00:00:00.000 minimumWeek()144 static inline double minimumWeek() { return -62135596800000.0; } // 0001-01-01, the first Monday of 0001. maximumDate()145 static inline double maximumDate() { return 8640000000000000.0; } // 275760-09-13T00:00Z maximumDateTime()146 static inline double maximumDateTime() { return 8640000000000000.0; } // ditto. maximumMonth()147 static inline double maximumMonth() { return (275760 - 1970) * 12.0 + 9 - 1; } // 275760-09 maximumTime()148 static inline double maximumTime() { return 86399999; } // 23:59:59.999 maximumWeek()149 static inline double maximumWeek() { return 8639999568000000.0; } // 275760-09-08, the Monday of the week including 275760-09-13. 150 151 private: 152 // Returns the maximum week number in this DateComponents's year. 153 // The result is either of 52 and 53. 154 int maxWeekNumberInYear() const; 155 bool parseYear(const UChar* src, unsigned length, unsigned start, unsigned& end); 156 bool addDay(int); 157 bool addMinute(int); 158 bool parseTimeZone(const UChar* src, unsigned length, unsigned start, unsigned& end); 159 // Helper for millisecondsSinceEpoch(). 160 double millisecondsSinceEpochForTime() const; 161 // Helpers for setMillisecondsSinceEpochFor*(). 162 bool setMillisecondsSinceEpochForDateInternal(double ms); 163 void setMillisecondsSinceMidnightInternal(double ms); 164 // Helper for toString(). 165 String toStringForTime(SecondFormat) const; 166 167 // m_weekDay values 168 enum { 169 Sunday = 0, 170 Monday, 171 Tuesday, 172 Wednesday, 173 Thursday, 174 Friday, 175 Saturday, 176 }; 177 178 int m_millisecond; // 0 - 999 179 int m_second; 180 int m_minute; 181 int m_hour; 182 int m_monthDay; // 1 - 31 183 int m_month; // 0:January - 11:December 184 int m_year; // 1582 - 185 int m_week; // 1 - 53 186 187 enum Type { 188 Invalid, 189 Date, 190 DateTime, 191 DateTimeLocal, 192 Month, 193 Time, 194 Week, 195 }; 196 Type m_type; 197 }; 198 199 200 } // namespace WebCore 201 202 #endif // DateComponents_h 203