15 #if defined(__APPLE__) && defined(__MACH__)
16 # include <mach/mach_time.h>
20 #include "ecore_private.h"
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;
29 double _ecore_time_loop_time = -1.0;
32 * @addtogroup Ecore_Time_Group
38 * Retrieves the current system time as a floating point value in seconds.
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).
44 * @see ecore_loop_time_get().
45 * @see ecore_time_unix_get().
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).
55 #if defined (HAVE_CLOCK_GETTIME) || defined (EXOTIC_PROVIDE_CLOCK_GETTIME)
58 if (EINA_UNLIKELY(_ecore_time_clock_id < 0))
59 return ecore_time_unix_get();
61 if (EINA_UNLIKELY(clock_gettime(_ecore_time_clock_id, &t)))
63 CRIT("Cannot get current time.");
64 /* Try to at least return the latest value retrieved*/
65 return _ecore_time_loop_time;
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();
74 return ecore_time_unix_get();
79 * Retrieves the current UNIX time as a floating point value in seconds.
81 * @see ecore_time_get().
82 * @see ecore_loop_time_get().
84 * @return The number of seconds since 12.00AM 1st January 1970.
87 ecore_time_unix_get(void)
89 #ifdef HAVE_GETTIMEOFDAY
92 gettimeofday(&timev, NULL);
93 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
95 # error "Your platform isn't supported yet"
100 * Retrieves the time at which the last loop stopped waiting for timeouts or
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.
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.
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).
121 ecore_loop_time_get(void)
123 return _ecore_time_loop_time;
130 /********************** Internal methods ********************************/
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
137 _ecore_time_init(void)
139 #if defined (HAVE_CLOCK_GETTIME) || defined (EXOTIC_PROVIDE_CLOCK_GETTIME)
142 if (_ecore_time_clock_id != -1) return;
144 if (!clock_gettime(CLOCK_MONOTONIC, &t))
146 _ecore_time_clock_id = CLOCK_MONOTONIC;
147 DBG("using CLOCK_MONOTONIC.");
149 else if (!clock_gettime(CLOCK_REALTIME, &t))
151 /* may go backwards */
152 _ecore_time_clock_id = CLOCK_REALTIME;
153 WRN("CLOCK_MONOTONIC not available. Fallback to CLOCK_REALTIME.");
157 _ecore_time_clock_id = -2;
158 CRIT("Cannot get a valid clock_gettime() clock id! "
159 "Fallback to unix time.");
163 # if defined(__APPLE__) && defined(__MACH__)
164 mach_timebase_info_data_t info;
165 kern_return_t err = mach_timebase_info(&info);
168 _ecore_time_clock_conversion = 1e-9 * (double)info.numer / (double)info.denom;
172 WRN("Unable to get timebase info. Fallback to nanoseconds.");
175 # warning "Your platform isn't supported yet"
176 CRIT("Platform does not support clock_gettime. "
177 "Fallback to unix time.");
182 _ecore_time_loop_time = ecore_time_get();