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