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#if ENABLE(DATE_AND_TIME_INPUT_TYPES)
33#include "BaseDateAndTimeInputType.h"
34
35#include "HTMLInputElement.h"
36#include "HTMLNames.h"
37#include "KeyboardEvent.h"
38#include "PlatformLocale.h"
39#include <limits>
40#include <wtf/CurrentTime.h>
41#include <wtf/DateMath.h>
42#include <wtf/MathExtras.h>
43#include <wtf/text/StringView.h>
44
45namespace WebCore {
46
47using namespace HTMLNames;
48
49static const int msecPerMinute = 60 * 1000;
50static const int msecPerSecond = 1000;
51
52double BaseDateAndTimeInputType::valueAsDate() const
53{
54    return valueAsDouble();
55}
56
57void BaseDateAndTimeInputType::setValueAsDate(double value, ExceptionCode&) const
58{
59    element().setValue(serializeWithMilliseconds(value));
60}
61
62double BaseDateAndTimeInputType::valueAsDouble() const
63{
64    const Decimal value = parseToNumber(element().value(), Decimal::nan());
65    return value.isFinite() ? value.toDouble() : DateComponents::invalidMilliseconds();
66}
67
68void BaseDateAndTimeInputType::setValueAsDecimal(const Decimal& newValue, TextFieldEventBehavior eventBehavior, ExceptionCode&) const
69{
70    element().setValue(serialize(newValue), eventBehavior);
71}
72
73bool BaseDateAndTimeInputType::typeMismatchFor(const String& value) const
74{
75    return !value.isEmpty() && !parseToDateComponents(value, 0);
76}
77
78bool BaseDateAndTimeInputType::typeMismatch() const
79{
80    return typeMismatchFor(element().value());
81}
82
83Decimal BaseDateAndTimeInputType::defaultValueForStepUp() const
84{
85    double ms = currentTimeMS();
86    int offset = calculateLocalTimeOffset(ms).offset / msPerMinute;
87    return Decimal::fromDouble(ms + (offset * msPerMinute));
88}
89
90bool BaseDateAndTimeInputType::isSteppable() const
91{
92    return true;
93}
94
95Decimal BaseDateAndTimeInputType::parseToNumber(const String& source, const Decimal& defaultValue) const
96{
97    DateComponents date;
98    if (!parseToDateComponents(source, &date))
99        return defaultValue;
100    double msec = date.millisecondsSinceEpoch();
101    ASSERT(std::isfinite(msec));
102    return Decimal::fromDouble(msec);
103}
104
105bool BaseDateAndTimeInputType::parseToDateComponents(const String& source, DateComponents* out) const
106{
107    if (source.isEmpty())
108        return false;
109    DateComponents ignoredResult;
110    if (!out)
111        out = &ignoredResult;
112    return parseToDateComponentsInternal(StringView(source).upconvertedCharacters(), source.length(), out);
113}
114
115String BaseDateAndTimeInputType::serialize(const Decimal& value) const
116{
117    if (!value.isFinite())
118        return String();
119    DateComponents date;
120    if (!setMillisecondToDateComponents(value.toDouble(), &date))
121        return String();
122    return serializeWithComponents(date);
123}
124
125String BaseDateAndTimeInputType::serializeWithComponents(const DateComponents& date) const
126{
127    Decimal step;
128    if (!element().getAllowedValueStep(&step))
129        return date.toString();
130    if (step.remainder(msecPerMinute).isZero())
131        return date.toString(DateComponents::None);
132    if (step.remainder(msecPerSecond).isZero())
133        return date.toString(DateComponents::Second);
134    return date.toString(DateComponents::Millisecond);
135}
136
137String BaseDateAndTimeInputType::serializeWithMilliseconds(double value) const
138{
139    return serialize(Decimal::fromDouble(value));
140}
141
142String BaseDateAndTimeInputType::localizeValue(const String& proposedValue) const
143{
144    DateComponents date;
145    if (!parseToDateComponents(proposedValue, &date))
146        return proposedValue;
147
148    String localized = element().locale().formatDateTime(date);
149    return localized.isEmpty() ? proposedValue : localized;
150}
151
152String BaseDateAndTimeInputType::visibleValue() const
153{
154    return localizeValue(element().value());
155}
156
157String BaseDateAndTimeInputType::sanitizeValue(const String& proposedValue) const
158{
159    return typeMismatchFor(proposedValue) ? String() : proposedValue;
160}
161
162bool BaseDateAndTimeInputType::supportsReadOnly() const
163{
164    return true;
165}
166
167bool BaseDateAndTimeInputType::shouldRespectListAttribute()
168{
169    return InputType::themeSupportsDataListUI(this);
170}
171
172bool BaseDateAndTimeInputType::valueMissing(const String& value) const
173{
174    return element().isRequired() && value.isEmpty();
175}
176
177#if PLATFORM(IOS)
178bool BaseDateAndTimeInputType::isKeyboardFocusable(KeyboardEvent*) const
179{
180    return !element().isReadOnly() && element().isTextFormControlFocusable();
181}
182#endif
183
184} // namespace WebCore
185#endif
186