2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef DateComponents_h
32 #define DateComponents_h
35 #include "platform/PlatformExport.h"
36 #include "wtf/Forward.h"
37 #include "wtf/unicode/Unicode.h"
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 {
72 int millisecond() const { return m_millisecond; }
73 int second() const { return m_second; }
74 int minute() const { return m_minute; }
75 int hour() const { return m_hour; }
76 int monthDay() const { return m_monthDay; }
78 int month() const { return m_month; }
79 int fullYear() const { return m_year; }
80 int week() const { return m_week; }
81 Type type() const { return m_type; }
84 None, // Suppress the second part and the millisecond part if they are 0.
85 Second, // Always show the second part, and suppress the millisecond part if it is 0.
86 Millisecond // Always show the second part and the millisecond part.
89 // Returns an ISO 8601 representation for this instance.
90 // The format argument is valid for DateTime, DateTimeLocal, and Time types.
91 String toString(SecondFormat format = None) const;
93 // parse*() and setMillisecondsSince*() functions are initializers for an
94 // DateComponents instance. If these functions return false, the instance
97 // The following six functions parse the input 'src' whose length is
98 // 'length', and updates some fields of this instance. The parsing starts at
99 // src[start] and examines characters before src[length].
100 // 'src' must be non-null. The 'src' string doesn't need to be
102 // The functions return true if the parsing succeeds, and set 'end' to the
103 // next index after the last consumed. Extra leading characters cause parse
104 // failures, and the trailing extra characters don't cause parse failures.
106 // Sets year and month.
107 bool parseMonth(const String&, unsigned start, unsigned& end);
108 // Sets year, month and monthDay.
109 bool parseDate(const String&, unsigned start, unsigned& end);
110 // Sets year and week.
111 bool parseWeek(const String&, unsigned start, unsigned& end);
112 // Sets hour, minute, second and millisecond.
113 bool parseTime(const String&, unsigned start, unsigned& end);
114 // Sets year, month, monthDay, hour, minute, second and millisecond.
115 bool parseDateTimeLocal(const String&, unsigned start, unsigned& end);
116 // Sets year, month, monthDay, hour, minute, second and millisecond, and adjusts timezone.
117 bool parseDateTime(const String&, unsigned start, unsigned& end);
119 // The following setMillisecondsSinceEpochFor*() functions take
120 // the number of milliseconds since 1970-01-01 00:00:00.000 UTC as
121 // the argument, and update all fields for the corresponding
122 // DateComponents type. The functions return true if it succeeds, and
123 // false if they fail.
125 // For Date type. Updates m_year, m_month and m_monthDay.
126 bool setMillisecondsSinceEpochForDate(double ms);
127 // For DateTime type. Updates m_year, m_month, m_monthDay, m_hour, m_minute, m_second and m_millisecond.
128 bool setMillisecondsSinceEpochForDateTime(double ms);
129 // For DateTimeLocal type. Updates m_year, m_month, m_monthDay, m_hour, m_minute, m_second and m_millisecond.
130 bool setMillisecondsSinceEpochForDateTimeLocal(double ms);
131 // For Month type. Updates m_year and m_month.
132 bool setMillisecondsSinceEpochForMonth(double ms);
133 // For Week type. Updates m_year and m_week.
134 bool setMillisecondsSinceEpochForWeek(double ms);
136 // For Time type. Updates m_hour, m_minute, m_second and m_millisecond.
137 bool setMillisecondsSinceMidnight(double ms);
139 // Another initializer for Month type. Updates m_year and m_month.
140 bool setMonthsSinceEpoch(double months);
141 // Another initializer for Week type. Updates m_year and m_week.
142 bool setWeek(int year, int weekNumber);
144 // Returns the number of milliseconds from 1970-01-01 00:00:00 UTC.
145 // For a DateComponents initialized with parseDateTimeLocal(),
146 // millisecondsSinceEpoch() returns a value for UTC timezone.
147 double millisecondsSinceEpoch() const;
148 // Returns the number of months from 1970-01.
149 // Do not call this for types other than Month.
150 double monthsSinceEpoch() const;
151 static inline double invalidMilliseconds() { return std::numeric_limits<double>::quiet_NaN(); }
153 // Minimum and maxmimum limits for setMillisecondsSince*(),
154 // setMonthsSinceEpoch(), millisecondsSinceEpoch(), and monthsSinceEpoch().
155 static inline double minimumDate() { return -62135596800000.0; } // 0001-01-01T00:00Z
156 static inline double minimumDateTime() { return -62135596800000.0; } // ditto.
157 static inline double minimumMonth() { return (1 - 1970) * 12.0 + 1 - 1; } // 0001-01
158 static inline double minimumTime() { return 0; } // 00:00:00.000
159 static inline double minimumWeek() { return -62135596800000.0; } // 0001-01-01, the first Monday of 0001.
160 static inline double maximumDate() { return 8640000000000000.0; } // 275760-09-13T00:00Z
161 static inline double maximumDateTime() { return 8640000000000000.0; } // ditto.
162 static inline double maximumMonth() { return (275760 - 1970) * 12.0 + 9 - 1; } // 275760-09
163 static inline double maximumTime() { return 86399999; } // 23:59:59.999
164 static inline double maximumWeek() { return 8639999568000000.0; } // 275760-09-08, the Monday of the week including 275760-09-13.
166 // HTML5 uses ISO-8601 format with year >= 1. Gregorian calendar started in
167 // 1582. However, we need to support 0001-01-01 in Gregorian calendar rule.
168 static inline int minimumYear() { return 1; }
169 // Date in ECMAScript can't represent dates later than 275760-09-13T00:00Z.
170 // So, we have the same upper limit in HTML5 date/time types.
171 static inline int maximumYear() { return 275760; }
172 static const int minimumWeekNumber;
173 static const int maximumWeekNumber;
176 // Returns the maximum week number in this DateComponents's year.
177 // The result is either of 52 and 53.
178 int maxWeekNumberInYear() const;
179 bool parseYear(const String&, unsigned start, unsigned& end);
182 bool parseTimeZone(const String&, unsigned start, unsigned& end);
183 // Helper for millisecondsSinceEpoch().
184 double millisecondsSinceEpochForTime() const;
185 // Helpers for setMillisecondsSinceEpochFor*().
186 bool setMillisecondsSinceEpochForDateInternal(double ms);
187 void setMillisecondsSinceMidnightInternal(double ms);
188 // Helper for toString().
189 String toStringForTime(SecondFormat) const;
202 int m_millisecond; // 0 - 999
206 int m_monthDay; // 1 - 31
207 int m_month; // 0:January - 11:December
208 int m_year; // 1582 -
209 int m_week; // 1 - 53
217 #endif // DateComponents_h