CLAM-Development  1.4.0
xtime.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef __XTIME__
23 #define __XTIME__
24 
25 #ifdef WIN32
26 #include "CLAM_windows.h" // for FTIME
27 #undef GetClassName
28 #else
29 #include <sys/time.h> // for GetTimeOfDay
30 #endif
31 #include <pthread.h>
32 
33 namespace CLAM
34 {
35 
36  enum
37  {
46  };
47 
48  struct xtime
49  {
50  unsigned int sec;
51  unsigned int nsec;
52  };
53 
54  inline int xtime_get( xtime* xtp, int clock_type )
55  {
56  if ( clock_type == TIME_UTC )
57  {
58 #ifdef WIN32
59  FILETIME ft;
60  GetSystemTimeAsFileTime(&ft);
61  const unsigned __int64 TIMESPEC_TO_FILETIME_OFFSET = ((unsigned __int64)27111902UL << 32) + (unsigned __int64)3577643008UL;
62  xtp->sec = (int)((*( __int64*)&ft - TIMESPEC_TO_FILETIME_OFFSET) / 10000000);
63  xtp->nsec = (int)((*( __int64*)&ft - TIMESPEC_TO_FILETIME_OFFSET -
64  (( __int64)xtp->sec * ( __int64)10000000)) * 100);
65  return clock_type;
66 #else
67  struct timeval tv;
68  gettimeofday(&tv, 0);
69  xtp->sec = tv.tv_sec;
70  xtp->nsec = tv.tv_usec * 1000;
71  return clock_type;
72 #endif
73  }
74  return clock_type;
75  }
76 
77 
78  const int MILLISECONDS_PER_SECOND = 1000;
79  const int NANOSECONDS_PER_SECOND = 1000000000;
80  const int NANOSECONDS_PER_MILLISECOND = 1000000;
81 
82  const int MICROSECONDS_PER_SECOND = 1000000;
83  const int NANOSECONDS_PER_MICROSECOND = 1000;
84 
85  inline void to_time(int milliseconds, xtime& xt)
86  {
87 
88  xtime_get(&xt, TIME_UTC);
89 
90  xt.sec += (milliseconds / MILLISECONDS_PER_SECOND);
91  xt.nsec += ((milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND);
92 
93  if (xt.nsec > static_cast<const int>(NANOSECONDS_PER_SECOND))
94  {
95  ++xt.sec;
97  }
98 
99 
100 
101  }
102 
103 
104  inline void to_timespec(const xtime& xt, timespec& ts)
105  {
106  ts.tv_sec = static_cast<int>(xt.sec);
107  ts.tv_nsec = static_cast<int>(xt.nsec);
108  if(ts.tv_nsec > static_cast<const int>(NANOSECONDS_PER_SECOND))
109  {
110  ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
111  ts.tv_nsec %= NANOSECONDS_PER_SECOND;
112  }
113  }
114 
115  inline void to_time(int milliseconds, timespec& ts)
116  {
117  xtime xt;
118  to_time(milliseconds, xt);
119  to_timespec(xt, ts);
120 
121 
122 
123  }
124 
125  inline void to_timespec_duration(const xtime& xt, timespec& ts)
126  {
127  xtime cur;
128 
129  xtime_get(&cur, TIME_UTC);
130 
131  if (xt.sec < cur.sec || (xt.sec == cur.sec && xt.nsec < cur.nsec))
132  {
133  ts.tv_sec = 0;
134  ts.tv_nsec = 0;
135  }
136  else
137  {
138  ts.tv_sec = xt.sec - cur.sec;
139  ts.tv_nsec = xt.nsec - cur.nsec;
140 
141  if( ts.tv_nsec < 0 )
142  {
143  ts.tv_sec -= 1;
144  ts.tv_nsec += NANOSECONDS_PER_SECOND;
145  }
146  if(ts.tv_nsec > static_cast<const int>(NANOSECONDS_PER_SECOND))
147  {
148  ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND;
149  ts.tv_nsec %= NANOSECONDS_PER_SECOND;
150  }
151  }
152  }
153 
154 
155 
156 } // end of namespace CLAM
157 
158 #endif // XTime.hxx
159