[ecore] merged svn latest code (svn54830)
[profile/ivi/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 #include "Ecore.h"
16 #include "ecore_private.h"
17
18 #include <time.h>
19
20 #ifdef HAVE_CLOCK_GETTIME
21 static clockid_t _ecore_time_clock_id = -1;
22 #endif
23 double _ecore_time_loop_time = -1.0;
24
25 /**
26  * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
27  *
28  * @{
29  */
30
31 /**
32  * @addtogroup Ecore_Time_Group Ecore Time functions
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 #ifdef HAVE_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 #else
70 # ifdef HAVE_EVIL
71    return evil_time_get();
72 # else
73    return ecore_time_unix_get();
74 # endif
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 /**
131  * @}
132  */
133
134
135 /**********************   Internal methods   ********************************/
136
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
140  */
141 void
142 _ecore_time_init(void)
143 {
144 #ifdef HAVE_CLOCK_GETTIME
145    struct timespec t;
146
147    if (_ecore_time_clock_id != -1) return;
148
149    if (!clock_gettime(CLOCK_MONOTONIC, &t))
150      {
151         _ecore_time_clock_id = CLOCK_MONOTONIC;
152         DBG("using CLOCK_MONOTONIC.");
153      }
154    else if (!clock_gettime(CLOCK_REALTIME, &t))
155      {
156         /* may go backwards */
157         _ecore_time_clock_id = CLOCK_REALTIME;
158         WRN("CLOCK_MONOTONIC not available. Fallback to CLOCK_REALTIME.");
159      }
160    else
161      {
162         _ecore_time_clock_id = -2;
163         CRIT("Cannot get a valid clock_gettime() clock id! "
164              "Fallback to unix time.");
165      }
166 #else
167 # ifndef HAVE_EVIL
168 #  warning "Your platform isn't supported yet"
169    CRIT("Platform does not support clock_gettime. "
170         "Fallback to unix time.");
171 # endif
172 #endif
173
174    _ecore_time_loop_time = ecore_time_get();
175 }