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 FLclLocaleData.cpp
19 * @brief This is the implementation file for _LocaleData class.
25 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilStringUtil.h>
28 #include "FBase_StringConverter.h"
29 #include "FLcl_LocaleData.h"
33 #define LOCALE_DATA_NUM_OF_MILLISEC_IN_HOUR 3600000LL // number of mili second in a hour
34 #define LOCALE_DATA_NUM_OF_MILLISEC_IN_MINUTE 60000LL // number of mili second in a minute
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Utility;
40 namespace Tizen { namespace Locales
44 _LocaleData::GetIcuFieldPosition(_FieldPosition pos)
46 IcuFieldPosition icuPos(pos.GetField()); // Create IcuFieldPosition with field value in pos
47 icuPos.setBeginIndex(pos.GetBeginIndex()); // Set Beginning index
48 icuPos.setEndIndex(pos.GetEndIndex()); // Set End index
53 U_ICU_NAMESPACE::UnicodeString
54 _LocaleData::GetIcuString(const String& ospStr)
56 IcuUnicodeString icuStr;
58 if (!ospStr.IsEmpty()) // if ospStr is not empty
60 ByteBuffer* pBuff = null;
61 pBuff = StringUtil::StringToUtf8N(ospStr); // Convert unicode value to UTF8
64 icuStr = IcuUnicodeString((const char*) pBuff->GetPointer()); // Create ICU string using UTF8 array
65 icuStr.setCharAt(0, icuStr.charAt(0)); // This is to handle ICU copy on write design
66 delete pBuff; // delete temporary buffer
75 _LocaleData::GetOspString(const IcuUnicodeString& icuStr)
77 int len = icuStr.length(); // get length
78 if (len > 0) // if icuStr is not empty
80 wchar_t wstr[len + 1];
81 IcuUnicodeString tmpIcuStr(icuStr);
82 for (int i = 0; i < len ; i++)
85 icuChar = tmpIcuStr.charAt(i);
86 wstr[i] = (wchar_t)icuChar;
96 _LocaleData::ConvertIcuStringArrayToOspArrayN(const IcuUnicodeString* pIcuStrList, int count)
98 SysTryReturn(NID_LCL, pIcuStrList && count > 0, null, E_INVALID_ARG, "It is invalid argument.");
100 result r = E_SUCCESS;
101 std::unique_ptr< ArrayList> pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
102 SysTryReturn(NID_LCL, pTempArrayList, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
103 pTempArrayList->Construct(count);
105 for (int i = 0; i < count; i++)
107 std::unique_ptr< String > pString(new (std::nothrow) String(_LocaleData::GetOspString(pIcuStrList[i])));
108 SysTryReturn(NID_LCL, pString, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
110 if (!pString->IsEmpty())
112 r = pTempArrayList->Add(pString.get()); // Add OSP string to arraylist if it is not empty
113 SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to add string [%ls]", pString->GetPointer());
118 SetLastResult(r); // Setting last result value
119 return pTempArrayList.release(); // Return array list
123 _LocaleData::ConvertOspArrayToIcuStringArrayN(const Tizen::Base::Collection::IList* pIcuStrList, int& count)
126 SysTryReturn(NID_LCL, pIcuStrList, null, E_INVALID_ARG, "[%s] Invalid argument is used. pIcuStrList is null.", GetErrorMessage(E_INVALID_ARG));
128 count = pIcuStrList->GetCount();
129 SysTryReturn(NID_LCL, count > 0, null, E_INVALID_ARG, "The list is empty.");
131 std::unique_ptr< IcuUnicodeString[] > pIcuStrArray(new IcuUnicodeString[count]);
132 SysTryReturn(NID_LCL, pIcuStrArray, null, E_OUT_OF_MEMORY, "It is not enough memory.");
134 std::unique_ptr< IEnumerator > pEnum(pIcuStrList->GetEnumeratorN());
135 SysTryReturn(NID_LCL, pEnum, null, E_SYSTEM, "It is failed to get enumerator.");
139 while (pEnum->MoveNext() == E_SUCCESS)
141 pObj = static_cast< String* >(pEnum->GetCurrent());
144 pIcuStrArray[i] = GetIcuString(*pObj);
148 return pIcuStrArray.release();
153 _LocaleData::GetIcuLocale(const Locale& ospLocale)
156 String language = ospLocale.GetLanguageCodeString();
157 String country = ospLocale.GetCountryCodeString();
158 String variant = ospLocale.GetVariantCodeString();
160 const char* pLangStr = _StringConverter::CopyToCharArrayN(language);
161 const char* pCountryStr = _StringConverter::CopyToCharArrayN(country);
162 const char* pVariantStr = null;
164 if (!variant.IsEmpty())
166 pVariantStr = _StringConverter::CopyToCharArrayN(variant);
169 IcuLocale icuLocale = IcuLocale(pLangStr, pCountryStr, pVariantStr);
172 delete[] pCountryStr;
173 delete[] pVariantStr;
175 if (icuLocale.isBogus())
177 SetLastResult(E_SYSTEM);
185 _LocaleData::GetIcuDate(DateTime date)
187 DateTime icuBaseTime;
188 icuBaseTime.SetValue(1970, 1, 1);
189 DateTime ospBaseTime = DateTime::GetMinValue();
191 result r = icuBaseTime.Subtract(ospBaseTime.GetTime());
194 SysLogException(NID_LCL, r, "[%s] Propagated.", GetErrorMessage(r));
197 TimeSpan tsIcu = icuBaseTime.GetTime();
198 TimeSpan tsOsp = date.GetTime();
199 TimeSpan diff = tsOsp - tsIcu;
201 IcuUDate icuDate = diff.GetTicks();
205 _LocaleData::_LocaleData(void)
210 _LocaleData::~_LocaleData(void)