2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FLclDateTimeFormatter.cpp
20 * @brief This is the implementation file for DateTimeFormatter class.
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FApp_AppInfo.h>
27 #include <FLclDateTimeFormatter.h>
28 #include "FLcl_DateTimeFormatterImpl.h"
29 #include "FLcl_LocaleData.h"
30 #include "FLcl_LocaleManagerImpl.h"
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Base::Utility;
36 namespace Tizen { namespace Locales
39 _DateTimeFormatterImpl::_DateTimeFormatterImpl(void)
45 _DateTimeFormatterImpl::~_DateTimeFormatterImpl(void)
51 // The DateTimeFormatter always use System Locale if not provided by the user.
52 // System locale may be different for default ICU locale.
54 _DateTimeFormatterImpl::CreateInstanceN(DateTimeStyle dateStyle, DateTimeStyle timeStyle)
56 result r = E_INVALID_ARG;
58 if (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat())
60 r = E_UNSUPPORTED_OPERATION;
63 SysTryReturn(NID_LCL, ValidateDateTimeStyle(dateStyle), null, r, "[%s] Invalid argument is used. dateStyle is invalid", GetErrorMessage(r));
64 SysTryReturn(NID_LCL, ValidateDateTimeStyle(timeStyle), null, r, "[%s] Invalid argument is used. timeStyle is invalid", GetErrorMessage(r));
66 // using system locale, may be different from ICU default locale
67 DateTimeFormatter* pDateTimeFormatter = CreateInstanceN(_LocaleManagerImpl::GetSystemLocale(), dateStyle, timeStyle);
69 if (GetLastResult() == E_INVALID_ARG)
71 SetLastResult(E_UNSUPPORTED_OPERATION);
73 return pDateTimeFormatter;
77 _DateTimeFormatterImpl::CreateInstanceN(const Locale& locale, DateTimeStyle dateStyle, DateTimeStyle timeStyle)
79 result r = (Tizen::App::_AppInfo::GetApiVersion() == _API_VERSION_2_0 && Tizen::App::_AppInfo::IsOspCompat()) ? E_UNSUPPORTED_OPERATION : E_INVALID_ARG;
81 SysTryReturn(NID_LCL, ValidateDateTimeStyle(dateStyle), null, r, "[%s] Invalid argument is used. dateStyle is invalid", GetErrorMessage(r));
82 SysTryReturn(NID_LCL, ValidateDateTimeStyle(timeStyle), null, r, "[%s] Invalid argument is used. timeStyle is invalid", GetErrorMessage(r));
86 // Create a DateTimeFormatter of the desired style.
87 std::unique_ptr<DateTimeFormatter> pDateTimeFormatter(new (std::nothrow) DateTimeFormatter());
88 SysTryReturn(NID_LCL, pDateTimeFormatter, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
90 std::unique_ptr<_DateTimeFormatterImpl> pDateTimeFormatterImpl(new (std::nothrow) _DateTimeFormatterImpl);
91 SysTryReturn(NID_LCL,pDateTimeFormatterImpl, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
93 std::unique_ptr<_LocaleData> pLocaleData(new (std::nothrow) _LocaleData);
94 SysTryReturn(NID_LCL, pLocaleData, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
96 // This create and set ICU DateFormatter in _Locale data object for future use
97 r = pLocaleData->SetDateTimeFormatter(dateStyle, timeStyle, locale);
98 SysTryReturn(NID_LCL, !IsFailed(r), null, r, "[%s]",GetErrorMessage(r));
100 // Create DateTimeSymbols and construct using locale
101 pDateTimeFormatterImpl->__pSymbols = new (std::nothrow) DateTimeSymbols;
102 if (pDateTimeFormatterImpl->__pSymbols)
104 pDateTimeFormatterImpl->__pSymbols->Construct(locale);
107 pDateTimeFormatter->__pDateTimeFormatterImpl = pDateTimeFormatterImpl.release();
108 pDateTimeFormatter->__pDateTimeFormatterImpl->__pLocaleData = pLocaleData.release();
109 return pDateTimeFormatter.release(); // Here as LastResult is already cleared, no need to set it to E_SUCCESS
112 // This function will not have TimeZone informations and always print time zone as GMT+00:00
114 _DateTimeFormatterImpl::Format(const DateTime& date, String& strBuf) const
116 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
117 return __pLocaleData->FormatDateTime(date, strBuf); // Format date using ICU date formatter
121 _DateTimeFormatterImpl::Format(const Calendar& calendar, String& strBuf) const
123 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
124 return __pLocaleData->FormatDateTime(calendar, strBuf); // Format date using ICU date formatter
127 // This function sets pattern used by DateTimeFormatter for formatting date.
128 // Please check http://icu-project.org/apiref/icu4c/classSimpleDateFormat.html for possible letter and
129 // their meaning in patter string.
131 _DateTimeFormatterImpl::ApplyPattern(const String& pattern)
133 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
134 return __pLocaleData->SetDateTimePattern(pattern); // Set pattern to ICU date formatter
138 _DateTimeFormatterImpl::GetPattern(void) const
140 return __pLocaleData->GetDateTimePattern();
143 const DateTimeSymbols*
144 _DateTimeFormatterImpl::GetDateTimeSymbols(void) const
150 _DateTimeFormatterImpl::SetDateTimeSymbols(const DateTimeSymbols& newSymbols)
152 SysAssertf(__pLocaleData != null, "Not yet constructed! Construct() should be called before use.");
154 result r = __pLocaleData->SetDateTimeSymbols(newSymbols); // Set symbols to ICU date formatter
159 __pSymbols = __pLocaleData->GetDateTimeSymbolsN(__pSymbols); // Set __pSymbols obtained from ICU date formatter
165 _DateTimeFormatterImpl::ValidateDateTimeStyle(DateTimeStyle dateTimeStyle)
167 return (dateTimeStyle < DATE_TIME_STYLE_NONE || dateTimeStyle > DATE_TIME_STYLE_SHORT)? false : true;