1 /*! ========================================================================
2 ** Extended Template and Library
3 ** gettimeofday() Clock Description Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__CLOCK_GETTIMEOFDAY_H
28 #define __ETL__CLOCK_GETTIMEOFDAY_H
30 /* === H E A D E R S ======================================================= */
35 /* === M A C R O S ========================================================= */
37 /* === T Y P E D E F S ===================================================== */
39 /* === C L A S S E S & S T R U C T S ======================================= */
43 class clock_desc_gettimeofday
46 typedef double value_type;
48 inline static bool realtime()
51 inline static bool proctime()
54 inline static value_type
58 inline static value_type precision()
59 { return one_second()/(value_type)1000000.0f; }
61 inline static const char *description()
62 { return "UNIX gettimeofday()"; };
65 class timestamp : public timeval
67 timestamp(int sec, int usec)
68 { tv_sec=sec; tv_usec=usec; }
70 friend class clock_desc_gettimeofday;
75 inline timestamp operator-(const timestamp &rhs)const
78 ret.tv_usec=tv_usec-rhs.tv_usec;
82 ret.tv_sec=tv_sec-rhs.tv_sec-1;
86 ret.tv_sec=tv_sec-rhs.tv_sec;
90 inline timestamp operator+(timestamp rhs)const
94 if(rhs.tv_usec>1000000)
104 inline bool operator<(const timestamp &rhs)const
105 { return tv_sec<rhs.tv_sec || tv_usec<rhs.tv_usec; }
107 inline bool operator==(const timestamp &rhs)const
108 { return tv_usec==rhs.tv_usec && tv_sec==rhs.tv_sec; }
110 inline bool operator!=(const timestamp &rhs)const
111 { return tv_usec!=rhs.tv_usec || tv_sec!=rhs.tv_sec; }
115 get_current_time(timestamp &x)
116 { gettimeofday(&x,NULL);}
120 { timestamp ret; get_current_time(ret); return ret; }
123 timestamp_to_seconds(const timestamp &x)
124 { return (value_type)x.tv_sec + precision()*x.tv_usec; }
127 seconds_to_timestamp(const value_type &x)
128 { return timestamp((int)floor(x), (int)((x-floor(x))/precision()+0.5)); }
133 /* === E N D =============================================================== */