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 "platform/PlatformExport.h" 36 #include "wtf/Forward.h" 37 #include "wtf/unicode/Unicode.h" 38 39 namespace WebCore { 40 41 // A DateComponents instance represents one of the following date and time combinations: 42 // * Month type: year-month 43 // * Date type: year-month-day 44 // * Week type: year-week 45 // * Time type: hour-minute-second-millisecond 46 // * DateTime or DateTimeLocal type: year-month-day hour-minute-second-millisecond 47 class PLATFORM_EXPORT DateComponents { 48 public: DateComponents()49 DateComponents() 50 : m_millisecond(0) 51 , m_second(0) 52 , m_minute(0) 53 , m_hour(0) 54 , m_monthDay(0) 55 , m_month(0) 56 , m_year(0) 57 , m_week(0) 58 , m_type(Invalid) 59 { 60 } 61 62 enum Type { 63 Invalid, 64 Date, 65 DateTime, 66 DateTimeLocal, 67 Month, 68 Time, 69 Week, 70 }; 71 millisecond()72 int millisecond() const { return m_millisecond; } second()73 int second() const { return m_second; } minute()74 int minute() const { return m_minute; } hour()75 int hour() const { return m_hour; } monthDay()76 int monthDay() const { return m_monthDay; } month()77 int month() const { return m_month; } fullYear()78 int fullYear() const { return m_year; } week()79 int week() const { return m_week; } type()80 Type type() const { return m_type; } 81 82 enum SecondFormat { 83 None, // Suppress the second part and the millisecond part if they are 0. 84 Second, // Always show the second part, and suppress the millisecond part if it is 0. 85 Millisecond // Always show the second part and the millisecond part. 86 }; 87 88 // Returns an ISO 8601 representation for this instance. 89 // The format argument is valid for DateTime, DateTimeLocal, and Time types. 90 String toString(SecondFormat format = None) const; 91 92 // parse*() and setMillisecondsSince*() functions are initializers for an 93 // DateComponents instance. If these functions return false, the instance 94 // might be invalid. 95 96 // The following six functions parse the input 'src' whose length is 97 // 'length', and updates some fields of this instance. The parsing starts at 98 // src[start] and examines characters before src[length]. 99 // 'src' must be non-null. The 'src' string doesn't need to be 100 // null-terminated. 101 // The functions return true if the parsing succeeds, and set 'end' to the 102 // next index after the last consumed. Extra leading characters cause parse 103 // failures, and the trailing extra characters don't cause parse failures. 104 105 // Sets year and month. 106 bool parseMonth(const String&, unsigned start, unsigned& end); 107 // Sets year, month and monthDay. 108 bool parseDate(const String&, unsigned start, unsigned& end); 109 // Sets year and week. 110 bool parseWeek(const String&, unsigned start, unsigned& end); 111 // Sets hour, minute, second and millisecond. 112 bool parseTime(const String&, unsigned start, unsigned& end); 113 // Sets year, month, monthDay, hour, minute, second and millisecond. 114 bool parseDateTimeLocal(const String&, unsigned start, unsigned& end); 115 // Sets year, month, monthDay, hour, minute, second and millisecond, and adjusts timezone. 116 bool parseDateTime(const String&, unsigned start, unsigned& end); 117 118 // The following setMillisecondsSinceEpochFor*() functions take 119 // the number of milliseconds since 1970-01-01 00:00:00.000 UTC as 120 // the argument, and update all fields for the corresponding 121 // DateComponents type. The functions return true if it succeeds, and 122 // false if they fail. 123 124 // For Date type. Updates m_year, m_month and m_monthDay. 125 bool setMillisecondsSinceEpochForDate(double ms); 126 // For DateTime type. Updates m_year, m_month, m_monthDay, m_hour, m_minute, m_second and m_millisecond. 127 bool setMillisecondsSinceEpochForDateTime(double ms); 128 // For DateTimeLocal type. Updates m_year, m_month, m_monthDay, m_hour, m_minute, m_second and m_millisecond. 129 bool setMillisecondsSinceEpochForDateTimeLocal(double ms); 130 // For Month type. Updates m_year and m_month. 131 bool setMillisecondsSinceEpochForMonth(double ms); 132 // For Week type. Updates m_year and m_week. 133 bool setMillisecondsSinceEpochForWeek(double ms); 134 135 // For Time type. Updates m_hour, m_minute, m_second and m_millisecond. 136 bool setMillisecondsSinceMidnight(double ms); 137 138 // Another initializer for Month type. Updates m_year and m_month. 139 bool setMonthsSinceEpoch(double months); 140 141 // Returns the number of milliseconds from 1970-01-01 00:00:00 UTC. 142 // For a DateComponents initialized with parseDateTimeLocal(), 143 // millisecondsSinceEpoch() returns a value for UTC timezone. 144 double millisecondsSinceEpoch() const; 145 // Returns the number of months from 1970-01. 146 // Do not call this for types other than Month. 147 double monthsSinceEpoch() const; invalidMilliseconds()148 static inline double invalidMilliseconds() { return std::numeric_limits<double>::quiet_NaN(); } 149 150 // Minimum and maxmimum limits for setMillisecondsSince*(), 151 // setMonthsSinceEpoch(), millisecondsSinceEpoch(), and monthsSinceEpoch(). minimumDate()152 static inline double minimumDate() { return -62135596800000.0; } // 0001-01-01T00:00Z minimumDateTime()153 static inline double minimumDateTime() { return -62135596800000.0; } // ditto. minimumMonth()154 static inline double minimumMonth() { return (1 - 1970) * 12.0 + 1 - 1; } // 0001-01 minimumTime()155 static inline double minimumTime() { return 0; } // 00:00:00.000 minimumWeek()156 static inline double minimumWeek() { return -62135596800000.0; } // 0001-01-01, the first Monday of 0001. maximumDate()157 static inline double maximumDate() { return 8640000000000000.0; } // 275760-09-13T00:00Z maximumDateTime()158 static inline double maximumDateTime() { return 8640000000000000.0; } // ditto. maximumMonth()159 static inline double maximumMonth() { return (275760 - 1970) * 12.0 + 9 - 1; } // 275760-09 maximumTime()160 static inline double maximumTime() { return 86399999; } // 23:59:59.999 maximumWeek()161 static inline double maximumWeek() { return 8639999568000000.0; } // 275760-09-08, the Monday of the week including 275760-09-13. 162 163 // HTML5 uses ISO-8601 format with year >= 1. Gregorian calendar started in 164 // 1582. However, we need to support 0001-01-01 in Gregorian calendar rule. minimumYear()165 static inline int minimumYear() { return 1; } 166 // Date in ECMAScript can't represent dates later than 275760-09-13T00:00Z. 167 // So, we have the same upper limit in HTML5 date/time types. maximumYear()168 static inline int maximumYear() { return 275760; } 169 static const int minimumWeekNumber; 170 static const int maximumWeekNumber; 171 172 private: 173 // Returns the maximum week number in this DateComponents's year. 174 // The result is either of 52 and 53. 175 int maxWeekNumberInYear() const; 176 bool parseYear(const String&, unsigned start, unsigned& end); 177 bool addDay(int); 178 bool addMinute(int); 179 bool parseTimeZone(const String&, unsigned start, unsigned& end); 180 // Helper for millisecondsSinceEpoch(). 181 double millisecondsSinceEpochForTime() const; 182 // Helpers for setMillisecondsSinceEpochFor*(). 183 bool setMillisecondsSinceEpochForDateInternal(double ms); 184 void setMillisecondsSinceMidnightInternal(double ms); 185 // Helper for toString(). 186 String toStringForTime(SecondFormat) const; 187 188 // m_weekDay values 189 enum { 190 Sunday = 0, 191 Monday, 192 Tuesday, 193 Wednesday, 194 Thursday, 195 Friday, 196 Saturday, 197 }; 198 199 int m_millisecond; // 0 - 999 200 int m_second; 201 int m_minute; 202 int m_hour; 203 int m_monthDay; // 1 - 31 204 int m_month; // 0:January - 11:December 205 int m_year; // 1582 - 206 int m_week; // 1 - 53 207 208 Type m_type; 209 }; 210 211 212 } // namespace WebCore 213 214 #endif // DateComponents_h 215