]> git.stg.codes - stg.git/blob - include/stg/utime.h
fe6b3f69d9601d2f737404f78e0bfc31e24196a3
[stg.git] / include / stg / utime.h
1 #ifndef UTIME_H
2 #define UTIME_H
3
4 /*
5  *    This program is free software; you can redistribute it and/or modify
6  *    it under the terms of the GNU General Public License as published by
7  *    the Free Software Foundation; either version 2 of the License, or
8  *    (at your option) any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 /*
21  *    Date: 22.12.2007
22  */
23
24 /*
25  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
26  */
27
28  /*
29  $Revision: 1.6 $
30  $Date: 2009/08/05 11:40:30 $
31  $Author: faust $
32  */
33
34 #include <sys/time.h>
35 #include <time.h>
36
37 #ifdef FREE_BSD
38 typedef long suseconds_t;
39 #endif
40
41 struct UTIME: public timeval
42 {
43     UTIME()
44     {
45     tv_sec = 0;
46     tv_usec = 0;
47     }
48
49     UTIME(time_t t)
50     {
51     tv_sec = t;
52     tv_usec = 0;
53     }
54
55     UTIME(long long a, long long b)
56     {
57     tv_sec = a;
58     tv_usec = b;
59     }
60
61     bool operator<(const UTIME & rhs) const
62     {
63     if (tv_sec < rhs.tv_sec)
64         return true;
65     else if (tv_sec > rhs.tv_sec)
66         return false;
67     else if (tv_usec < rhs.tv_usec)
68         return true;
69     return false;
70     }
71
72     bool operator<=(const UTIME & rhs) const
73     {
74     if (tv_sec < rhs.tv_sec)
75         return true;
76     else if (tv_sec > rhs.tv_sec)
77         return false;
78     else if (tv_usec < rhs.tv_usec)
79         return true;
80     else if (tv_usec > rhs.tv_usec)
81         return false;
82     return true;
83     }
84
85     bool operator>(const UTIME & rhs) const
86     {
87     if (tv_sec > rhs.tv_sec)
88         return true;
89     else if (tv_sec < rhs.tv_sec)
90         return false;
91     else if (tv_usec > rhs.tv_usec)
92         return true;
93     return false;
94     }
95
96     bool operator>=(const UTIME & rhs) const
97     {
98     if (tv_sec > rhs.tv_sec)
99         return true;
100     else if (tv_sec < rhs.tv_sec)
101         return false;
102     else if (tv_usec > rhs.tv_usec)
103         return true;
104     else if (tv_usec < rhs.tv_usec)
105         return false;
106     return true;
107     }
108
109     bool operator==(const UTIME & rhs) const
110     {
111     return (tv_sec == rhs.tv_sec) && (tv_usec == rhs.tv_usec);
112     }
113
114     UTIME operator+(const UTIME & rhs)
115     {
116     // TODO optimize
117     long long a, b;
118     a = tv_sec + rhs.tv_sec;
119     b = tv_usec + rhs.tv_usec;
120     if (b > 1000000)
121         {
122         ++a;
123         b -= 1000000;
124         }
125     return UTIME(a, b);
126     }
127
128     UTIME operator-(const UTIME & rhs)
129     {
130     // TODO optimize
131     long long a, b;
132     a = tv_sec - rhs.tv_sec;
133     b = tv_usec - rhs.tv_usec;
134     if (a >= 0)
135         {
136         if (b >= 0)
137             {
138             return UTIME(a, b);
139             }
140         else
141             {
142             return UTIME(--a, b + 1000000);
143             }
144         }
145     else
146         {
147         if (b >= 0)
148             {
149             return UTIME(++a, 1000000 - b);
150             }
151         else
152             {
153             return UTIME(a, b);
154             }
155         }
156     }
157
158     time_t GetSec() const
159     {
160     return tv_sec;
161     }
162
163     suseconds_t GetUSec() const
164     {
165     return tv_usec;
166     }
167
168     double AsDouble() const
169     {
170     return tv_sec + tv_usec * 1e-6;
171     }
172 };
173
174
175 #endif //UTIME_H