add command manager module
[apps/native/tizen-things-daemon.git] / common / common-util.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <time.h>
18
19 #ifndef CLOCK_REALTIME_COARSE
20 #define CLOCK_REALTIME_COARSE CLOCK_REALTIME
21 #endif
22
23 #ifndef CLOCK_MONOTONIC_COARSE
24 #define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
25 #endif
26
27 long long common_get_monotonic_time(void)
28 {
29         struct timespec ts;
30         int result;
31
32         result = clock_gettime(CLOCK_MONOTONIC, &ts);
33
34         if (result != 0)
35                 return 0;
36
37         return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
38 }
39
40 long long common_get_monotonic_coarse_time(void)
41 {
42         struct timespec ts;
43         int result;
44
45         result = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
46
47         if (result != 0)
48                 return 0;
49
50         return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
51 }
52
53 long long common_get_epoch_time(void)
54 {
55         long long int ret_time = 0;
56         struct timespec time_s;
57
58         if (0 == clock_gettime(CLOCK_REALTIME, &time_s))
59                 ret_time = time_s.tv_sec;
60
61         return ret_time;
62 }
63
64 long long common_get_epoch_coarse_time(void)
65 {
66         long long int ret_time = 0;
67         struct timespec time_s;
68
69         if (0 == clock_gettime(CLOCK_REALTIME_COARSE, &time_s))
70                 ret_time = time_s.tv_sec;
71
72         return ret_time;
73 }
74
75 int common_get_epoch_timespec(struct timespec *tp)
76 {
77         if (!tp)
78                 return -1;
79
80         return clock_gettime(CLOCK_REALTIME, tp);
81 }
82
83 int common_get_epoch_coarse_timespec(struct timespec *tp)
84 {
85         if (!tp)
86                 return -1;
87
88         return clock_gettime(CLOCK_REALTIME_COARSE, tp);
89 }