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)
77 struct tm* pGmTime = null;
80 struct timeval currentTimeVal;
82 _LocaleManagerImpl* plm = null;
84 gettimeofday(¤tTimeVal, null);
85 SysLog(NID_SYS, "Current Epoch is %lld.", currentTimeVal.tv_sec);
87 pGmTime = gmtime_r(¤tTimeVal.tv_sec, &gmTime);
88 SysTryReturnResult(NID_SYS, null != pGmTime, E_SYSTEM, "It is failed to convert the time value to UTC time");
89 SysLog(NID_SYS, "Calendar time is sec:%d, min:%d, hour:%d, day:%d, mon:%d, year:%d, wday%d, year:%d, dst:%d", gmTime.tm_sec, gmTime.tm_min, gmTime.tm_hour, gmTime.tm_mday, gmTime.tm_mon, gmTime.tm_year, gmTime.tm_wday, gmTime.tm_yday, gmTime.tm_isdst);
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, "Current time zone is %S, %d, %d", timeZoneId.GetPointer(), rawOffset, dstSavings);
101 tempTime.SetValue(gmTime.tm_year + 1900, gmTime.tm_mon + 1, gmTime.tm_mday, gmTime.tm_hour, gmTime.tm_min, gmTime.tm_sec, currentTimeVal.tv_usec / 1000);
103 SysLog(NID_SYS, "Original Time is %d %d/%d, %d:%d:%d:%d", tempTime.GetYear(), tempTime.GetMonth(), tempTime.GetDay(), tempTime.GetHour(), tempTime.GetMinute(), tempTime.GetSecond(), tempTime.GetMillisecond());
108 currentTime.SetValue(tempTime);
112 currentTime = timeZone.UtcTimeToStandardTime(tempTime);
116 currentTime = timeZone.UtcTimeToWallTime(tempTime);
123 SysLog(NID_SYS, "Current Time is %d %d/%d, %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
129 _SystemTimeImpl::GetCurrentTime(DateTime& currentTime)
131 return GetCurrentTime(UTC_TIME, currentTime);
135 _SystemTimeImpl::SetCurrentTime(const DateTime& currentTime)
137 result r = E_SUCCESS;
139 ArrayList requestMessage;
140 ArrayList responseMessage;
141 char datetime[64] = {0,};
143 LocaleManager localeManager;
144 localeManager.Construct();
145 TimeZone timeZone = localeManager.GetSystemTimeZone();
147 DateTime wallTime = timeZone.UtcTimeToWallTime(currentTime);
149 std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
150 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
151 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to create IpcClient", GetErrorMessage(r));
153 requestMessage.Construct();
154 responseMessage.Construct();
156 String serviceId = _SYSTEM_SERVICE_ID;
157 String commandId = _SYSTEM_COMMAND_CHANGE_TIME ;
160 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());
161 dateTime.Append(datetime);
163 requestMessage.Add(serviceId);
164 requestMessage.Add(commandId);
165 requestMessage.Add(dateTime);
167 std::unique_ptr<IoService_Request> pMsg (new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
168 SysTryReturnResult(NID_SYS, pMsg != null, E_SYSTEM, "[E_SYSTEM] fail to create Ipc message");
170 r = pIpcClient->SendRequest(pMsg.get());
171 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to ipc message", GetErrorMessage(r));
173 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
175 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "[E_SYSTEM] fail to receive Ipc response");
176 SysTryReturnResult(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] applciation does not have proper privilege.");
177 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "[E_SYSTEM] fail to set time");
179 vconf_sync_key(VCONFKEY_SYSTEM_TIME_CHANGED);
184 _SystemTimeImpl::GetTicks(long long& ticks)
187 struct timeval timeValue;
189 ret = gettimeofday(&timeValue, null);
190 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the date and Time");
192 ticks = (long long) timeValue.tv_sec * 1000ll;
193 ticks += (long long) timeValue.tv_usec / 1000ll;
199 _SystemTimeImpl::GetNanoTicks(long long& nanoTicks)
202 struct timespec realTime;
204 ret = clock_gettime(CLOCK_MONOTONIC_RAW, &realTime);
206 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get clock information");
208 nanoTicks = (long long) realTime.tv_sec * 1000000000;
209 nanoTicks += (long long) realTime.tv_nsec;
215 _SystemTimeImpl::GetInstance(SystemTime& systemtime)
217 return systemtime.__pSystemTimeImpl;
220 const _SystemTimeImpl*
221 _SystemTimeImpl::GetInstance(const SystemTime& systemtime)
223 return systemtime.__pSystemTimeImpl;