~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/html/TimeInputType.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "TimeInputType.h"
 
33
 
 
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>
 
42
 
 
43
#if ENABLE(INPUT_TYPE_TIME)
 
44
 
 
45
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
 
46
#include "DateTimeFieldsState.h"
 
47
#include "PlatformLocale.h"
 
48
#include <wtf/text/WTFString.h>
 
49
#endif
 
50
 
 
51
namespace WebCore {
 
52
 
 
53
using namespace HTMLNames;
 
54
 
 
55
static const int timeDefaultStep = 60;
 
56
static const int timeDefaultStepBase = 0;
 
57
static const int timeStepScaleFactor = 1000;
 
58
 
 
59
TimeInputType::TimeInputType(HTMLInputElement*  element)
 
60
    : BaseTimeInputType(element)
 
61
{
 
62
}
 
63
 
 
64
PassOwnPtr<InputType> TimeInputType::create(HTMLInputElement* element)
 
65
{
 
66
    return adoptPtr(new TimeInputType(element));
 
67
}
 
68
 
 
69
const AtomicString& TimeInputType::formControlType() const
 
70
{
 
71
    return InputTypeNames::time();
 
72
}
 
73
 
 
74
DateComponents::Type TimeInputType::dateType() const
 
75
{
 
76
    return DateComponents::Time;
 
77
}
 
78
 
 
79
Decimal TimeInputType::defaultValueForStepUp() const
 
80
{
 
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;
 
86
 
 
87
    DateComponents date;
 
88
    date.setMillisecondsSinceMidnight(current);
 
89
    double milliseconds = date.millisecondsSinceEpoch();
 
90
    ASSERT(isfinite(milliseconds));
 
91
    return Decimal::fromDouble(milliseconds);
 
92
}
 
93
 
 
94
StepRange TimeInputType::createStepRange(AnyStepHandling anyStepHandling) const
 
95
{
 
96
    DEFINE_STATIC_LOCAL(const StepRange::StepDescription, stepDescription, (timeDefaultStep, timeDefaultStepBase, timeStepScaleFactor, StepRange::ScaledStepValueShouldBeInteger));
 
97
 
 
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);
 
103
}
 
104
 
 
105
bool TimeInputType::parseToDateComponentsInternal(const UChar* characters, unsigned length, DateComponents* out) const
 
106
{
 
107
    ASSERT(out);
 
108
    unsigned end;
 
109
    return out->parseTime(characters, length, 0, end) && end == length;
 
110
}
 
111
 
 
112
bool TimeInputType::setMillisecondToDateComponents(double value, DateComponents* date) const
 
113
{
 
114
    ASSERT(date);
 
115
    return date->setMillisecondsSinceMidnight(value);
 
116
}
 
117
 
 
118
bool TimeInputType::isTimeField() const
 
119
{
 
120
    return true;
 
121
}
 
122
 
 
123
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
 
124
 
 
125
String TimeInputType::localizeValue(const String& proposedValue) const
 
126
{
 
127
    DateComponents date;
 
128
    if (!parseToDateComponents(proposedValue, &date))
 
129
        return proposedValue;
 
130
 
 
131
    Locale::FormatType formatType = shouldHaveSecondField(date) ? Locale::FormatTypeMedium : Locale::FormatTypeShort;
 
132
 
 
133
    String localized = element()->locale().formatDateTime(date, formatType);
 
134
    return localized.isEmpty() ? proposedValue : localized;
 
135
}
 
136
 
 
137
String TimeInputType::formatDateTimeFieldsState(const DateTimeFieldsState& dateTimeFieldsState) const
 
138
{
 
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());
 
153
}
 
154
 
 
155
void TimeInputType::setupLayoutParameters(DateTimeEditElement::LayoutParameters& layoutParameters, const DateComponents& date) const
 
156
{
 
157
    if (shouldHaveSecondField(date)) {
 
158
        layoutParameters.dateTimeFormat = layoutParameters.locale.timeFormat();
 
159
        layoutParameters.fallbackDateTimeFormat = "HH:mm:ss";
 
160
    } else {
 
161
        layoutParameters.dateTimeFormat = layoutParameters.locale.shortTimeFormat();
 
162
        layoutParameters.fallbackDateTimeFormat = "HH:mm";
 
163
    }
 
164
}
 
165
#endif
 
166
 
 
167
} // namespace WebCore
 
168
 
 
169
#endif