[dali_2.3.27] Merge branch '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 namespace Internal
28 {
29 namespace Adaptor
30 {
31 namespace TimeService
32 {
33 void GetNanoseconds(uint64_t& timeInNanoseconds)
34 {
35   // Get the time of a monotonic clock since its epoch.
36   auto epoch = std::chrono::steady_clock::now().time_since_epoch();
37
38   auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
39
40   timeInNanoseconds = static_cast<uint64_t>(duration.count());
41 }
42
43 uint32_t GetMilliSeconds()
44 {
45   // Get the time of a monotonic clock since its epoch.
46   auto epoch = std::chrono::steady_clock::now().time_since_epoch();
47
48   auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
49
50   return static_cast<uint32_t>(duration.count());
51 }
52
53 void SleepUntil(uint64_t timeInNanoseconds)
54 {
55   using Clock     = std::chrono::steady_clock;
56   using TimePoint = std::chrono::time_point<Clock>;
57
58   const Clock::duration duration = std::chrono::nanoseconds(timeInNanoseconds);
59   const TimePoint       timePoint(duration);
60
61   std::this_thread::sleep_until(timePoint);
62 }
63
64 } // namespace TimeService
65
66 } // namespace Adaptor
67
68 } // namespace Internal
69
70 } // namespace Dali