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 "TimeInputType.h"
34
#include "DateComponents.h"
35
#include "HTMLInputElement.h"
36
#include "HTMLNames.h"
37
#include "InputTypeNames.h"
38
#include <wtf/CurrentTime.h>
39
#include <wtf/DateMath.h>
40
#include <wtf/MathExtras.h>
41
#include <wtf/PassOwnPtr.h>
43
#if ENABLE(INPUT_TYPE_TIME)
45
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
46
#include "DateTimeFieldsState.h"
47
#include "PlatformLocale.h"
48
#include <wtf/text/WTFString.h>
53
using namespace HTMLNames;
55
static const int timeDefaultStep = 60;
56
static const int timeDefaultStepBase = 0;
57
static const int timeStepScaleFactor = 1000;
59
TimeInputType::TimeInputType(HTMLInputElement* element)
60
: BaseTimeInputType(element)
64
PassOwnPtr<InputType> TimeInputType::create(HTMLInputElement* element)
66
return adoptPtr(new TimeInputType(element));
69
const AtomicString& TimeInputType::formControlType() const
71
return InputTypeNames::time();
74
DateComponents::Type TimeInputType::dateType() const
76
return DateComponents::Time;
79
Decimal TimeInputType::defaultValueForStepUp() const
81
double current = currentTimeMS();
82
double utcOffset = calculateUTCOffset();
83
double dstOffset = calculateDSTOffset(current, utcOffset);
84
int offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
85
current += offset * msPerMinute;
88
date.setMillisecondsSinceMidnight(current);
89
double milliseconds = date.millisecondsSinceEpoch();
90
ASSERT(isfinite(milliseconds));
91
return Decimal::fromDouble(milliseconds);
94
StepRange TimeInputType::createStepRange(AnyStepHandling anyStepHandling) const
96
DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (timeDefaultStep, timeDefaultStepBase, timeStepScaleFactor, StepRange::ScaledStepValueShouldBeInteger));
98
const Decimal stepBase = parseToNumber(element()->fastGetAttribute(minAttr), 0);
99
const Decimal minimum = parseToNumber(element()->fastGetAttribute(minAttr), Decimal::fromDouble(DateComponents::minimumTime()));
100
const Decimal maximum = parseToNumber(element()->fastGetAttribute(maxAttr), Decimal::fromDouble(DateComponents::maximumTime()));
101
const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription, element()->fastGetAttribute(stepAttr));
102
return StepRange(stepBase, minimum, maximum, step, stepDescription);
105
bool TimeInputType::parseToDateComponentsInternal(const UChar* characters, unsigned length, DateComponents* out) const
109
return out->parseTime(characters, length, 0, end) && end == length;
112
bool TimeInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
115
return date->setMillisecondsSinceMidnight(value);
118
bool TimeInputType::isTimeField() const
123
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
125
String TimeInputType::localizeValue(const String& proposedValue) const
128
if (!parseToDateComponents(proposedValue, &date))
129
return proposedValue;
131
Locale::FormatType formatType = shouldHaveSecondField(date) ? Locale::FormatTypeMedium : Locale::FormatTypeShort;
133
String localized = element()->locale().formatDateTime(date, formatType);
134
return localized.isEmpty() ? proposedValue : localized;
137
String TimeInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
139
if (!dateTimeFieldsState.hasHour() || !dateTimeFieldsState.hasMinute() || !dateTimeFieldsState.hasAMPM())
140
return emptyString();
141
if (dateTimeFieldsState.hasMillisecond() && dateTimeFieldsState.millisecond())
142
return String::format("%02u:%02u:%02u.%03u",
143
dateTimeFieldsState.hour23(),
144
dateTimeFieldsState.minute(),
145
dateTimeFieldsState.hasSecond() ? dateTimeFieldsState.second() : 0,
146
dateTimeFieldsState.millisecond());
147
if (dateTimeFieldsState.hasSecond() && dateTimeFieldsState.second())
148
return String::format("%02u:%02u:%02u",
149
dateTimeFieldsState.hour23(),
150
dateTimeFieldsState.minute(),
151
dateTimeFieldsState.second());
152
return String::format("%02u:%02u", dateTimeFieldsState.hour23(), dateTimeFieldsState.minute());
155
void TimeInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
157
if (shouldHaveSecondField(date)) {
158
layoutParameters.dateTimeFormat = layoutParameters.locale.timeFormat();
159
layoutParameters.fallbackDateTimeFormat = "HH:mm:ss";
161
layoutParameters.dateTimeFormat = layoutParameters.locale.shortTimeFormat();
162
layoutParameters.fallbackDateTimeFormat = "HH:mm";
167
} // namespace WebCore