16 #include "ecore_private.h"
20 #ifdef HAVE_CLOCK_GETTIME
21 static clockid_t _ecore_time_clock_id = -1;
23 double _ecore_time_loop_time = -1.0;
26 * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
32 * @addtogroup Ecore_Time_Group Ecore Time functions
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 #ifdef HAVE_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);
71 return evil_time_get();
73 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;
135 /********************** Internal methods ********************************/
137 /* TODO: Documentation says "All implementations support the system-wide
138 * real-time clock, which is identified by CLOCK_REALTIME. Check if the fallback
139 * to unix time (without specifying the resolution) might be removed
142 _ecore_time_init(void)
144 #ifdef HAVE_CLOCK_GETTIME
147 if (_ecore_time_clock_id != -1) return;
149 if (!clock_gettime(CLOCK_MONOTONIC, &t))
151 _ecore_time_clock_id = CLOCK_MONOTONIC;
152 DBG("using CLOCK_MONOTONIC.");
154 else if (!clock_gettime(CLOCK_REALTIME, &t))
156 /* may go backwards */
157 _ecore_time_clock_id = CLOCK_REALTIME;
158 WRN("CLOCK_MONOTONIC not available. Fallback to CLOCK_REALTIME.");
162 _ecore_time_clock_id = -2;
163 CRIT("Cannot get a valid clock_gettime() clock id! "
164 "Fallback to unix time.");
168 # warning "Your platform isn't supported yet"
169 CRIT("Platform does not support clock_gettime. "
170 "Fallback to unix time.");
174 _ecore_time_loop_time = ecore_time_get();