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/DateTimeLocalInputType.h"
34 #include "bindings/core/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"
46 using blink::WebLocalizedString;
47 using namespace HTMLNames;
49 static const int dateTimeLocalDefaultStep = 60;
50 static const int dateTimeLocalDefaultStepBase = 0;
51 static const int dateTimeLocalStepScaleFactor = 1000;
53 PassRefPtrWillBeRawPtr<InputType> DateTimeLocalInputType::create(HTMLInputElement& element)
55 return adoptRefWillBeNoop(new DateTimeLocalInputType(element));
58 void DateTimeLocalInputType::countUsage()
60 countUsageIfVisible(UseCounter::InputTypeDateTimeLocal);
63 const AtomicString& DateTimeLocalInputType::formControlType() const
65 return InputTypeNames::datetime_local;
68 double DateTimeLocalInputType::valueAsDate() const
70 // valueAsDate doesn't work for the datetime-local type according to the standard.
71 return DateComponents::invalidMilliseconds();
74 void DateTimeLocalInputType::setValueAsDate(double value, ExceptionState& exceptionState) const
76 // valueAsDate doesn't work for the datetime-local type according to the standard.
77 InputType::setValueAsDate(value, exceptionState);
80 StepRange DateTimeLocalInputType::createStepRange(AnyStepHandling anyStepHandling) const
82 DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (dateTimeLocalDefaultStep, dateTimeLocalDefaultStepBase, dateTimeLocalStepScaleFactor, StepRange::ScaledStepValueShouldBeInteger));
84 return InputType::createStepRange(anyStepHandling, dateTimeLocalDefaultStepBase, Decimal::fromDouble(DateComponents::minimumDateTime()), Decimal::fromDouble(DateComponents::maximumDateTime()), stepDescription);
87 bool DateTimeLocalInputType::parseToDateComponentsInternal(const String& string, DateComponents* out) const
91 return out->parseDateTimeLocal(string, 0, end) && end == string.length();
94 bool DateTimeLocalInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
97 return date->setMillisecondsSinceEpochForDateTimeLocal(value);
100 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
101 // FIXME: It is better to share code for DateTimeInputType::formatDateTimeFieldsState()
102 // and DateTimeInputLocalType::formatDateTimeFieldsState().
103 String DateTimeLocalInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
105 if (!dateTimeFieldsState.hasDayOfMonth() || !dateTimeFieldsState.hasMonth() || !dateTimeFieldsState.hasYear()
106 || !dateTimeFieldsState.hasHour() || !dateTimeFieldsState.hasMinute() || !dateTimeFieldsState.hasAMPM())
107 return emptyString();
109 if (dateTimeFieldsState.hasMillisecond() && dateTimeFieldsState.millisecond()) {
110 return String::format("%04u-%02u-%02uT%02u:%02u:%02u.%03u",
111 dateTimeFieldsState.year(),
112 dateTimeFieldsState.month(),
113 dateTimeFieldsState.dayOfMonth(),
114 dateTimeFieldsState.hour23(),
115 dateTimeFieldsState.minute(),
116 dateTimeFieldsState.hasSecond() ? dateTimeFieldsState.second() : 0,
117 dateTimeFieldsState.millisecond());
120 if (dateTimeFieldsState.hasSecond() && dateTimeFieldsState.second()) {
121 return String::format("%04u-%02u-%02uT%02u:%02u:%02u",
122 dateTimeFieldsState.year(),
123 dateTimeFieldsState.month(),
124 dateTimeFieldsState.dayOfMonth(),
125 dateTimeFieldsState.hour23(),
126 dateTimeFieldsState.minute(),
127 dateTimeFieldsState.second());
130 return String::format("%04u-%02u-%02uT%02u:%02u",
131 dateTimeFieldsState.year(),
132 dateTimeFieldsState.month(),
133 dateTimeFieldsState.dayOfMonth(),
134 dateTimeFieldsState.hour23(),
135 dateTimeFieldsState.minute());
138 void DateTimeLocalInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
140 if (shouldHaveSecondField(date)) {
141 layoutParameters.dateTimeFormat = layoutParameters.locale.dateTimeFormatWithSeconds();
142 layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";
144 layoutParameters.dateTimeFormat = layoutParameters.locale.dateTimeFormatWithoutSeconds();
145 layoutParameters.fallbackDateTimeFormat = "yyyy-MM-dd'T'HH:mm";
147 if (!parseToDateComponents(element().fastGetAttribute(minAttr), &layoutParameters.minimum))
148 layoutParameters.minimum = DateComponents();
149 if (!parseToDateComponents(element().fastGetAttribute(maxAttr), &layoutParameters.maximum))
150 layoutParameters.maximum = DateComponents();
151 layoutParameters.placeholderForDay = locale().queryString(WebLocalizedString::PlaceholderForDayOfMonthField);
152 layoutParameters.placeholderForMonth = locale().queryString(WebLocalizedString::PlaceholderForMonthField);
153 layoutParameters.placeholderForYear = locale().queryString(WebLocalizedString::PlaceholderForYearField);
156 bool DateTimeLocalInputType::isValidFormat(bool hasYear, bool hasMonth, bool hasWeek, bool hasDay, bool hasAMPM, bool hasHour, bool hasMinute, bool hasSecond) const
158 return hasYear && hasMonth && hasDay && hasAMPM && hasHour && hasMinute;