Add log on time function
[platform/framework/native/appfw.git] / src / system / FSys_SystemTimeImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSys_SystemTimeImpl.cpp
19  * @brief               This is the implementation file for _SystemTimeImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <new>
24 #include <sys/sysinfo.h>
25 #include <sys/time.h>
26 #include <time.h>
27 #include <vconf.h>
28
29 #include <FBaseDateTime.h>
30 #include <FBaseResult.h>
31 #include <FBaseColArrayList.h>
32 #include <FBaseSysLog.h>
33
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>
39
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
43 using namespace Tizen::Locales;
44
45 namespace Tizen { namespace System
46 {
47
48 _SystemTimeImpl::_SystemTimeImpl(void)
49 {
50 }
51
52 _SystemTimeImpl::~_SystemTimeImpl(void)
53 {
54 }
55
56 result
57 _SystemTimeImpl::GetUptime(TimeSpan& uptime)
58 {
59         int ret = 0;
60         long long ticks = 0;
61         struct timespec timeSpec;
62
63         ret = clock_gettime(CLOCK_MONOTONIC_RAW, &timeSpec);
64         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the time ");
65
66         ticks = (long long) timeSpec.tv_sec * 1000ll;
67         ticks += (long long) timeSpec.tv_nsec / 1000000ll;
68         uptime = TimeSpan(ticks);
69
70         return E_SUCCESS;
71 }
72
73 result
74 _SystemTimeImpl::GetCurrentTime(TimeMode timeMode, DateTime& currentTime)
75 {
76         struct tm* pGmTime = null;
77         time_t currTime = 0;
78         TimeZone timeZone;
79         DateTime tempTime;
80         struct timeval currentTimeVal;
81
82         _LocaleManagerImpl* plm = null;
83
84         time(&currTime);
85         SysLog(NID_SYS, "Current Epoch is %lld.", currTime);
86         gettimeofday(&currentTimeVal, null);
87
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);
91
92         plm = new (std::nothrow) _LocaleManagerImpl();
93         SysTryReturnResult(NID_SYS, null != plm, E_SYSTEM, "_LocaleManagerImpl instance must not be null");
94
95         timeZone = plm->GetSystemTimeZone();
96         delete plm;
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);
101
102
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);
104
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());
106
107         switch (timeMode)
108         {
109         case UTC_TIME:
110                 currentTime.SetValue(tempTime);
111                 break;
112
113         case STANDARD_TIME:
114                 currentTime = timeZone.UtcTimeToStandardTime(tempTime);
115                 break;
116
117         case WALL_TIME:
118                 currentTime = timeZone.UtcTimeToWallTime(tempTime);
119                 break;
120
121         default:
122                 break;
123         }
124
125         SysLog(NID_SYS, "Current Time %d %d/%d, %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
126
127         return E_SUCCESS;
128 }
129
130 result
131 _SystemTimeImpl::GetCurrentTime(DateTime& currentTime)
132 {
133         return GetCurrentTime(UTC_TIME, currentTime);
134 }
135
136 result
137 _SystemTimeImpl::SetCurrentTime(const DateTime& currentTime)
138 {
139         result r = E_SUCCESS;
140
141         ArrayList requestMessage;
142         ArrayList responseMessage;
143         char datetime[32] = {0,};
144
145         LocaleManager localeManager;
146         localeManager.Construct();
147         TimeZone timeZone = localeManager.GetSystemTimeZone();
148
149         DateTime wallTime = timeZone.UtcTimeToWallTime(currentTime);
150
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));
154
155         requestMessage.Construct();
156         responseMessage.Construct();
157
158         String serviceId = _SYSTEM_SERVICE_ID;
159         String commandId = _SYSTEM_COMMAND_CHANGE_TIME ;
160         String dateTime;
161
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);
164
165         requestMessage.Add(serviceId);
166         requestMessage.Add(commandId);
167         requestMessage.Add(dateTime);
168
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");
171
172         r = pIpcClient->SendRequest(pMsg.get());
173         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to ipc message", GetErrorMessage(r));
174
175         String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
176
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");
180
181         vconf_sync_key(VCONFKEY_SYSTEM_TIME_CHANGED);
182         
183         return r;
184 }
185 result
186 _SystemTimeImpl::GetTicks(long long& ticks)
187 {
188         int ret = 0;
189         struct timeval timeValue;
190
191         ret = gettimeofday(&timeValue, null);
192         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the date and Time");
193
194         ticks = (long long) timeValue.tv_sec * 1000ll;
195         ticks += (long long) timeValue.tv_usec / 1000ll;
196
197         return E_SUCCESS;
198 }
199
200 result
201 _SystemTimeImpl::GetNanoTicks(long long& nanoTicks)
202 {
203         int ret = 0;
204         struct timespec realTime;
205
206         ret = clock_gettime(CLOCK_MONOTONIC_RAW, &realTime);
207
208         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get clock information");
209
210         nanoTicks = (long long) realTime.tv_sec * 1000000000;
211         nanoTicks += (long long) realTime.tv_nsec;
212
213         return E_SUCCESS;
214 }
215
216 _SystemTimeImpl*
217 _SystemTimeImpl::GetInstance(SystemTime& systemtime)
218 {
219         return systemtime.__pSystemTimeImpl;
220 }
221
222 const _SystemTimeImpl*
223 _SystemTimeImpl::GetInstance(const SystemTime& systemtime)
224 {
225         return systemtime.__pSystemTimeImpl;
226 }
227
228
229 } } // Tizen::System