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