Merge "Use existing callback ID for recurring callbacks" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / time-service.cpp
1 /*
2  * Copyright (c) 2015 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 // HEADER
19 #include <dali/internal/system/common/time-service.h>
20
21 // EXTERNAL INCLUDES
22 #include <chrono>
23 #include <thread>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 namespace TimeService
35 {
36
37 void GetNanoseconds( uint64_t& timeInNanoseconds )
38 {
39   // Get the time of a monotonic clock since its epoch.
40   auto epoch = std::chrono::steady_clock::now().time_since_epoch();
41
42   auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
43
44   timeInNanoseconds = static_cast<uint64_t>(duration.count());
45 }
46
47 uint32_t GetMilliSeconds()
48 {
49   // Get the time of a monotonic clock since its epoch.
50   auto epoch = std::chrono::steady_clock::now().time_since_epoch();
51
52   auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
53
54   return static_cast<uint32_t>(duration.count());
55 }
56
57
58 void SleepUntil( uint64_t timeInNanoseconds )
59 {
60   using Clock = std::chrono::steady_clock;
61   using TimePoint = std::chrono::time_point<Clock>;
62
63   const Clock::duration duration = std::chrono::nanoseconds(timeInNanoseconds);
64   const TimePoint timePoint(duration);
65
66   std::this_thread::sleep_until(timePoint);
67 }
68
69 } // namespace TimeService
70
71 } // namespace Adaptor
72
73 } // namespace Internal
74
75 } // namespace Dali