MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_getsystime.c
1 /* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 /* get time since epoc in 100 nanosec units */
17 /* thus to get the current time we should use the system function
18  with the highest possible resolution */
19 
20 /*
21  TODO: in functions my_micro_time() and my_micro_time_and_time() there
22  exists some common code that should be merged into a function.
23 */
24 
25 #include "mysys_priv.h"
26 #include "my_static.h"
27 
40 ulonglong my_getsystime()
41 {
42 #ifdef HAVE_CLOCK_GETTIME
43  struct timespec tp;
44  clock_gettime(CLOCK_REALTIME, &tp);
45  return (ulonglong)tp.tv_sec*10000000+(ulonglong)tp.tv_nsec/100;
46 #elif defined(_WIN32)
47  LARGE_INTEGER t_cnt;
48  if (query_performance_frequency)
49  {
50  QueryPerformanceCounter(&t_cnt);
51  return ((t_cnt.QuadPart / query_performance_frequency * 10000000) +
52  ((t_cnt.QuadPart % query_performance_frequency) * 10000000 /
53  query_performance_frequency) + query_performance_offset);
54  }
55  return 0;
56 #else
57  /* TODO: check for other possibilities for hi-res timestamping */
58  struct timeval tv;
59  gettimeofday(&tv,NULL);
60  return (ulonglong)tv.tv_sec*10000000+(ulonglong)tv.tv_usec*10;
61 #endif
62 }
63 
64 
73 time_t my_time(myf flags)
74 {
75  time_t t;
76  /* The following loop is here beacuse time() may fail on some systems */
77  while ((t= time(0)) == (time_t) -1)
78  {
79  if (flags & MY_WME)
80  fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
81  }
82  return t;
83 }
84 
85 
86 #define OFFSET_TO_EPOCH 116444736000000000ULL
87 
99 ulonglong my_micro_time()
100 {
101 #ifdef _WIN32
102  ulonglong newtime;
103  GetSystemTimeAsFileTime((FILETIME*)&newtime);
104  newtime-= OFFSET_TO_EPOCH;
105  return (newtime/10);
106 #else
107  ulonglong newtime;
108  struct timeval t;
109  /*
110  The following loop is here because gettimeofday may fail on some systems
111  */
112  while (gettimeofday(&t, NULL) != 0)
113  {}
114  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
115  return newtime;
116 #endif
117 }
118 
119 
137 /* Difference between GetSystemTimeAsFileTime() and now() */
138 
139 ulonglong my_micro_time_and_time(time_t *time_arg)
140 {
141 #ifdef _WIN32
142  ulonglong newtime;
143  GetSystemTimeAsFileTime((FILETIME*)&newtime);
144  *time_arg= (time_t) ((newtime - OFFSET_TO_EPOCH) / 10000000);
145  return (newtime/10);
146 #else
147  ulonglong newtime;
148  struct timeval t;
149  /*
150  The following loop is here because gettimeofday may fail on some systems
151  */
152  while (gettimeofday(&t, NULL) != 0)
153  {}
154  *time_arg= t.tv_sec;
155  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
156  return newtime;
157 #endif
158 }
159 
160 
173 time_t my_time_possible_from_micro(ulonglong microtime __attribute__((unused)))
174 {
175 #ifdef _WIN32
176  time_t t;
177  while ((t= time(0)) == (time_t) -1)
178  {}
179  return t;
180 #else
181  return (time_t) (microtime / 1000000);
182 #endif
183 }
184