Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / DateComponents.cpp
blob6b67e1d813ab69abdf38acebf21e882713445274
1 /*
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
6 * met:
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.
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 #include "config.h"
32 #include "platform/DateComponents.h"
34 #include <limits.h>
35 #include "wtf/ASCIICType.h"
36 #include "wtf/DateMath.h"
37 #include "wtf/MathExtras.h"
38 #include "wtf/text/WTFString.h"
40 namespace blink {
42 // HTML5 specification defines minimum week of year is one.
43 const int DateComponents::minimumWeekNumber = 1;
45 // HTML5 specification defines maximum week of year is 53.
46 const int DateComponents::maximumWeekNumber = 53;
48 static const int maximumMonthInMaximumYear = 8; // This is September, since months are 0 based.
49 static const int maximumDayInMaximumMonth = 13;
50 static const int maximumWeekInMaximumYear = 37; // The week of 275760-09-13
52 static const int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
54 // 'month' is 0-based.
55 static int maxDayOfMonth(int year, int month)
57 if (month != 1) // February?
58 return daysInMonth[month];
59 return isLeapYear(year) ? 29 : 28;
62 // 'month' is 0-based.
63 static int dayOfWeek(int year, int month, int day)
65 int shiftedMonth = month + 2;
66 // 2:January, 3:Feburuary, 4:March, ...
68 // Zeller's congruence
69 if (shiftedMonth <= 3) {
70 shiftedMonth += 12;
71 year--;
73 // 4:March, ..., 14:January, 15:February
75 int highYear = year / 100;
76 int lowYear = year % 100;
77 // We add 6 to make the result Sunday-origin.
78 int result = (day + 13 * shiftedMonth / 5 + lowYear + lowYear / 4 + highYear / 4 + 5 * highYear + 6) % 7;
79 return result;
82 int DateComponents::weekDay() const
84 return dayOfWeek(m_year, m_month, m_monthDay);
87 int DateComponents::maxWeekNumberInYear() const
89 int day = dayOfWeek(m_year, 0, 1); // January 1.
90 return day == Thursday || (day == Wednesday && isLeapYear(m_year)) ? maximumWeekNumber : maximumWeekNumber - 1;
93 static unsigned countDigits(const String& src, unsigned start)
95 unsigned index = start;
96 for (; index < src.length(); ++index) {
97 if (!isASCIIDigit(src[index]))
98 break;
100 return index - start;
103 // Very strict integer parser. Do not allow leading or trailing whitespace unlike charactersToIntStrict().
104 static bool toInt(const String& src, unsigned parseStart, unsigned parseLength, int& out)
106 if (parseStart + parseLength > src.length() || !parseLength)
107 return false;
108 int value = 0;
109 unsigned current = parseStart;
110 unsigned end = current + parseLength;
112 // We don't need to handle negative numbers for ISO 8601.
113 for (; current < end; ++current) {
114 if (!isASCIIDigit(src[current]))
115 return false;
116 int digit = src[current] - '0';
117 if (value > (INT_MAX - digit) / 10) // Check for overflow.
118 return false;
119 value = value * 10 + digit;
121 out = value;
122 return true;
125 bool DateComponents::parseYear(const String& src, unsigned start, unsigned& end)
127 unsigned digitsLength = countDigits(src, start);
128 // Needs at least 4 digits according to the standard.
129 if (digitsLength < 4)
130 return false;
131 int year;
132 if (!toInt(src, start, digitsLength, year))
133 return false;
134 if (year < minimumYear() || year > maximumYear())
135 return false;
136 m_year = year;
137 end = start + digitsLength;
138 return true;
141 static bool withinHTMLDateLimits(int year, int month)
143 if (year < DateComponents::minimumYear())
144 return false;
145 if (year < DateComponents::maximumYear())
146 return true;
147 return month <= maximumMonthInMaximumYear;
150 static bool withinHTMLDateLimits(int year, int month, int monthDay)
152 if (year < DateComponents::minimumYear())
153 return false;
154 if (year < DateComponents::maximumYear())
155 return true;
156 if (month < maximumMonthInMaximumYear)
157 return true;
158 return monthDay <= maximumDayInMaximumMonth;
161 static bool withinHTMLDateLimits(int year, int month, int monthDay, int hour, int minute, int second, int millisecond)
163 if (year < DateComponents::minimumYear())
164 return false;
165 if (year < DateComponents::maximumYear())
166 return true;
167 if (month < maximumMonthInMaximumYear)
168 return true;
169 if (monthDay < maximumDayInMaximumMonth)
170 return true;
171 if (monthDay > maximumDayInMaximumMonth)
172 return false;
173 // (year, month, monthDay) = (maximumYear, maximumMonthInMaximumYear, maximumDayInMaximumMonth)
174 return !hour && !minute && !second && !millisecond;
177 bool DateComponents::addDay(int dayDiff)
179 ASSERT(m_monthDay);
181 int day = m_monthDay + dayDiff;
182 if (day > maxDayOfMonth(m_year, m_month)) {
183 day = m_monthDay;
184 int year = m_year;
185 int month = m_month;
186 int maxDay = maxDayOfMonth(year, month);
187 for (; dayDiff > 0; --dayDiff) {
188 ++day;
189 if (day > maxDay) {
190 day = 1;
191 ++month;
192 if (month >= 12) { // month is 0-origin.
193 month = 0;
194 ++year;
196 maxDay = maxDayOfMonth(year, month);
199 if (!withinHTMLDateLimits(year, month, day))
200 return false;
201 m_year = year;
202 m_month = month;
203 } else if (day < 1) {
204 int month = m_month;
205 int year = m_year;
206 day = m_monthDay;
207 for (; dayDiff < 0; ++dayDiff) {
208 --day;
209 if (day < 1) {
210 --month;
211 if (month < 0) {
212 month = 11;
213 --year;
215 day = maxDayOfMonth(year, month);
218 if (!withinHTMLDateLimits(year, month, day))
219 return false;
220 m_year = year;
221 m_month = month;
222 } else {
223 if (!withinHTMLDateLimits(m_year, m_month, day))
224 return false;
226 m_monthDay = day;
227 return true;
230 bool DateComponents::addMinute(int minute)
232 // This function is used to adjust timezone offset. So m_year, m_month,
233 // m_monthDay have values between the lower and higher limits.
234 ASSERT(withinHTMLDateLimits(m_year, m_month, m_monthDay));
236 int carry;
237 // minute can be negative or greater than 59.
238 minute += m_minute;
239 if (minute > 59) {
240 carry = minute / 60;
241 minute = minute % 60;
242 } else if (minute < 0) {
243 carry = (59 - minute) / 60;
244 minute += carry * 60;
245 carry = -carry;
246 ASSERT(minute >= 0 && minute <= 59);
247 } else {
248 if (!withinHTMLDateLimits(m_year, m_month, m_monthDay, m_hour, minute, m_second, m_millisecond))
249 return false;
250 m_minute = minute;
251 return true;
254 int hour = m_hour + carry;
255 if (hour > 23) {
256 carry = hour / 24;
257 hour = hour % 24;
258 } else if (hour < 0) {
259 carry = (23 - hour) / 24;
260 hour += carry * 24;
261 carry = -carry;
262 ASSERT(hour >= 0 && hour <= 23);
263 } else {
264 if (!withinHTMLDateLimits(m_year, m_month, m_monthDay, hour, minute, m_second, m_millisecond))
265 return false;
266 m_minute = minute;
267 m_hour = hour;
268 return true;
270 if (!addDay(carry))
271 return false;
272 if (!withinHTMLDateLimits(m_year, m_month, m_monthDay, hour, minute, m_second, m_millisecond))
273 return false;
274 m_minute = minute;
275 m_hour = hour;
276 return true;
279 // Parses a timezone part, and adjust year, month, monthDay, hour, minute, second, millisecond.
280 bool DateComponents::parseTimeZone(const String& src, unsigned start, unsigned& end)
282 if (start >= src.length())
283 return false;
284 unsigned index = start;
285 if (src[index] == 'Z') {
286 end = index + 1;
287 return true;
290 bool minus;
291 if (src[index] == '+')
292 minus = false;
293 else if (src[index] == '-')
294 minus = true;
295 else
296 return false;
297 ++index;
299 int hour;
300 int minute;
301 if (!toInt(src, index, 2, hour) || hour < 0 || hour > 23)
302 return false;
303 index += 2;
305 if (index >= src.length() || src[index] != ':')
306 return false;
307 ++index;
309 if (!toInt(src, index, 2, minute) || minute < 0 || minute > 59)
310 return false;
311 index += 2;
313 if (minus) {
314 hour = -hour;
315 minute = -minute;
318 // Subtract the timezone offset.
319 if (!addMinute(-(hour * 60 + minute)))
320 return false;
321 end = index;
322 return true;
325 bool DateComponents::parseMonth(const String& src, unsigned start, unsigned& end)
327 unsigned index;
328 if (!parseYear(src, start, index))
329 return false;
330 if (index >= src.length() || src[index] != '-')
331 return false;
332 ++index;
334 int month;
335 if (!toInt(src, index, 2, month) || month < 1 || month > 12)
336 return false;
337 --month;
338 if (!withinHTMLDateLimits(m_year, month))
339 return false;
340 m_month = month;
341 end = index + 2;
342 m_type = Month;
343 return true;
346 bool DateComponents::parseDate(const String& src, unsigned start, unsigned& end)
348 unsigned index;
349 if (!parseMonth(src, start, index))
350 return false;
351 // '-' and 2-digits are needed.
352 if (index + 2 >= src.length())
353 return false;
354 if (src[index] != '-')
355 return false;
356 ++index;
358 int day;
359 if (!toInt(src, index, 2, day) || day < 1 || day > maxDayOfMonth(m_year, m_month))
360 return false;
361 if (!withinHTMLDateLimits(m_year, m_month, day))
362 return false;
363 m_monthDay = day;
364 end = index + 2;
365 m_type = Date;
366 return true;
369 bool DateComponents::parseWeek(const String& src, unsigned start, unsigned& end)
371 unsigned index;
372 if (!parseYear(src, start, index))
373 return false;
375 // 4 characters ('-' 'W' digit digit) are needed.
376 if (index + 3 >= src.length())
377 return false;
378 if (src[index] != '-')
379 return false;
380 ++index;
381 if (src[index] != 'W')
382 return false;
383 ++index;
385 int week;
386 if (!toInt(src, index, 2, week) || week < minimumWeekNumber || week > maxWeekNumberInYear())
387 return false;
388 if (m_year == maximumYear() && week > maximumWeekInMaximumYear)
389 return false;
390 m_week = week;
391 end = index + 2;
392 m_type = Week;
393 return true;
396 bool DateComponents::parseTime(const String& src, unsigned start, unsigned& end)
398 int hour;
399 if (!toInt(src, start, 2, hour) || hour < 0 || hour > 23)
400 return false;
401 unsigned index = start + 2;
402 if (index >= src.length())
403 return false;
404 if (src[index] != ':')
405 return false;
406 ++index;
408 int minute;
409 if (!toInt(src, index, 2, minute) || minute < 0 || minute > 59)
410 return false;
411 index += 2;
413 int second = 0;
414 int millisecond = 0;
415 // Optional second part.
416 // Do not return with false because the part is optional.
417 if (index + 2 < src.length() && src[index] == ':') {
418 if (toInt(src, index + 1, 2, second) && second >= 0 && second <= 59) {
419 index += 3;
421 // Optional fractional second part.
422 if (index < src.length() && src[index] == '.') {
423 unsigned digitsLength = countDigits(src, index + 1);
424 if (digitsLength > 0) {
425 ++index;
426 bool ok;
427 if (digitsLength == 1) {
428 ok = toInt(src, index, 1, millisecond);
429 millisecond *= 100;
430 } else if (digitsLength == 2) {
431 ok = toInt(src, index, 2, millisecond);
432 millisecond *= 10;
433 } else { // digitsLength >= 3
434 ok = toInt(src, index, 3, millisecond);
436 ASSERT_UNUSED(ok, ok);
437 index += digitsLength;
442 m_hour = hour;
443 m_minute = minute;
444 m_second = second;
445 m_millisecond = millisecond;
446 end = index;
447 m_type = Time;
448 return true;
451 bool DateComponents::parseDateTimeLocal(const String& src, unsigned start, unsigned& end)
453 unsigned index;
454 if (!parseDate(src, start, index))
455 return false;
456 if (index >= src.length())
457 return false;
458 if (src[index] != 'T')
459 return false;
460 ++index;
461 if (!parseTime(src, index, end))
462 return false;
463 if (!withinHTMLDateLimits(m_year, m_month, m_monthDay, m_hour, m_minute, m_second, m_millisecond))
464 return false;
465 m_type = DateTimeLocal;
466 return true;
469 static inline double positiveFmod(double value, double divider)
471 double remainder = fmod(value, divider);
472 return remainder < 0 ? remainder + divider : remainder;
475 void DateComponents::setMillisecondsSinceMidnightInternal(double msInDay)
477 ASSERT(msInDay >= 0 && msInDay < msPerDay);
478 m_millisecond = static_cast<int>(fmod(msInDay, msPerSecond));
479 double value = std::floor(msInDay / msPerSecond);
480 m_second = static_cast<int>(fmod(value, secondsPerMinute));
481 value = std::floor(value / secondsPerMinute);
482 m_minute = static_cast<int>(fmod(value, minutesPerHour));
483 m_hour = static_cast<int>(value / minutesPerHour);
486 bool DateComponents::setMillisecondsSinceEpochForDateInternal(double ms)
488 m_year = msToYear(ms);
489 int yearDay = dayInYear(ms, m_year);
490 m_month = monthFromDayInYear(yearDay, isLeapYear(m_year));
491 m_monthDay = dayInMonthFromDayInYear(yearDay, isLeapYear(m_year));
492 return true;
495 bool DateComponents::setMillisecondsSinceEpochForDate(double ms)
497 m_type = Invalid;
498 if (!std::isfinite(ms))
499 return false;
500 if (!setMillisecondsSinceEpochForDateInternal(round(ms)))
501 return false;
502 if (!withinHTMLDateLimits(m_year, m_month, m_monthDay))
503 return false;
504 m_type = Date;
505 return true;
508 bool DateComponents::setMillisecondsSinceEpochForDateTime(double ms)
510 m_type = Invalid;
511 if (!std::isfinite(ms))
512 return false;
513 ms = round(ms);
514 setMillisecondsSinceMidnightInternal(positiveFmod(ms, msPerDay));
515 if (!setMillisecondsSinceEpochForDateInternal(ms))
516 return false;
517 if (!withinHTMLDateLimits(m_year, m_month, m_monthDay, m_hour, m_minute, m_second, m_millisecond))
518 return false;
519 m_type = DateTime;
520 return true;
523 bool DateComponents::setMillisecondsSinceEpochForDateTimeLocal(double ms)
525 // Internal representation of DateTimeLocal is the same as DateTime except m_type.
526 if (!setMillisecondsSinceEpochForDateTime(ms))
527 return false;
528 m_type = DateTimeLocal;
529 return true;
532 bool DateComponents::setMillisecondsSinceEpochForMonth(double ms)
534 m_type = Invalid;
535 if (!std::isfinite(ms))
536 return false;
537 if (!setMillisecondsSinceEpochForDateInternal(round(ms)))
538 return false;
539 if (!withinHTMLDateLimits(m_year, m_month))
540 return false;
541 m_type = Month;
542 return true;
545 bool DateComponents::setMillisecondsSinceMidnight(double ms)
547 m_type = Invalid;
548 if (!std::isfinite(ms))
549 return false;
550 setMillisecondsSinceMidnightInternal(positiveFmod(round(ms), msPerDay));
551 m_type = Time;
552 return true;
555 bool DateComponents::setMonthsSinceEpoch(double months)
557 if (!std::isfinite(months))
558 return false;
559 months = round(months);
560 double doubleMonth = positiveFmod(months, 12);
561 double doubleYear = 1970 + (months - doubleMonth) / 12;
562 if (doubleYear < minimumYear() || maximumYear() < doubleYear)
563 return false;
564 int year = static_cast<int>(doubleYear);
565 int month = static_cast<int>(doubleMonth);
566 if (!withinHTMLDateLimits(year, month))
567 return false;
568 m_year = year;
569 m_month = month;
570 m_type = Month;
571 return true;
574 // Offset from January 1st to Monday of the ISO 8601's first week.
575 // ex. If January 1st is Friday, such Monday is 3 days later. Returns 3.
576 static int offsetTo1stWeekStart(int year)
578 int offsetTo1stWeekStart = 1 - dayOfWeek(year, 0, 1);
579 if (offsetTo1stWeekStart <= -4)
580 offsetTo1stWeekStart += 7;
581 return offsetTo1stWeekStart;
584 bool DateComponents::setMillisecondsSinceEpochForWeek(double ms)
586 m_type = Invalid;
587 if (!std::isfinite(ms))
588 return false;
589 ms = round(ms);
591 m_year = msToYear(ms);
592 if (m_year < minimumYear() || m_year > maximumYear())
593 return false;
595 int yearDay = dayInYear(ms, m_year);
596 int offset = offsetTo1stWeekStart(m_year);
597 if (yearDay < offset) {
598 // The day belongs to the last week of the previous year.
599 m_year--;
600 if (m_year <= minimumYear())
601 return false;
602 m_week = maxWeekNumberInYear();
603 } else {
604 m_week = ((yearDay - offset) / 7) + 1;
605 if (m_week > maxWeekNumberInYear()) {
606 m_year++;
607 m_week = 1;
609 if (m_year > maximumYear() || (m_year == maximumYear() && m_week > maximumWeekInMaximumYear))
610 return false;
612 m_type = Week;
613 return true;
616 bool DateComponents::setWeek(int year, int weekNumber)
618 m_type = Invalid;
619 if (year < minimumYear() || year > maximumYear())
620 return false;
621 m_year = year;
622 if (weekNumber < 1 || weekNumber > maxWeekNumberInYear())
623 return false;
624 m_week = weekNumber;
625 m_type = Week;
626 return true;
629 double DateComponents::millisecondsSinceEpochForTime() const
631 ASSERT(m_type == Time || m_type == DateTime || m_type == DateTimeLocal);
632 return ((m_hour * minutesPerHour + m_minute) * secondsPerMinute + m_second) * msPerSecond + m_millisecond;
635 double DateComponents::millisecondsSinceEpoch() const
637 switch (m_type) {
638 case Date:
639 return dateToDaysFrom1970(m_year, m_month, m_monthDay) * msPerDay;
640 case DateTime:
641 case DateTimeLocal:
642 return dateToDaysFrom1970(m_year, m_month, m_monthDay) * msPerDay + millisecondsSinceEpochForTime();
643 case Month:
644 return dateToDaysFrom1970(m_year, m_month, 1) * msPerDay;
645 case Time:
646 return millisecondsSinceEpochForTime();
647 case Week:
648 return (dateToDaysFrom1970(m_year, 0, 1) + offsetTo1stWeekStart(m_year) + (m_week - 1) * 7) * msPerDay;
649 case Invalid:
650 break;
652 ASSERT_NOT_REACHED();
653 return invalidMilliseconds();
656 double DateComponents::monthsSinceEpoch() const
658 ASSERT(m_type == Month);
659 return (m_year - 1970) * 12 + m_month;
662 String DateComponents::toStringForTime(SecondFormat format) const
664 ASSERT(m_type == DateTime || m_type == DateTimeLocal || m_type == Time);
665 SecondFormat effectiveFormat = format;
666 if (m_millisecond)
667 effectiveFormat = Millisecond;
668 else if (format == None && m_second)
669 effectiveFormat = Second;
671 switch (effectiveFormat) {
672 default:
673 ASSERT_NOT_REACHED();
674 // Fallback to None.
675 case None:
676 return String::format("%02d:%02d", m_hour, m_minute);
677 case Second:
678 return String::format("%02d:%02d:%02d", m_hour, m_minute, m_second);
679 case Millisecond:
680 return String::format("%02d:%02d:%02d.%03d", m_hour, m_minute, m_second, m_millisecond);
684 String DateComponents::toString(SecondFormat format) const
686 switch (m_type) {
687 case Date:
688 return String::format("%04d-%02d-%02d", m_year, m_month + 1, m_monthDay);
689 case DateTime:
690 return String::format("%04d-%02d-%02dT", m_year, m_month + 1, m_monthDay)
691 + toStringForTime(format) + String("Z");
692 case DateTimeLocal:
693 return String::format("%04d-%02d-%02dT", m_year, m_month + 1, m_monthDay)
694 + toStringForTime(format);
695 case Month:
696 return String::format("%04d-%02d", m_year, m_month + 1);
697 case Time:
698 return toStringForTime(format);
699 case Week:
700 return String::format("%04d-W%02d", m_year, m_week);
701 case Invalid:
702 break;
704 ASSERT_NOT_REACHED();
705 return String("(Invalid DateComponents)");
708 } // namespace blink