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 FSys_SystemTimeImpl.cpp
20 * @brief This is the implementation file for _SystemTimeImpl class.
23 #include <unique_ptr.h>
25 #include <sys/sysinfo.h>
30 #include <FBaseDateTime.h>
31 #include <FBaseResult.h>
32 #include <FBaseColArrayList.h>
33 #include <FBaseSysLog.h>
35 #include <FLcl_LocaleManagerImpl.h>
36 #include <FSys_Types.h>
37 #include <FSys_SystemTimeImpl.h>
38 #include <FIo_AppServiceIpcMessages.h>
39 #include <FIo_IpcClient.h>
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Io;
44 using namespace Tizen::Locales;
46 namespace Tizen { namespace System
49 _SystemTimeImpl::_SystemTimeImpl(void)
53 _SystemTimeImpl::~_SystemTimeImpl(void)
58 _SystemTimeImpl::GetUptime(TimeSpan& uptime)
62 struct timespec timeSpec;
64 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &timeSpec);
65 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the time ");
67 ticks = (long long) timeSpec.tv_sec * 1000ll;
68 ticks += (long long) timeSpec.tv_nsec / 1000000ll;
69 uptime = TimeSpan(ticks);
75 _SystemTimeImpl::GetCurrentTime(TimeMode timeMode, DateTime& currentTime)
77 struct tm* pGmTime = null;
81 _LocaleManagerImpl* plm = null;
85 pGmTime = gmtime(&currTime);
86 SysTryReturnResult(NID_SYS, null != pGmTime, E_SYSTEM, "Failed to convert the time value to UTC time");
88 plm = new (std::nothrow) _LocaleManagerImpl();
89 SysTryReturnResult(NID_SYS, null != plm, E_SYSTEM, "_LocaleManagerImpl instance must not be null");
91 timeZone = plm->GetSystemTimeZone();
93 String timeZoneId = timeZone.GetId();
94 int rawOffset = timeZone.GetRawOffset();
95 int dstSavings = timeZone.GetDstSavings();
96 SysLog(NID_SYS, "Time zone: %S, %d, %d", timeZoneId.GetPointer(), rawOffset, dstSavings);
99 tempTime.SetValue(pGmTime->tm_year + 1900, pGmTime->tm_mon + 1, pGmTime->tm_mday, pGmTime->tm_hour, pGmTime->tm_min, pGmTime->tm_sec);
101 SysLog(NID_SYS, "Temp Time %d %d/%d, %d:%d:%d", tempTime.GetYear(), tempTime.GetMonth(), tempTime.GetDay(), tempTime.GetHour(), tempTime.GetMinute(), tempTime.GetSecond());
106 currentTime.SetValue(tempTime);
110 currentTime = timeZone.UtcTimeToStandardTime(tempTime);
114 currentTime = timeZone.UtcTimeToWallTime(tempTime);
121 SysLog(NID_SYS, "Current Time %d %d/%d, %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
127 _SystemTimeImpl::GetCurrentTime(DateTime& currentTime)
129 return GetCurrentTime(UTC_TIME, currentTime);
133 _SystemTimeImpl::SetCurrentTime(const DateTime& currentTime)
135 result r = E_SUCCESS;
137 ArrayList requestMessage;
138 ArrayList responseMessage;
139 char datetime[32] = {0,};
141 LocaleManager localeManager;
142 localeManager.Construct();
143 TimeZone timeZone = localeManager.GetSystemTimeZone();
145 DateTime wallTime = timeZone.UtcTimeToWallTime(currentTime);
147 std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
148 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
149 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to create IpcClient", GetErrorMessage(r));
151 requestMessage.Construct();
152 responseMessage.Construct();
154 String serviceId = _SYSTEM_SERVICE_ID;
155 String commandId = _SYSTEM_COMMAND_CHANGE_TIME ;
158 sprintf(datetime, "%03d %02d %02d %02d:%02d:%02d", wallTime.GetYear() - 1900, wallTime.GetMonth()-1, wallTime.GetDay(), wallTime.GetHour(), wallTime.GetMinute(), wallTime.GetSecond());
159 dateTime.Append(datetime);
161 requestMessage.Add(serviceId);
162 requestMessage.Add(commandId);
163 requestMessage.Add(dateTime);
165 std::unique_ptr<IoService_Request> pMsg (new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
166 SysTryReturnResult(NID_SYS, pMsg != null, E_SYSTEM, "[E_SYSTEM] fail to create Ipc message");
168 r = pIpcClient->SendRequest(pMsg.get());
169 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to ipc message", GetErrorMessage(r));
171 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
173 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "[E_SYSTEM] fail to receive Ipc response");
174 SysTryReturnResult(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] applciation does not have proper privilege.");
175 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "[E_SYSTEM] fail to set time");
177 vconf_sync_key(VCONFKEY_SYSTEM_TIME_CHANGED);
182 _SystemTimeImpl::GetTicks(long long& ticks)
185 struct timeval timeValue;
187 ret = gettimeofday(&timeValue, null);
188 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the date and Time");
190 ticks = (long long) timeValue.tv_sec * 1000ll;
191 ticks += (long long) timeValue.tv_usec / 1000ll;
197 _SystemTimeImpl::GetNanoTicks(long long& nanoTicks)
200 struct timespec realTime;
202 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &realTime);
204 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get clock information");
206 nanoTicks = (long long) realTime.tv_sec * 1000000000;
207 nanoTicks += (long long) realTime.tv_nsec;
213 _SystemTimeImpl::GetInstance(SystemTime& systemtime)
215 return systemtime.__pSystemTimeImpl;
218 const _SystemTimeImpl*
219 _SystemTimeImpl::GetInstance(const SystemTime& systemtime)
221 return systemtime.__pSystemTimeImpl;