2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FLclDateTimeFormatter.cpp
19 * @brief This is the implementation file for DateTimeFormatter class.
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FApp_AppInfo.h>
26 #include <FLclDateTimeFormatter.h>
27 #include "FLcl_DateTimeFormatterImpl.h"
28 #include "FLcl_LocaleData.h"
29 #include "FLcl_LocaleManagerImpl.h"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
35 namespace Tizen { namespace Locales
38 _DateTimeFormatterImpl::_DateTimeFormatterImpl(void)
44 _DateTimeFormatterImpl::~_DateTimeFormatterImpl(void)
50 // The DateTimeFormatter always use System Locale if not provided by the user.
51 // System locale may be different for default ICU locale.
53 _DateTimeFormatterImpl::CreateInstanceN(DateTimeStyle dateStyle, DateTimeStyle timeStyle)
55 result r = E_INVALID_ARG;
57 if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
59 r = E_UNSUPPORTED_OPERATION;
62 SysTryReturn(NID_LCL, ValidateDateTimeStyle(dateStyle), null, r, "[%s] Invalid argument is used. dateStyle is invalid", GetErrorMessage(r));
63 SysTryReturn(NID_LCL, ValidateDateTimeStyle(timeStyle), null, r, "[%s] Invalid argument is used. timeStyle is invalid", GetErrorMessage(r));
65 // using system locale, may be different from ICU default locale
66 DateTimeFormatter* pDateTimeFormatter = CreateInstanceN(_LocaleManagerImpl::GetSystemLocale(), dateStyle, timeStyle);
68 if (GetLastResult() == E_INVALID_ARG)
70 SetLastResult(E_UNSUPPORTED_OPERATION);
72 return pDateTimeFormatter;
76 _DateTimeFormatterImpl::CreateInstanceN(const Locale& locale, DateTimeStyle dateStyle, DateTimeStyle timeStyle)
78 result r = (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat()) ? E_UNSUPPORTED_OPERATION : E_INVALID_ARG;
80 SysTryReturn(NID_LCL, ValidateDateTimeStyle(dateStyle), null, r, "[%s] Invalid argument is used. dateStyle is invalid", GetErrorMessage(r));
81 SysTryReturn(NID_LCL, ValidateDateTimeStyle(timeStyle), null, r, "[%s] Invalid argument is used. timeStyle is invalid", GetErrorMessage(r));
85 // Create a DateTimeFormatter of the desired style.
86 std::unique_ptr<DateTimeFormatter> pDateTimeFormatter(new (std::nothrow) DateTimeFormatter());
87 SysTryReturn(NID_LCL, pDateTimeFormatter, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
89 std::unique_ptr<_DateTimeFormatterImpl> pDateTimeFormatterImpl(new (std::nothrow) _DateTimeFormatterImpl);
90 SysTryReturn(NID_LCL,pDateTimeFormatterImpl, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
92 std::unique_ptr<_LocaleData> pLocaleData(new (std::nothrow) _LocaleData);
93 SysTryReturn(NID_LCL, pLocaleData, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
95 // This create and set ICU DateFormatter in _Locale data object for future use
96 r = pLocaleData->SetDateTimeFormatter(dateStyle, timeStyle, locale);
97 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s]",GetErrorMessage(r));
99 // Create DateTimeSymbols and construct using locale
100 pDateTimeFormatterImpl->__pSymbols = new (std::nothrow) DateTimeSymbols;
101 if (pDateTimeFormatterImpl->__pSymbols)
103 pDateTimeFormatterImpl->__pSymbols->Construct(locale);
106 pDateTimeFormatter->__pDateTimeFormatterImpl = pDateTimeFormatterImpl.release();
107 pDateTimeFormatter->__pDateTimeFormatterImpl->__pLocaleData = pLocaleData.release();
108 return pDateTimeFormatter.release(); // Here as LastResult is already cleared, no need to set it to E_SUCCESS
111 // This function will not have TimeZone informations and always print time zone as GMT+00:00
113 _DateTimeFormatterImpl::Format(const DateTime& date, String& strBuf) const
115 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
116 return __pLocaleData->FormatDateTime(date, strBuf); // Format date using ICU date formatter
120 _DateTimeFormatterImpl::Format(const Calendar& calendar, String& strBuf) const
122 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
123 return __pLocaleData->FormatDateTime(calendar, strBuf); // Format date using ICU date formatter
126 // This function sets pattern used by DateTimeFormatter for formatting date.
127 // Please check http://icu-project.org/apiref/icu4c/classSimpleDateFormat.html for possible letter and
128 // their meaning in patter string.
130 _DateTimeFormatterImpl::ApplyPattern(const String& pattern)
132 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
133 return __pLocaleData->SetDateTimePattern(pattern); // Set pattern to ICU date formatter
137 _DateTimeFormatterImpl::GetPattern(void) const
139 return __pLocaleData->GetDateTimePattern();
142 const DateTimeSymbols*
143 _DateTimeFormatterImpl::GetDateTimeSymbols(void) const
149 _DateTimeFormatterImpl::SetDateTimeSymbols(const DateTimeSymbols& newSymbols)
151 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
153 result r = __pLocaleData->SetDateTimeSymbols(newSymbols); // Set symbols to ICU date formatter
158 __pSymbols = __pLocaleData->GetDateTimeSymbolsN(__pSymbols); // Set __pSymbols obtained from ICU date formatter
164 _DateTimeFormatterImpl::ValidateDateTimeStyle(DateTimeStyle dateTimeStyle)
166 return (dateTimeStyle < DATE_TIME_STYLE_NONE || dateTimeStyle > DATE_TIME_STYLE_SHORT)? false : true;