Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore / ecore_time.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6
7 #ifdef HAVE_SYS_TIME_H
8 # include <sys/time.h>
9 #endif
10
11 #ifdef HAVE_EVIL
12 # include <Evil.h>
13 #endif
14
15 #if defined(__APPLE__) && defined(__MACH__)
16 # include <mach/mach_time.h>
17 #endif
18
19 #include "Ecore.h"
20 #include "ecore_private.h"
21
22 #include <time.h>
23
24 #if defined (HAVE_CLOCK_GETTIME) || defined (EXOTIC_PROVIDE_CLOCK_GETTIME)
25 static clockid_t _ecore_time_clock_id = -1;
26 #elif defined(__APPLE__) && defined(__MACH__)
27 static double _ecore_time_clock_conversion = 1e-9;
28 #endif
29 double _ecore_time_loop_time = -1.0;
30
31 /**
32  * @addtogroup Ecore_Time_Group
33  *
34  * @{
35  */
36
37 /**
38  * Retrieves the current system time as a floating point value in seconds.
39  *
40  * This uses a monotonic clock and thus never goes back in time while
41  * machine is live (even if user changes time or timezone changes,
42  * however it may be reset whenever the machine is restarted).
43  *
44  * @see ecore_loop_time_get().
45  * @see ecore_time_unix_get().
46  *
47  * @return The number of seconds. Start time is not defined (it may be
48  *         when the machine was booted, unix time, etc), all it is
49  *         defined is that it never goes backwards (unless you got big critical
50  *         messages when the application started).
51  */
52 EAPI double
53 ecore_time_get(void)
54 {
55 #if defined (HAVE_CLOCK_GETTIME) || defined (EXOTIC_PROVIDE_CLOCK_GETTIME) 
56    struct timespec t;
57
58    if (EINA_UNLIKELY(_ecore_time_clock_id < 0))
59      return ecore_time_unix_get();
60
61    if (EINA_UNLIKELY(clock_gettime(_ecore_time_clock_id, &t)))
62      {
63         CRIT("Cannot get current time.");
64         /* Try to at least return the latest value retrieved*/
65         return _ecore_time_loop_time;
66      }
67
68    return (double)t.tv_sec + (((double)t.tv_nsec) / 1000000000.0);
69 #elif defined(HAVE_EVIL)
70    return evil_time_get();
71 #elif defined(__APPLE__) && defined(__MACH__)
72    return _ecore_time_clock_conversion * (double)mach_absolute_time();
73 #else
74    return ecore_time_unix_get();
75 #endif
76 }
77
78 /**
79  * Retrieves the current UNIX time as a floating point value in seconds.
80  *
81  * @see ecore_time_get().
82  * @see ecore_loop_time_get().
83  *
84  * @return  The number of seconds since 12.00AM 1st January 1970.
85  */
86 EAPI double
87 ecore_time_unix_get(void)
88 {
89 #ifdef HAVE_GETTIMEOFDAY
90    struct timeval timev;
91
92    gettimeofday(&timev, NULL);
93    return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
94 #else
95 # error "Your platform isn't supported yet"
96 #endif
97 }
98
99 /**
100  * Retrieves the time at which the last loop stopped waiting for timeouts or
101  * events.
102  *
103  * This gets the time that the main loop ceased waiting for timouts and/or
104  * events to come in or for signals or any other interrupt source. This should
105  * be considered a reference point for all time based activity that should
106  * calculate its timepoint from the return of ecore_loop_time_get(). Use this
107  * UNLESS you absolutely must get the current actual timepoint - then use
108  * ecore_time_get(). Note that this time is meant to be used as relative to
109  * other times obtained on this run. If you need absolute time references, use
110  * ecore_time_unix_get() instead.
111  *
112  * This function can be called before any loop has ever been run, but either
113  * ecore_init() or ecore_time_get() must have been called once.
114  *
115  * @return The number of seconds. Start time is not defined (it may be
116  *         when the machine was booted, unix time, etc), all it is
117  *         defined is that it never goes backwards (unless you got big critical
118  *         messages when the application started).
119  */
120 EAPI double
121 ecore_loop_time_get(void)
122 {
123    return _ecore_time_loop_time;
124 }
125
126 /**
127  * @}
128  */
129
130 /**********************   Internal methods   ********************************/
131
132 /* TODO: Documentation says "All  implementations  support  the  system-wide
133  * real-time clock, which is identified by CLOCK_REALTIME. Check if the fallback
134  * to unix time (without specifying the resolution) might be removed
135  */
136 void
137 _ecore_time_init(void)
138 {
139 #if defined (HAVE_CLOCK_GETTIME) || defined (EXOTIC_PROVIDE_CLOCK_GETTIME)
140    struct timespec t;
141
142    if (_ecore_time_clock_id != -1) return;
143
144    if (!clock_gettime(CLOCK_MONOTONIC, &t))
145      {
146         _ecore_time_clock_id = CLOCK_MONOTONIC;
147         DBG("using CLOCK_MONOTONIC.");
148      }
149    else if (!clock_gettime(CLOCK_REALTIME, &t))
150      {
151         /* may go backwards */
152          _ecore_time_clock_id = CLOCK_REALTIME;
153          WRN("CLOCK_MONOTONIC not available. Fallback to CLOCK_REALTIME.");
154      }
155    else
156      {
157         _ecore_time_clock_id = -2;
158         CRIT("Cannot get a valid clock_gettime() clock id! "
159              "Fallback to unix time.");
160      }
161 #else
162 # ifndef HAVE_EVIL
163 #  if defined(__APPLE__) && defined(__MACH__)
164    mach_timebase_info_data_t info;
165    kern_return_t err = mach_timebase_info(&info);
166    if (err == 0)
167      {
168         _ecore_time_clock_conversion = 1e-9 * (double)info.numer / (double)info.denom;
169      }
170    else
171      {
172         WRN("Unable to get timebase info. Fallback to nanoseconds.");
173      }
174 #  else
175 #  warning "Your platform isn't supported yet"
176    CRIT("Platform does not support clock_gettime. "
177         "Fallback to unix time.");
178 #  endif
179 # endif
180 #endif
181
182    _ecore_time_loop_time = ecore_time_get();
183 }
184