summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorsatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-14 05:08:02 +0000
committersatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-14 05:08:02 +0000
commit3b250d77202f8e830f468a2e96877d859260920a (patch)
treeeb7666b3128d764fcfdb82624cce9e0a33a5bc8e /base
parentc429713c4b93a1b1f9106734fe4b88b336aef4ae (diff)
downloadchromium_src-3b250d77202f8e830f468a2e96877d859260920a.zip
chromium_src-3b250d77202f8e830f468a2e96877d859260920a.tar.gz
chromium_src-3b250d77202f8e830f468a2e96877d859260920a.tar.bz2
Add TimeFormatTimeOfDayWithHourClockType() and GetHourClockType().
These functions will be used to implement 12/24 hour clock switching on Chrome OS. BUG=chromium-os:10534 TEST=with another patch, confirmed that the 12/24 hour clock switching worked Review URL: http://codereview.chromium.org/6835018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81532 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/i18n/time_formatting.cc70
-rw-r--r--base/i18n/time_formatting.h19
2 files changed, 88 insertions, 1 deletions
diff --git a/base/i18n/time_formatting.cc b/base/i18n/time_formatting.cc
index 823f7a6..e52bd22 100644
--- a/base/i18n/time_formatting.cc
+++ b/base/i18n/time_formatting.cc
@@ -9,6 +9,8 @@
#include "base/utf_string_conversions.h"
#include "base/time.h"
#include "unicode/datefmt.h"
+#include "unicode/dtptngen.h"
+#include "unicode/smpdtfmt.h"
using base::Time;
@@ -36,6 +38,34 @@ string16 TimeFormatTimeOfDay(const Time& time) {
return TimeFormat(formatter.get(), time);
}
+string16 TimeFormatTimeOfDayWithHourClockType(const Time& time,
+ HourClockType type) {
+ // Just redirect to the normal function if the default type matches the
+ // given type.
+ HourClockType default_type = GetHourClockType();
+ if (default_type == type) {
+ return TimeFormatTimeOfDay(time);
+ }
+
+ // Generate a locale-dependent format pattern. The generator will take
+ // care of locale-dependent formatting issues like which separator to
+ // use (some locales use '.' instead of ':'), and where to put the am/pm
+ // marker.
+ UErrorCode status = U_ZERO_ERROR;
+ icu::DateTimePatternGenerator *generator =
+ icu::DateTimePatternGenerator::createInstance(status);
+ CHECK(U_SUCCESS(status));
+ const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm");
+ icu::UnicodeString generated_pattern =
+ generator->getBestPattern(icu::UnicodeString(base_pattern), status);
+ CHECK(U_SUCCESS(status));
+
+ // Then, format the time using the generated pattern.
+ icu::SimpleDateFormat formatter(generated_pattern, status);
+ CHECK(U_SUCCESS(status));
+ return TimeFormat(&formatter, time);
+}
+
string16 TimeFormatShortDate(const Time& time) {
scoped_ptr<icu::DateFormat> formatter(
icu::DateFormat::createDateInstance(icu::DateFormat::kMedium));
@@ -66,4 +96,44 @@ string16 TimeFormatFriendlyDate(const Time& time) {
return TimeFormat(formatter.get(), time);
}
+HourClockType GetHourClockType() {
+ // TODO(satorux,jshin): Rework this with ures_getByKeyWithFallback()
+ // once it becomes public. The short time format can be found at
+ // "calendar/gregorian/DateTimePatterns/3" in the resources.
+ scoped_ptr<icu::SimpleDateFormat> formatter(
+ static_cast<icu::SimpleDateFormat*>(
+ icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)));
+ // Retrieve the short time format.
+ icu::UnicodeString pattern_unicode;
+ formatter->toPattern(pattern_unicode);
+
+ // Determine what hour clock type the current locale uses, by checking
+ // "a" (am/pm marker) in the short time format. This is reliable as "a"
+ // is used by all of 12-hour clock formats, but not any of 24-hour clock
+ // formats, as shown below.
+ //
+ // % grep -A4 DateTimePatterns third_party/icu/source/data/locales/*.txt |
+ // grep -B1 -- -- |grep -v -- '--' |
+ // perl -nle 'print $1 if /^\S+\s+"(.*)"/' |sort -u
+ //
+ // H.mm
+ // H:mm
+ // HH.mm
+ // HH:mm
+ // a h:mm
+ // ah:mm
+ // ahh:mm
+ // h-mm a
+ // h:mm a
+ // hh:mm a
+ //
+ // See http://userguide.icu-project.org/formatparse/datetime for details
+ // about the date/time format syntax.
+ if (pattern_unicode.indexOf('a') == -1) {
+ return k24HourClock;
+ } else {
+ return k12HourClock;
+ }
+}
+
} // namespace base
diff --git a/base/i18n/time_formatting.h b/base/i18n/time_formatting.h
index e70ad3d..99d1911 100644
--- a/base/i18n/time_formatting.h
+++ b/base/i18n/time_formatting.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -15,9 +15,21 @@ namespace base {
class Time;
+// Argument type used to specify the hour clock type.
+enum HourClockType {
+ k12HourClock, // Uses 1-12. e.g., "3:07 PM"
+ k24HourClock, // Uses 0-23. e.g., "15:07"
+};
+
// Returns the time of day, e.g., "3:07 PM".
string16 TimeFormatTimeOfDay(const Time& time);
+// Returns the time of day in the specified hour clock type. e.g.
+// "3:07 PM" (type == k12HourClock).
+// "15:07" (type == k24HourClock).
+string16 TimeFormatTimeOfDayWithHourClockType(const Time& time,
+ HourClockType type);
+
// Returns a shortened date, e.g. "Nov 7, 2007"
string16 TimeFormatShortDate(const Time& time);
@@ -36,6 +48,11 @@ string16 TimeFormatFriendlyDateAndTime(const Time& time);
// "Monday, March 6, 2008".
string16 TimeFormatFriendlyDate(const Time& time);
+// Gets the hour clock type of the current locale. e.g.
+// k12HourClock (en-US).
+// k24HourClock (en-GB).
+HourClockType GetHourClockType();
+
} // namespace base
#endif // BASE_I18N_TIME_FORMATTING_H_