String Locale::formatDateTime(const DateComponents& date, FormatType formatType) { if (date.type() == DateComponents::Invalid) return String(); DateTimeStringBuilder builder(*this, date); switch (date.type()) { case DateComponents::Time: builder.build(formatType == FormatTypeShort ? shortTimeFormat() : timeFormat()); break; case DateComponents::Date: builder.build(dateFormat()); break; case DateComponents::Month: builder.build(formatType == FormatTypeShort ? shortMonthFormat() : monthFormat()); break; case DateComponents::Week: builder.build(weekFormatInLDML()); break; case DateComponents::DateTime: case DateComponents::DateTimeLocal: builder.build(formatType == FormatTypeShort ? dateTimeFormatWithoutSeconds() : dateTimeFormatWithSeconds()); break; case DateComponents::Invalid: ASSERT_NOT_REACHED(); break; } return builder.toString(); }
String formatLocalizedDate(const DateComponents& dateComponents) { switch (dateComponents.type()) { case DateComponents::Date: { UDateFormat* dateFormat = createShortDateFormatter(); if (!dateFormat) break; double input = dateComponents.millisecondsSinceEpoch(); UErrorCode status = U_ZERO_ERROR; int32_t length = udat_format(dateFormat, input, 0, 0, 0, &status); if (status != U_BUFFER_OVERFLOW_ERROR) { udat_close(dateFormat); break; } Vector<UChar> buffer(length); status = U_ZERO_ERROR; udat_format(dateFormat, input, buffer.data(), length, 0, &status); udat_close(dateFormat); if (U_FAILURE(status)) break; return String::adopt(buffer); } case DateComponents::DateTime: case DateComponents::DateTimeLocal: case DateComponents::Month: case DateComponents::Time: case DateComponents::Week: case DateComponents::Invalid: break; } return String(); }
bool DateTimeEditBuilder::shouldHourFieldDisabled() const { if (m_hour23Range.isSingleton() && m_hour23Range.minimum == m_dateValue.hour() && !(shouldMinuteFieldDisabled() && shouldSecondFieldDisabled() && shouldMillisecondFieldDisabled())) return true; if (m_dateValue.type() == DateComponents::Time) return false; ASSERT(m_dateValue.type() == DateComponents::DateTimeLocal); if (shouldDayOfMonthFieldDisabled()) { ASSERT(m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear()); ASSERT(m_parameters.minimum.month() == m_parameters.maximum.month()); return false; } const Decimal decimalMsPerDay(static_cast<int>(msPerDay)); Decimal hourPartOfMinimum = (stepRange().stepBase().abs().remainder(decimalMsPerDay) / static_cast<int>(msPerHour)).floor(); return hourPartOfMinimum == m_dateValue.hour() && stepRange().step().remainder(decimalMsPerDay).isZero(); }
void DateTimeEditElement::setOnlyYearMonthDay(const DateComponents& date) { ASSERT(date.type() == DateComponents::Date); if (!m_editControlOwner) return; DateTimeFieldsState dateTimeFieldsState = valueAsDateTimeFieldsState(); dateTimeFieldsState.setYear(date.fullYear()); dateTimeFieldsState.setMonth(date.month() + 1); dateTimeFieldsState.setDayOfMonth(date.monthDay()); setValueAsDateTimeFieldsState(dateTimeFieldsState); m_editControlOwner->editControlValueChanged(); }
String formatLocalizedDate(const DateComponents& dateComponents) { switch (dateComponents.type()) { case DateComponents::Date: return LocaleWin::currentLocale()->formatDate(dateComponents); case DateComponents::DateTime: case DateComponents::DateTimeLocal: case DateComponents::Month: case DateComponents::Time: case DateComponents::Week: case DateComponents::Invalid: break; } return String(); }
static String valueToDateTimeString(double value, AtomicString type) { DateComponents components; if (type == InputTypeNames::date) components.setMillisecondsSinceEpochForDate(value); else if (type == InputTypeNames::datetime_local) components.setMillisecondsSinceEpochForDateTimeLocal(value); else if (type == InputTypeNames::month) components.setMonthsSinceEpoch(value); else if (type == InputTypeNames::time) components.setMillisecondsSinceMidnight(value); else if (type == InputTypeNames::week) components.setMillisecondsSinceEpochForWeek(value); else ASSERT_NOT_REACHED(); return components.type() == DateComponents::Invalid ? String() : components.toString(); }
bool DateTimeEditBuilder::shouldDayOfMonthFieldDisabled() const { return m_dayRange.isSingleton() && m_dayRange.minimum == m_dateValue.monthDay() && m_dateValue.type() != DateComponents::Date; }
void DateTimeEditBuilder::visitField(DateTimeFormat::FieldType fieldType, int count) { const int countForAbbreviatedMonth = 3; const int countForFullMonth = 4; const int countForNarrowMonth = 5; Document& document = m_editElement.document(); switch (fieldType) { case DateTimeFormat::FieldTypeDayOfMonth: { RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeDayFieldElement::create(document, m_editElement, m_parameters.placeholderForDay, m_dayRange); m_editElement.addField(field); if (shouldDayOfMonthFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeHour11: { DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerHour * 12); RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeHour11FieldElement::create(document, m_editElement, m_hour23Range, step); m_editElement.addField(field); if (shouldHourFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeHour12: { DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerHour * 12); RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeHour12FieldElement::create(document, m_editElement, m_hour23Range, step); m_editElement.addField(field); if (shouldHourFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeHour23: { DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerDay); RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeHour23FieldElement::create(document, m_editElement, m_hour23Range, step); m_editElement.addField(field); if (shouldHourFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeHour24: { DateTimeNumericFieldElement::Step step = createStep(msPerHour, msPerDay); RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeHour24FieldElement::create(document, m_editElement, m_hour23Range, step); m_editElement.addField(field); if (shouldHourFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeMinute: { DateTimeNumericFieldElement::Step step = createStep(msPerMinute, msPerHour); RefPtrWillBeRawPtr<DateTimeNumericFieldElement> field = DateTimeMinuteFieldElement::create(document, m_editElement, m_minuteRange, step); m_editElement.addField(field); if (shouldMinuteFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeMonth: // Fallthrough. case DateTimeFormat::FieldTypeMonthStandAlone: { int minMonth = 0, maxMonth = 11; if (m_parameters.minimum.type() != DateComponents::Invalid && m_parameters.maximum.type() != DateComponents::Invalid && m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear() && m_parameters.minimum.month() <= m_parameters.maximum.month()) { minMonth = m_parameters.minimum.month(); maxMonth = m_parameters.maximum.month(); } RefPtrWillBeRawPtr<DateTimeFieldElement> field; switch (count) { case countForNarrowMonth: // Fallthrough. case countForAbbreviatedMonth: field = DateTimeSymbolicMonthFieldElement::create(document, m_editElement, fieldType == DateTimeFormat::FieldTypeMonth ? m_parameters.locale.shortMonthLabels() : m_parameters.locale.shortStandAloneMonthLabels(), minMonth, maxMonth); break; case countForFullMonth: field = DateTimeSymbolicMonthFieldElement::create(document, m_editElement, fieldType == DateTimeFormat::FieldTypeMonth ? m_parameters.locale.monthLabels() : m_parameters.locale.standAloneMonthLabels(), minMonth, maxMonth); break; default: field = DateTimeMonthFieldElement::create(document, m_editElement, m_parameters.placeholderForMonth, DateTimeNumericFieldElement::Range(minMonth + 1, maxMonth + 1)); break; } m_editElement.addField(field); if (minMonth == maxMonth && minMonth == m_dateValue.month() && m_dateValue.type() != DateComponents::Month) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypePeriod: { RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeAMPMFieldElement::create(document, m_editElement, m_parameters.locale.timeAMPMLabels()); m_editElement.addField(field); if (shouldAMPMFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeSecond: { DateTimeNumericFieldElement::Step step = createStep(msPerSecond, msPerMinute); RefPtrWillBeRawPtr<DateTimeNumericFieldElement> field = DateTimeSecondFieldElement::create(document, m_editElement, m_secondRange, step); m_editElement.addField(field); if (shouldSecondFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } if (needMillisecondField()) { visitLiteral(m_parameters.locale.localizedDecimalSeparator()); visitField(DateTimeFormat::FieldTypeFractionalSecond, 3); } return; } case DateTimeFormat::FieldTypeFractionalSecond: { DateTimeNumericFieldElement::Step step = createStep(1, msPerSecond); RefPtrWillBeRawPtr<DateTimeNumericFieldElement> field = DateTimeMillisecondFieldElement::create(document, m_editElement, m_millisecondRange, step); m_editElement.addField(field); if (shouldMillisecondFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } case DateTimeFormat::FieldTypeWeekOfYear: { DateTimeNumericFieldElement::Range range(DateComponents::minimumWeekNumber, DateComponents::maximumWeekNumber); if (m_parameters.minimum.type() != DateComponents::Invalid && m_parameters.maximum.type() != DateComponents::Invalid && m_parameters.minimum.fullYear() == m_parameters.maximum.fullYear() && m_parameters.minimum.week() <= m_parameters.maximum.week()) { range.minimum = m_parameters.minimum.week(); range.maximum = m_parameters.maximum.week(); } m_editElement.addField(DateTimeWeekFieldElement::create(document, m_editElement, range)); return; } case DateTimeFormat::FieldTypeYear: { DateTimeYearFieldElement::Parameters yearParams; if (m_parameters.minimum.type() == DateComponents::Invalid) { yearParams.minimumYear = DateComponents::minimumYear(); yearParams.minIsSpecified = false; } else { yearParams.minimumYear = m_parameters.minimum.fullYear(); yearParams.minIsSpecified = true; } if (m_parameters.maximum.type() == DateComponents::Invalid) { yearParams.maximumYear = DateComponents::maximumYear(); yearParams.maxIsSpecified = false; } else { yearParams.maximumYear = m_parameters.maximum.fullYear(); yearParams.maxIsSpecified = true; } if (yearParams.minimumYear > yearParams.maximumYear) { std::swap(yearParams.minimumYear, yearParams.maximumYear); std::swap(yearParams.minIsSpecified, yearParams.maxIsSpecified); } yearParams.placeholder = m_parameters.placeholderForYear; RefPtrWillBeRawPtr<DateTimeFieldElement> field = DateTimeYearFieldElement::create(document, m_editElement, yearParams); m_editElement.addField(field); if (shouldYearFieldDisabled()) { field->setValueAsDate(m_dateValue); field->setDisabled(); } return; } default: return; } }