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 "BaseDateAndTimeInputType.h"
34 #include "HTMLInputElement.h"
35 #include "HTMLNames.h"
36 #include "KeyboardEvent.h"
37 #include "LocalizedDate.h"
39 #include <wtf/CurrentTime.h>
40 #include <wtf/DateMath.h>
41 #include <wtf/MathExtras.h>
42 #include <wtf/PassOwnPtr.h>
43 #include <wtf/text/WTFString.h>
47 using namespace HTMLNames;
50 static const double msecPerMinute = 60 * 1000;
51 static const double msecPerSecond = 1000;
53 double BaseDateAndTimeInputType::valueAsDate() const
55 return parseToDouble(element()->value(), DateComponents::invalidMilliseconds());
58 void BaseDateAndTimeInputType::setValueAsDate(double value, ExceptionCode&) const
60 element()->setValue(serializeWithMilliseconds(value));
63 double BaseDateAndTimeInputType::valueAsNumber() const
65 return parseToDouble(element()->value(), numeric_limits<double>::quiet_NaN());
68 void BaseDateAndTimeInputType::setValueAsNumber(double newValue, bool sendChangeEvent, ExceptionCode&) const
70 element()->setValue(serialize(newValue), sendChangeEvent);
73 bool BaseDateAndTimeInputType::typeMismatchFor(const String& value) const
75 return !value.isEmpty() && !parseToDateComponents(value, 0);
78 bool BaseDateAndTimeInputType::typeMismatch() const
80 return typeMismatchFor(element()->value());
83 bool BaseDateAndTimeInputType::rangeUnderflow(const String& value) const
85 const double nan = numeric_limits<double>::quiet_NaN();
86 double doubleValue = parseToDouble(value, nan);
87 return isfinite(doubleValue) && doubleValue < minimum();
90 bool BaseDateAndTimeInputType::rangeOverflow(const String& value) const
92 const double nan = numeric_limits<double>::quiet_NaN();
93 double doubleValue = parseToDouble(value, nan);
94 return isfinite(doubleValue) && doubleValue > maximum();
97 bool BaseDateAndTimeInputType::supportsRangeLimitation() const
102 double BaseDateAndTimeInputType::defaultValueForStepUp() const
104 double ms = currentTimeMS();
105 double utcOffset = calculateUTCOffset();
106 double dstOffset = calculateDSTOffset(ms, utcOffset);
107 int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
108 return ms + (offset * msPerMinute);
111 bool BaseDateAndTimeInputType::isSteppable() const
116 bool BaseDateAndTimeInputType::stepMismatch(const String& value, double step) const
118 const double nan = numeric_limits<double>::quiet_NaN();
119 double doubleValue = parseToDouble(value, nan);
120 doubleValue = fabs(doubleValue - stepBase());
121 if (!isfinite(doubleValue))
123 ASSERT(round(doubleValue) == doubleValue);
124 ASSERT(round(step) == step);
125 return fmod(doubleValue, step);
128 double BaseDateAndTimeInputType::stepBase() const
130 return parseToDouble(element()->fastGetAttribute(minAttr), defaultStepBase());
133 void BaseDateAndTimeInputType::handleKeydownEvent(KeyboardEvent* event)
135 handleKeydownEventForSpinButton(event);
136 if (!event->defaultHandled())
137 TextFieldInputType::handleKeydownEvent(event);
140 void BaseDateAndTimeInputType::handleWheelEvent(WheelEvent* event)
142 handleWheelEventForSpinButton(event);
145 double BaseDateAndTimeInputType::parseToDouble(const String& src, double defaultValue) const
148 if (!parseToDateComponents(src, &date))
150 double msec = date.millisecondsSinceEpoch();
151 ASSERT(isfinite(msec));
155 bool BaseDateAndTimeInputType::parseToDateComponents(const String& source, DateComponents* out) const
157 if (source.isEmpty())
159 DateComponents ignoredResult;
161 out = &ignoredResult;
162 return parseToDateComponentsInternal(source.characters(), source.length(), out);
165 String BaseDateAndTimeInputType::serialize(double value) const
167 if (!isfinite(value))
170 if (!setMillisecondToDateComponents(value, &date))
172 return serializeWithComponents(date);
175 String BaseDateAndTimeInputType::serializeWithComponents(const DateComponents& date) const
178 if (!element()->getAllowedValueStep(&step))
179 return date.toString();
180 if (!fmod(step, msecPerMinute))
181 return date.toString(DateComponents::None);
182 if (!fmod(step, msecPerSecond))
183 return date.toString(DateComponents::Second);
184 return date.toString(DateComponents::Millisecond);
187 String BaseDateAndTimeInputType::serializeWithMilliseconds(double value) const
189 return serialize(value);
192 String BaseDateAndTimeInputType::visibleValue() const
194 String currentValue = element()->value();
196 if (!parseToDateComponents(currentValue, &date))
199 String localized = formatLocalizedDate(date);
200 return localized.isEmpty() ? currentValue : localized;
203 String BaseDateAndTimeInputType::convertFromVisibleValue(const String& visibleValue) const
205 if (visibleValue.isEmpty())
208 double parsedValue = parseLocalizedDate(visibleValue, dateType());
209 if (!isfinite(parsedValue))
212 return serializeWithMilliseconds(parsedValue);
215 } // namespace WebCore