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 struct timeval currentTimeVal;
83 _LocaleManagerImpl* plm = null;
86 gettimeofday(¤tTimeVal, null);
88 pGmTime = gmtime(&currTime);
89 SysTryReturnResult(NID_SYS, null != pGmTime, E_SYSTEM, "Failed to convert the time value to UTC time");
91 plm = new (std::nothrow) _LocaleManagerImpl();
92 SysTryReturnResult(NID_SYS, null != plm, E_SYSTEM, "_LocaleManagerImpl instance must not be null");
94 timeZone = plm->GetSystemTimeZone();
96 String timeZoneId = timeZone.GetId();
97 int rawOffset = timeZone.GetRawOffset();
98 int dstSavings = timeZone.GetDstSavings();
99 SysLog(NID_SYS, "Time zone: %S, %d, %d", timeZoneId.GetPointer(), rawOffset, dstSavings);
102 tempTime.SetValue(pGmTime->tm_year + 1900, pGmTime->tm_mon + 1, pGmTime->tm_mday, pGmTime->tm_hour, pGmTime->tm_min, pGmTime->tm_sec, currentTimeVal.tv_usec / 1000);
104 SysLog(NID_SYS, "Original Time %d %d/%d, %d:%d:%d:%d", tempTime.GetYear(), tempTime.GetMonth(), tempTime.GetDay(), tempTime.GetHour(), tempTime.GetMinute(), tempTime.GetSecond(), tempTime.GetMillisecond());
109 currentTime.SetValue(tempTime);
113 currentTime = timeZone.UtcTimeToStandardTime(tempTime);
117 currentTime = timeZone.UtcTimeToWallTime(tempTime);
124 SysLog(NID_SYS, "Current Time %d %d/%d, %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
130 _SystemTimeImpl::GetCurrentTime(DateTime& currentTime)
132 return GetCurrentTime(UTC_TIME, currentTime);
136 _SystemTimeImpl::SetCurrentTime(const DateTime& currentTime)
138 result r = E_SUCCESS;
140 ArrayList requestMessage;
141 ArrayList responseMessage;
142 char datetime[32] = {0,};
144 LocaleManager localeManager;
145 localeManager.Construct();
146 TimeZone timeZone = localeManager.GetSystemTimeZone();
148 DateTime wallTime = timeZone.UtcTimeToWallTime(currentTime);
150 std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
151 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
152 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to create IpcClient", GetErrorMessage(r));
154 requestMessage.Construct();
155 responseMessage.Construct();
157 String serviceId = _SYSTEM_SERVICE_ID;
158 String commandId = _SYSTEM_COMMAND_CHANGE_TIME ;
161 sprintf(datetime, "%03d %02d %02d %02d:%02d:%02d:%03d", wallTime.GetYear() - 1900, wallTime.GetMonth()-1, wallTime.GetDay(), wallTime.GetHour(), wallTime.GetMinute(), wallTime.GetSecond(), wallTime.GetMillisecond());
162 dateTime.Append(datetime);
164 requestMessage.Add(serviceId);
165 requestMessage.Add(commandId);
166 requestMessage.Add(dateTime);
168 std::unique_ptr<IoService_Request> pMsg (new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
169 SysTryReturnResult(NID_SYS, pMsg != null, E_SYSTEM, "[E_SYSTEM] fail to create Ipc message");
171 r = pIpcClient->SendRequest(pMsg.get());
172 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to ipc message", GetErrorMessage(r));
174 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
176 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "[E_SYSTEM] fail to receive Ipc response");
177 SysTryReturnResult(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] applciation does not have proper privilege.");
178 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "[E_SYSTEM] fail to set time");
180 vconf_sync_key(VCONFKEY_SYSTEM_TIME_CHANGED);
185 _SystemTimeImpl::GetTicks(long long& ticks)
188 struct timeval timeValue;
190 ret = gettimeofday(&timeValue, null);
191 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the date and Time");
193 ticks = (long long) timeValue.tv_sec * 1000ll;
194 ticks += (long long) timeValue.tv_usec / 1000ll;
200 _SystemTimeImpl::GetNanoTicks(long long& nanoTicks)
203 struct timespec realTime;
205 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &realTime);
207 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get clock information");
209 nanoTicks = (long long) realTime.tv_sec * 1000000000;
210 nanoTicks += (long long) realTime.tv_nsec;
216 _SystemTimeImpl::GetInstance(SystemTime& systemtime)
218 return systemtime.__pSystemTimeImpl;
221 const _SystemTimeImpl*
222 _SystemTimeImpl::GetInstance(const SystemTime& systemtime)
224 return systemtime.__pSystemTimeImpl;