2 * Copyright (C) 2010 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.
32 #include "core/html/forms/BaseDateAndTimeInputType.h"
35 #include "core/html/HTMLInputElement.h"
36 #include "platform/text/PlatformLocale.h"
37 #include "wtf/CurrentTime.h"
38 #include "wtf/DateMath.h"
39 #include "wtf/MathExtras.h"
40 #include "wtf/text/WTFString.h"
44 using blink::WebLocalizedString;
45 using namespace HTMLNames;
47 static const int msecPerMinute = 60 * 1000;
48 static const int msecPerSecond = 1000;
50 double BaseDateAndTimeInputType::valueAsDate() const
52 return valueAsDouble();
55 void BaseDateAndTimeInputType::setValueAsDate(double value, ExceptionState&) const
57 element().setValue(serializeWithMilliseconds(value));
60 double BaseDateAndTimeInputType::valueAsDouble() const
62 const Decimal value = parseToNumber(element().value(), Decimal::nan());
63 return value.isFinite() ? value.toDouble() : DateComponents::invalidMilliseconds();
66 void BaseDateAndTimeInputType::setValueAsDouble(double newValue, TextFieldEventBehavior eventBehavior, ExceptionState& exceptionState) const
68 setValueAsDecimal(Decimal::fromDouble(newValue), eventBehavior, exceptionState);
71 bool BaseDateAndTimeInputType::typeMismatchFor(const String& value) const
73 return !value.isEmpty() && !parseToDateComponents(value, 0);
76 bool BaseDateAndTimeInputType::typeMismatch() const
78 return typeMismatchFor(element().value());
81 String BaseDateAndTimeInputType::rangeOverflowText(const Decimal& maximum) const
83 return locale().queryString(WebLocalizedString::ValidationRangeOverflowDateTime, localizeValue(serialize(maximum)));
86 String BaseDateAndTimeInputType::rangeUnderflowText(const Decimal& minimum) const
88 return locale().queryString(WebLocalizedString::ValidationRangeUnderflowDateTime, localizeValue(serialize(minimum)));
91 Decimal BaseDateAndTimeInputType::defaultValueForStepUp() const
93 return Decimal::fromDouble(convertToLocalTime(currentTimeMS()));
96 bool BaseDateAndTimeInputType::isSteppable() const
101 Decimal BaseDateAndTimeInputType::parseToNumber(const String& source, const Decimal& defaultValue) const
104 if (!parseToDateComponents(source, &date))
106 double msec = date.millisecondsSinceEpoch();
107 ASSERT(std::isfinite(msec));
108 return Decimal::fromDouble(msec);
111 bool BaseDateAndTimeInputType::parseToDateComponents(const String& source, DateComponents* out) const
113 if (source.isEmpty())
115 DateComponents ignoredResult;
117 out = &ignoredResult;
118 return parseToDateComponentsInternal(source, out);
121 String BaseDateAndTimeInputType::serialize(const Decimal& value) const
123 if (!value.isFinite())
126 if (!setMillisecondToDateComponents(value.toDouble(), &date))
128 return serializeWithComponents(date);
131 String BaseDateAndTimeInputType::serializeWithComponents(const DateComponents& date) const
134 if (!element().getAllowedValueStep(&step))
135 return date.toString();
136 if (step.remainder(msecPerMinute).isZero())
137 return date.toString(DateComponents::None);
138 if (step.remainder(msecPerSecond).isZero())
139 return date.toString(DateComponents::Second);
140 return date.toString(DateComponents::Millisecond);
143 String BaseDateAndTimeInputType::serializeWithMilliseconds(double value) const
145 return serialize(Decimal::fromDouble(value));
148 String BaseDateAndTimeInputType::localizeValue(const String& proposedValue) const
151 if (!parseToDateComponents(proposedValue, &date))
152 return proposedValue;
154 String localized = element().locale().formatDateTime(date);
155 return localized.isEmpty() ? proposedValue : localized;
158 String BaseDateAndTimeInputType::visibleValue() const
160 return localizeValue(element().value());
163 String BaseDateAndTimeInputType::sanitizeValue(const String& proposedValue) const
165 return typeMismatchFor(proposedValue) ? emptyString() : proposedValue;
168 bool BaseDateAndTimeInputType::supportsReadOnly() const
173 bool BaseDateAndTimeInputType::shouldRespectListAttribute()
178 bool BaseDateAndTimeInputType::valueMissing(const String& value) const
180 return element().isRequired() && value.isEmpty();
183 bool BaseDateAndTimeInputType::shouldShowFocusRingOnMouseFocus() const