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 FSys_SystemTimeImpl.cpp
19 * @brief This is the implementation file for _SystemTimeImpl class.
22 #include <unique_ptr.h>
24 #include <sys/sysinfo.h>
29 #include <FBaseDateTime.h>
30 #include <FBaseResult.h>
31 #include <FBaseColArrayList.h>
32 #include <FBaseSysLog.h>
34 #include <FLcl_LocaleManagerImpl.h>
35 #include <FSys_Types.h>
36 #include <FSys_SystemTimeImpl.h>
37 #include <FIo_AppServiceIpcMessages.h>
38 #include <FIo_IpcClient.h>
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
43 using namespace Tizen::Locales;
45 namespace Tizen { namespace System
48 _SystemTimeImpl::_SystemTimeImpl(void)
52 _SystemTimeImpl::~_SystemTimeImpl(void)
57 _SystemTimeImpl::GetUptime(TimeSpan& uptime)
61 struct timespec timeSpec;
63 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &timeSpec);
64 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the time ");
66 ticks = (long long) timeSpec.tv_sec * 1000ll;
67 ticks += (long long) timeSpec.tv_nsec / 1000000ll;
68 uptime = TimeSpan(ticks);
74 _SystemTimeImpl::GetCurrentTime(TimeMode timeMode, DateTime& currentTime)
76 struct tm* pGmTime = null;
80 struct timeval currentTimeVal;
82 _LocaleManagerImpl* plm = null;
85 SysLog(NID_SYS, "Current Epoch is %lld.", currTime);
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");
90 SysLog(NID_SYS, "Calendar time is sec:%d, min:%d, hour:%d, day:%d, mon:%d, year:%d, wday%d, year:%d, dst:%d", pGmTime->tm_sec, pGmTime->tm_min, pGmTime->tm_hour, pGmTime->tm_mday, pGmTime->tm_mon, pGmTime->tm_year, pGmTime->tm_wday, pGmTime->tm_yday, pGmTime->tm_isdst);
92 plm = new (std::nothrow) _LocaleManagerImpl();
93 SysTryReturnResult(NID_SYS, null != plm, E_SYSTEM, "_LocaleManagerImpl instance must not be null");
95 timeZone = plm->GetSystemTimeZone();
97 String timeZoneId = timeZone.GetId();
98 int rawOffset = timeZone.GetRawOffset();
99 int dstSavings = timeZone.GetDstSavings();
100 SysLog(NID_SYS, "Time zone: %S, %d, %d", timeZoneId.GetPointer(), rawOffset, dstSavings);
103 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);
105 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());
110 currentTime.SetValue(tempTime);
114 currentTime = timeZone.UtcTimeToStandardTime(tempTime);
118 currentTime = timeZone.UtcTimeToWallTime(tempTime);
125 SysLog(NID_SYS, "Current Time %d %d/%d, %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
131 _SystemTimeImpl::GetCurrentTime(DateTime& currentTime)
133 return GetCurrentTime(UTC_TIME, currentTime);
137 _SystemTimeImpl::SetCurrentTime(const DateTime& currentTime)
139 result r = E_SUCCESS;
141 ArrayList requestMessage;
142 ArrayList responseMessage;
143 char datetime[32] = {0,};
145 LocaleManager localeManager;
146 localeManager.Construct();
147 TimeZone timeZone = localeManager.GetSystemTimeZone();
149 DateTime wallTime = timeZone.UtcTimeToWallTime(currentTime);
151 std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
152 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
153 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to create IpcClient", GetErrorMessage(r));
155 requestMessage.Construct();
156 responseMessage.Construct();
158 String serviceId = _SYSTEM_SERVICE_ID;
159 String commandId = _SYSTEM_COMMAND_CHANGE_TIME ;
162 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());
163 dateTime.Append(datetime);
165 requestMessage.Add(serviceId);
166 requestMessage.Add(commandId);
167 requestMessage.Add(dateTime);
169 std::unique_ptr<IoService_Request> pMsg (new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
170 SysTryReturnResult(NID_SYS, pMsg != null, E_SYSTEM, "[E_SYSTEM] fail to create Ipc message");
172 r = pIpcClient->SendRequest(pMsg.get());
173 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to ipc message", GetErrorMessage(r));
175 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
177 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "[E_SYSTEM] fail to receive Ipc response");
178 SysTryReturnResult(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] applciation does not have proper privilege.");
179 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "[E_SYSTEM] fail to set time");
181 vconf_sync_key(VCONFKEY_SYSTEM_TIME_CHANGED);
186 _SystemTimeImpl::GetTicks(long long& ticks)
189 struct timeval timeValue;
191 ret = gettimeofday(&timeValue, null);
192 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the date and Time");
194 ticks = (long long) timeValue.tv_sec * 1000ll;
195 ticks += (long long) timeValue.tv_usec / 1000ll;
201 _SystemTimeImpl::GetNanoTicks(long long& nanoTicks)
204 struct timespec realTime;
206 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &realTime);
208 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get clock information");
210 nanoTicks = (long long) realTime.tv_sec * 1000000000;
211 nanoTicks += (long long) realTime.tv_nsec;
217 _SystemTimeImpl::GetInstance(SystemTime& systemtime)
219 return systemtime.__pSystemTimeImpl;
222 const _SystemTimeImpl*
223 _SystemTimeImpl::GetInstance(const SystemTime& systemtime)
225 return systemtime.__pSystemTimeImpl;