15 #if defined(__APPLE__) && defined(__MACH__)
16 # include <mach/mach_time.h>
20 #include "ecore_private.h"
24 #ifdef HAVE_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_Group Ecore - Main Loop and Job Functions.
38 * @addtogroup Ecore_Time_Group Ecore Time functions
44 * Retrieves the current system time as a floating point value in seconds.
46 * This uses a monotonic clock and thus never goes back in time while
47 * machine is live (even if user changes time or timezone changes,
48 * however it may be reset whenever the machine is restarted).
50 * @see ecore_loop_time_get().
51 * @see ecore_time_unix_get().
53 * @return The number of seconds. Start time is not defined (it may be
54 * when the machine was booted, unix time, etc), all it is
55 * defined is that it never goes backwards (unless you got big critical
56 * messages when the application started).
61 #ifdef HAVE_CLOCK_GETTIME
64 if (EINA_UNLIKELY(_ecore_time_clock_id < 0))
65 return ecore_time_unix_get();
67 if (EINA_UNLIKELY(clock_gettime(_ecore_time_clock_id, &t)))
69 CRIT("Cannot get current time.");
70 /* Try to at least return the latest value retrieved*/
71 return _ecore_time_loop_time;
74 return (double)t.tv_sec + (((double)t.tv_nsec) / 1000000000.0);
75 #elif defined(HAVE_EVIL)
76 return evil_time_get();
77 #elif defined(__APPLE__) && defined(__MACH__)
78 return _ecore_time_clock_conversion * (double) mach_absolute_time();
80 return ecore_time_unix_get();
85 * Retrieves the current UNIX time as a floating point value in seconds.
87 * @see ecore_time_get().
88 * @see ecore_loop_time_get().
90 * @return The number of seconds since 12.00AM 1st January 1970.
93 ecore_time_unix_get(void)
95 #ifdef HAVE_GETTIMEOFDAY
98 gettimeofday(&timev, NULL);
99 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
101 # error "Your platform isn't supported yet"
106 * Retrieves the time at which the last loop stopped waiting for timeouts or
109 * This gets the time that the main loop ceased waiting for timouts and/or
110 * events to come in or for signals or any other interrupt source. This should
111 * be considered a reference point for all time based activity that should
112 * calculate its timepoint from the return of ecore_loop_time_get(). Use this
113 * UNLESS you absolutely must get the current actual timepoint - then use
114 * ecore_time_get(). Note that this time is meant to be used as relative to
115 * other times obtained on this run. If you need absolute time references, use
116 * ecore_time_unix_get() instead.
118 * This function can be called before any loop has ever been run, but either
119 * ecore_init() or ecore_time_get() must have been called once.
121 * @return The number of seconds. Start time is not defined (it may be
122 * when the machine was booted, unix time, etc), all it is
123 * defined is that it never goes backwards (unless you got big critical
124 * messages when the application started).
127 ecore_loop_time_get(void)
129 return _ecore_time_loop_time;
141 /********************** Internal methods ********************************/
143 /* TODO: Documentation says "All implementations support the system-wide
144 * real-time clock, which is identified by CLOCK_REALTIME. Check if the fallback
145 * to unix time (without specifying the resolution) might be removed
148 _ecore_time_init(void)
150 #ifdef HAVE_CLOCK_GETTIME
153 if (_ecore_time_clock_id != -1) return;
155 if (!clock_gettime(CLOCK_MONOTONIC, &t))
157 _ecore_time_clock_id = CLOCK_MONOTONIC;
158 DBG("using CLOCK_MONOTONIC.");
160 else if (!clock_gettime(CLOCK_REALTIME, &t))
162 /* may go backwards */
163 _ecore_time_clock_id = CLOCK_REALTIME;
164 WRN("CLOCK_MONOTONIC not available. Fallback to CLOCK_REALTIME.");
168 _ecore_time_clock_id = -2;
169 CRIT("Cannot get a valid clock_gettime() clock id! "
170 "Fallback to unix time.");
174 # if defined(__APPLE__) && defined(__MACH__)
175 mach_timebase_info_data_t info;
176 kern_return_t err = mach_timebase_info(&info);
179 _ecore_time_clock_conversion = 1e-9 * (double) info.numer / (double) info.denom;
183 WRN("Unable to get timebase info. Fallback to nanoseconds.");
186 # warning "Your platform isn't supported yet"
187 CRIT("Platform does not support clock_gettime. "
188 "Fallback to unix time.");
193 _ecore_time_loop_time = ecore_time_get();