This source file includes following definitions.
- TimeFormat
- TimeFormatWithoutAmPm
- TimeFormatTimeOfDay
- TimeFormatTimeOfDayWithHourClockType
- TimeFormatShortDate
- TimeFormatShortDateNumeric
- TimeFormatShortDateAndTime
- TimeFormatFriendlyDateAndTime
- TimeFormatFriendlyDate
- GetHourClockType
#include "base/i18n/time_formatting.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "third_party/icu/source/i18n/unicode/datefmt.h"
#include "third_party/icu/source/i18n/unicode/dtptngen.h"
#include "third_party/icu/source/i18n/unicode/smpdtfmt.h"
namespace base {
namespace {
string16 TimeFormat(const icu::DateFormat* formatter,
const Time& time) {
DCHECK(formatter);
icu::UnicodeString date_string;
formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string);
return string16(date_string.getBuffer(),
static_cast<size_t>(date_string.length()));
}
string16 TimeFormatWithoutAmPm(const icu::DateFormat* formatter,
const Time& time) {
DCHECK(formatter);
icu::UnicodeString time_string;
icu::FieldPosition ampm_field(icu::DateFormat::kAmPmField);
formatter->format(
static_cast<UDate>(time.ToDoubleT() * 1000), time_string, ampm_field);
int ampm_length = ampm_field.getEndIndex() - ampm_field.getBeginIndex();
if (ampm_length) {
int begin = ampm_field.getBeginIndex();
if (begin)
begin--;
time_string.removeBetween(begin, ampm_field.getEndIndex());
}
return string16(time_string.getBuffer(),
static_cast<size_t>(time_string.length()));
}
}
string16 TimeFormatTimeOfDay(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(
icu::DateFormat::createTimeInstance(icu::DateFormat::kShort));
return TimeFormat(formatter.get(), time);
}
string16 TimeFormatTimeOfDayWithHourClockType(const Time& time,
HourClockType type,
AmPmClockType ampm) {
HourClockType default_type = GetHourClockType();
if (default_type == type && (type == k24HourClock || ampm == kKeepAmPm)) {
return TimeFormatTimeOfDay(time);
}
UErrorCode status = U_ZERO_ERROR;
scoped_ptr<icu::DateTimePatternGenerator> generator(
icu::DateTimePatternGenerator::createInstance(status));
DCHECK(U_SUCCESS(status));
const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm");
icu::UnicodeString generated_pattern =
generator->getBestPattern(icu::UnicodeString(base_pattern), status);
DCHECK(U_SUCCESS(status));
icu::SimpleDateFormat formatter(generated_pattern, status);
DCHECK(U_SUCCESS(status));
if (ampm == kKeepAmPm) {
return TimeFormat(&formatter, time);
} else {
return TimeFormatWithoutAmPm(&formatter, time);
}
}
string16 TimeFormatShortDate(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(
icu::DateFormat::createDateInstance(icu::DateFormat::kMedium));
return TimeFormat(formatter.get(), time);
}
string16 TimeFormatShortDateNumeric(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(
icu::DateFormat::createDateInstance(icu::DateFormat::kShort));
return TimeFormat(formatter.get(), time);
}
string16 TimeFormatShortDateAndTime(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(
icu::DateFormat::createDateTimeInstance(icu::DateFormat::kShort));
return TimeFormat(formatter.get(), time);
}
string16 TimeFormatFriendlyDateAndTime(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(
icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull));
return TimeFormat(formatter.get(), time);
}
string16 TimeFormatFriendlyDate(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(icu::DateFormat::createDateInstance(
icu::DateFormat::kFull));
return TimeFormat(formatter.get(), time);
}
HourClockType GetHourClockType() {
scoped_ptr<icu::SimpleDateFormat> formatter(
static_cast<icu::SimpleDateFormat*>(
icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)));
icu::UnicodeString pattern_unicode;
formatter->toPattern(pattern_unicode);
if (pattern_unicode.indexOf('a') == -1) {
return k24HourClock;
} else {
return k12HourClock;
}
}
}