OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/i18n/time_formatting.h" | 5 #include "base/i18n/time_formatting.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "unicode/datefmt.h" | 11 #include "unicode/datefmt.h" |
12 | 12 |
13 using base::Time; | 13 using base::Time; |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 std::wstring TimeFormat(const icu::DateFormat* formatter, | 17 string16 TimeFormat(const icu::DateFormat* formatter, |
18 const Time& time) { | 18 const Time& time) { |
19 DCHECK(formatter); | 19 DCHECK(formatter); |
20 icu::UnicodeString date_string; | 20 icu::UnicodeString date_string; |
21 | 21 |
22 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); | 22 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); |
23 std::wstring output; | 23 return string16(date_string.getBuffer(), |
24 bool success = UTF16ToWide(date_string.getBuffer(), date_string.length(), | 24 static_cast<size_t>(date_string.length())); |
25 &output); | |
26 DCHECK(success); | |
27 return output; | |
28 } | 25 } |
29 | 26 |
30 } // namespace | 27 } // namespace |
31 | 28 |
32 namespace base { | 29 namespace base { |
33 | 30 |
34 std::wstring TimeFormatTimeOfDay(const Time& time) { | 31 string16 TimeFormatTimeOfDay(const Time& time) { |
35 // We can omit the locale parameter because the default should match | 32 // We can omit the locale parameter because the default should match |
36 // Chrome's application locale. | 33 // Chrome's application locale. |
37 scoped_ptr<icu::DateFormat> formatter( | 34 scoped_ptr<icu::DateFormat> formatter( |
38 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); | 35 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); |
39 return TimeFormat(formatter.get(), time); | 36 return TimeFormat(formatter.get(), time); |
40 } | 37 } |
41 | 38 |
42 std::wstring TimeFormatShortDate(const Time& time) { | 39 string16 TimeFormatShortDate(const Time& time) { |
43 scoped_ptr<icu::DateFormat> formatter( | 40 scoped_ptr<icu::DateFormat> formatter( |
44 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); | 41 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); |
45 return TimeFormat(formatter.get(), time); | 42 return TimeFormat(formatter.get(), time); |
46 } | 43 } |
47 | 44 |
48 std::wstring TimeFormatShortDateNumeric(const Time& time) { | 45 string16 TimeFormatShortDateNumeric(const Time& time) { |
49 scoped_ptr<icu::DateFormat> formatter( | 46 scoped_ptr<icu::DateFormat> formatter( |
50 icu::DateFormat::createDateInstance(icu::DateFormat::kShort)); | 47 icu::DateFormat::createDateInstance(icu::DateFormat::kShort)); |
51 return TimeFormat(formatter.get(), time); | 48 return TimeFormat(formatter.get(), time); |
52 } | 49 } |
53 | 50 |
54 std::wstring TimeFormatShortDateAndTime(const Time& time) { | 51 string16 TimeFormatShortDateAndTime(const Time& time) { |
55 scoped_ptr<icu::DateFormat> formatter( | 52 scoped_ptr<icu::DateFormat> formatter( |
56 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kShort)); | 53 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kShort)); |
57 return TimeFormat(formatter.get(), time); | 54 return TimeFormat(formatter.get(), time); |
58 } | 55 } |
59 | 56 |
60 std::wstring TimeFormatFriendlyDateAndTime(const Time& time) { | 57 string16 TimeFormatFriendlyDateAndTime(const Time& time) { |
61 scoped_ptr<icu::DateFormat> formatter( | 58 scoped_ptr<icu::DateFormat> formatter( |
62 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull)); | 59 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kFull)); |
63 return TimeFormat(formatter.get(), time); | 60 return TimeFormat(formatter.get(), time); |
64 } | 61 } |
65 | 62 |
66 std::wstring TimeFormatFriendlyDate(const Time& time) { | 63 string16 TimeFormatFriendlyDate(const Time& time) { |
67 scoped_ptr<icu::DateFormat> formatter(icu::DateFormat::createDateInstance( | 64 scoped_ptr<icu::DateFormat> formatter(icu::DateFormat::createDateInstance( |
68 icu::DateFormat::kFull)); | 65 icu::DateFormat::kFull)); |
69 return TimeFormat(formatter.get(), time); | 66 return TimeFormat(formatter.get(), time); |
70 } | 67 } |
71 | 68 |
72 } // namespace base | 69 } // namespace base |
OLD | NEW |