1/*
2 * Copyright (C) 2010 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#include "config.h"
32#include "core/html/forms/BaseDateAndTimeInputType.h"
33
34#include <limits>
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"
41
42namespace WebCore {
43
44using blink::WebLocalizedString;
45using namespace HTMLNames;
46
47static const int msecPerMinute = 60 * 1000;
48static const int msecPerSecond = 1000;
49
50double BaseDateAndTimeInputType::valueAsDate() const
51{
52    return valueAsDouble();
53}
54
55void BaseDateAndTimeInputType::setValueAsDate(double value, ExceptionState&) const
56{
57    element().setValue(serializeWithMilliseconds(value));
58}
59
60double BaseDateAndTimeInputType::valueAsDouble() const
61{
62    const Decimal value = parseToNumber(element().value(), Decimal::nan());
63    return value.isFinite() ? value.toDouble() : DateComponents::invalidMilliseconds();
64}
65
66void BaseDateAndTimeInputType::setValueAsDouble(double newValue, TextFieldEventBehavior eventBehavior, ExceptionState& exceptionState) const
67{
68    setValueAsDecimal(Decimal::fromDouble(newValue), eventBehavior, exceptionState);
69}
70
71bool BaseDateAndTimeInputType::typeMismatchFor(const String& value) const
72{
73    return !value.isEmpty() && !parseToDateComponents(value, 0);
74}
75
76bool BaseDateAndTimeInputType::typeMismatch() const
77{
78    return typeMismatchFor(element().value());
79}
80
81String BaseDateAndTimeInputType::rangeOverflowText(const Decimal& maximum) const
82{
83    return locale().queryString(WebLocalizedString::ValidationRangeOverflowDateTime, localizeValue(serialize(maximum)));
84}
85
86String BaseDateAndTimeInputType::rangeUnderflowText(const Decimal& minimum) const
87{
88    return locale().queryString(WebLocalizedString::ValidationRangeUnderflowDateTime, localizeValue(serialize(minimum)));
89}
90
91Decimal BaseDateAndTimeInputType::defaultValueForStepUp() const
92{
93    double ms = currentTimeMS();
94    double utcOffset = calculateUTCOffset();
95    double dstOffset = calculateDSTOffset(ms, utcOffset);
96    int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
97    return Decimal::fromDouble(ms + (offset * msPerMinute));
98}
99
100bool BaseDateAndTimeInputType::isSteppable() const
101{
102    return true;
103}
104
105Decimal BaseDateAndTimeInputType::parseToNumber(const String& source, const Decimal& defaultValue) const
106{
107    DateComponents date;
108    if (!parseToDateComponents(source, &date))
109        return defaultValue;
110    double msec = date.millisecondsSinceEpoch();
111    ASSERT(std::isfinite(msec));
112    return Decimal::fromDouble(msec);
113}
114
115bool BaseDateAndTimeInputType::parseToDateComponents(const String& source, DateComponents* out) const
116{
117    if (source.isEmpty())
118        return false;
119    DateComponents ignoredResult;
120    if (!out)
121        out = &ignoredResult;
122    return parseToDateComponentsInternal(source, out);
123}
124
125String BaseDateAndTimeInputType::serialize(const Decimal& value) const
126{
127    if (!value.isFinite())
128        return String();
129    DateComponents date;
130    if (!setMillisecondToDateComponents(value.toDouble(), &date))
131        return String();
132    return serializeWithComponents(date);
133}
134
135String BaseDateAndTimeInputType::serializeWithComponents(const DateComponents& date) const
136{
137    Decimal step;
138    if (!element().getAllowedValueStep(&step))
139        return date.toString();
140    if (step.remainder(msecPerMinute).isZero())
141        return date.toString(DateComponents::None);
142    if (step.remainder(msecPerSecond).isZero())
143        return date.toString(DateComponents::Second);
144    return date.toString(DateComponents::Millisecond);
145}
146
147String BaseDateAndTimeInputType::serializeWithMilliseconds(double value) const
148{
149    return serialize(Decimal::fromDouble(value));
150}
151
152String BaseDateAndTimeInputType::localizeValue(const String& proposedValue) const
153{
154    DateComponents date;
155    if (!parseToDateComponents(proposedValue, &date))
156        return proposedValue;
157
158    String localized = element().locale().formatDateTime(date);
159    return localized.isEmpty() ? proposedValue : localized;
160}
161
162String BaseDateAndTimeInputType::visibleValue() const
163{
164    return localizeValue(element().value());
165}
166
167String BaseDateAndTimeInputType::sanitizeValue(const String& proposedValue) const
168{
169    return typeMismatchFor(proposedValue) ? emptyString() : proposedValue;
170}
171
172bool BaseDateAndTimeInputType::supportsReadOnly() const
173{
174    return true;
175}
176
177bool BaseDateAndTimeInputType::shouldRespectListAttribute()
178{
179    return true;
180}
181
182bool BaseDateAndTimeInputType::valueMissing(const String& value) const
183{
184    return element().isRequired() && value.isEmpty();
185}
186
187bool BaseDateAndTimeInputType::shouldShowFocusRingOnMouseFocus() const
188{
189    return true;
190}
191
192} // namespace WebCore
193