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/DateTimeLocalInputType.h"
33
34#include "bindings/v8/ExceptionState.h"
35#include "core/HTMLNames.h"
36#include "core/InputTypeNames.h"
37#include "core/html/HTMLInputElement.h"
38#include "core/html/forms/DateTimeFieldsState.h"
39#include "platform/DateComponents.h"
40#include "platform/text/PlatformLocale.h"
41#include "wtf/PassOwnPtr.h"
42#include "wtf/text/WTFString.h"
43
44namespace WebCore {
45
46using blink::WebLocalizedString;
47using namespace HTMLNames;
48
49static const int dateTimeLocalDefaultStep = 60;
50static const int dateTimeLocalDefaultStepBase = 0;
51static const int dateTimeLocalStepScaleFactor = 1000;
52
53PassRefPtrWillBeRawPtr<InputType> DateTimeLocalInputType::create(HTMLInputElement& element)
54{
55    return adoptRefWillBeNoop(new DateTimeLocalInputType(element));
56}
57
58void DateTimeLocalInputType::countUsage()
59{
60    countUsageIfVisible(UseCounter::InputTypeDateTimeLocal);
61}
62
63const AtomicString& DateTimeLocalInputType::formControlType() const
64{
65    return InputTypeNames::datetime_local;
66}
67
68double DateTimeLocalInputType::valueAsDate() const
69{
70    // valueAsDate doesn't work for the datetime-local type according to the standard.
71    return DateComponents::invalidMilliseconds();
72}
73
74void DateTimeLocalInputType::setValueAsDate(double value, ExceptionState& exceptionState) const
75{
76    // valueAsDate doesn't work for the datetime-local type according to the standard.
77    InputType::setValueAsDate(value, exceptionState);
78}
79
80StepRange DateTimeLocalInputType::createStepRange(AnyStepHandling anyStepHandling) const
81{
82    DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateTimeLocalDefaultStep, dateTimeLocalDefaultStepBase, dateTimeLocalStepScaleFactor, StepRange::ScaledStepValueShouldBeInteger));
83
84    return InputType::createStepRange(anyStepHandling, dateTimeLocalDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDateTime()), Decimal::fromDouble(DateComponents::maximumDateTime()), stepDescription);
85}
86
87bool DateTimeLocalInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
88{
89    ASSERT(out);
90    unsigned end;
91    return out->parseDateTimeLocal(string, 0, end) && end == string.length();
92}
93
94bool DateTimeLocalInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
95{
96    ASSERT(date);
97    return date->setMillisecondsSinceEpochForDateTimeLocal(value);
98}
99
100bool DateTimeLocalInputType::isDateTimeLocalField() const
101{
102    return true;
103}
104
105#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
106// FIXME: It is better to share code for DateTimeInputType::formatDateTimeFieldsState()
107// and DateTimeInputLocalType::formatDateTimeFieldsState().
108String DateTimeLocalInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
109{
110    if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear()
111        || !dateTimeFieldsState.hasHour() || !dateTimeFieldsState.hasMinute() || !dateTimeFieldsState.hasAMPM())
112        return emptyString();
113
114    if (dateTimeFieldsState.hasMillisecond() && dateTimeFieldsState.millisecond()) {
115        return String::format("%04u-%02u-%02uT%02u:%02u:%02u.%03u",
116            dateTimeFieldsState.year(),
117            dateTimeFieldsState.month(),
118            dateTimeFieldsState.dayOfMonth(),
119            dateTimeFieldsState.hour23(),
120            dateTimeFieldsState.minute(),
121            dateTimeFieldsState.hasSecond() ? dateTimeFieldsState.second() : 0,
122            dateTimeFieldsState.millisecond());
123    }
124
125    if (dateTimeFieldsState.hasSecond() && dateTimeFieldsState.second()) {
126        return String::format("%04u-%02u-%02uT%02u:%02u:%02u",
127            dateTimeFieldsState.year(),
128            dateTimeFieldsState.month(),
129            dateTimeFieldsState.dayOfMonth(),
130            dateTimeFieldsState.hour23(),
131            dateTimeFieldsState.minute(),
132            dateTimeFieldsState.second());
133    }
134
135    return String::format("%04u-%02u-%02uT%02u:%02u",
136        dateTimeFieldsState.year(),
137        dateTimeFieldsState.month(),
138        dateTimeFieldsState.dayOfMonth(),
139        dateTimeFieldsState.hour23(),
140        dateTimeFieldsState.minute());
141}
142
143void DateTimeLocalInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
144{
145    if (shouldHaveSecondField(date)) {
146        layoutParameters.dateTimeFormat = layoutParameters.locale.dateTimeFormatWithSeconds();
147        layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";
148    } else {
149        layoutParameters.dateTimeFormat = layoutParameters.locale.dateTimeFormatWithoutSeconds();
150        layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd'T'HH:mm";
151    }
152    if (!parseToDateComponents(element().fastGetAttribute(minAttr), &layoutParameters.minimum))
153        layoutParameters.minimum = DateComponents();
154    if (!parseToDateComponents(element().fastGetAttribute(maxAttr), &layoutParameters.maximum))
155        layoutParameters.maximum = DateComponents();
156    layoutParameters.placeholderForDay = locale().queryString(WebLocalizedString::PlaceholderForDayOfMonthField);
157    layoutParameters.placeholderForMonth = locale().queryString(WebLocalizedString::PlaceholderForMonthField);
158    layoutParameters.placeholderForYear = locale().queryString(WebLocalizedString::PlaceholderForYearField);
159}
160
161bool DateTimeLocalInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
162{
163    return hasYear && hasMonth && hasDay && hasAMPM && hasHour && hasMinute;
164}
165#endif
166
167} // namespace WebCore
168