Try to use the gettimeofday if clock_gettime is failed
[platform/core/appfw/com-core.git] / src / util.c
1 /*
2  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <dlog.h>
25
26 #include "debug.h"
27 #include "util.h"
28
29 int errno;
30 #if defined(_USE_ECORE_TIME_GET)
31 static struct {
32         clockid_t type;
33 } s_info = {
34         .type = CLOCK_MONOTONIC,
35 };
36 #endif
37
38 const char *util_basename(const char *name)
39 {
40         int length;
41         length = name ? strlen(name) : 0;
42         if (!length)
43                 return ".";
44
45         while (--length > 0 && name[length] != '/');
46
47         return length <= 0 ? name : name + length + (name[length] == '/');
48 }
49
50 double util_timestamp(void)
51 {
52 #if defined(_USE_ECORE_TIME_GET)
53         struct timespec ts;
54
55         do {
56                 if (clock_gettime(s_info.type, &ts) == 0) {
57                         return ts.tv_sec + ts.tv_nsec / 1000000000.0f;
58                 }
59
60                 ErrPrint("%d: %s\n", s_info.type, strerror(errno));
61                 if (s_info.type == CLOCK_MONOTONIC) {
62                         s_info.type = CLOCK_REALTIME;
63                 } else if (s_info.type == CLOCK_REALTIME) {
64                         struct timeval tv;
65                         if (gettimeofday(&tv, NULL) < 0) {
66                                 ErrPrint("gettimeofday: %s\n", strerror(errno));
67                                 break;
68                         }
69
70                         return tv.tv_sec + tv.tv_usec / 1000000.0f;
71                 }
72         } while (1);
73
74         return 0.0f;
75 #else
76         struct timeval tv;
77
78         if (gettimeofday(&tv, NULL) < 0) {
79                 ErrPrint("gettimeofday: %s\n", strerror(errno));
80                 tv.tv_sec = 0;
81                 tv.tv_usec = 0;
82         }
83
84         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
85 #endif
86 }
87
88 /* End of a file */