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