]> git.stg.codes - stg.git/blob - include/utime.h
Добавление исходников
[stg.git] / include / 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     //cout << tv_sec << "." << tv_usec << "  " << rhs.tv_sec << "." <<  rhs.tv_usec << endl;
112     //cout << (tv_sec == rhs.tv_sec) << " " << (tv_usec == rhs.tv_usec) << endl;
113     return (tv_sec == rhs.tv_sec) && (tv_usec == rhs.tv_usec);
114     }
115
116     UTIME operator+(const UTIME & rhs)
117     {
118     // TODO optimize
119     long long a, b;
120     /*a = tv_sec * 1000000 + tv_usec;
121     b = rhs.tv_sec * 1000000 + rhs.tv_usec;
122     return UTIME((a + b) / 1000000, (a + b) % 1000000);*/
123     a = tv_sec + rhs.tv_sec;
124     b = tv_usec + rhs.tv_usec;
125     if (b > 1000000)
126         {
127         ++a;
128         b -= 1000000;
129         }
130     return UTIME(a, b);
131     }
132
133     UTIME operator-(const UTIME & rhs)
134     {
135     // TODO optimize
136     long long a, b;
137     /*a = tv_sec * 1000000 + tv_usec;
138     b = rhs.tv_sec * 1000000 + rhs.tv_usec;
139     return UTIME((a - b) / 1000000, (a - b) % 1000000);*/
140     a = tv_sec - rhs.tv_sec;
141     b = tv_usec - rhs.tv_usec;
142     if (a >= 0)
143         {
144         if (b >= 0)
145             {
146             return UTIME(a, b);
147             }
148         else
149             {
150             return UTIME(--a, b + 1000000);
151             }
152         }
153     else
154         {
155         if (b >= 0)
156             {
157             return UTIME(++a, 1000000 - b);
158             }
159         else
160             {
161             return UTIME(a, b);
162             }
163         }
164     }
165
166     time_t GetSec() const
167     {
168     return tv_sec;
169     }
170
171     suseconds_t GetUSec() const
172     {
173     return tv_usec;
174     }
175 };
176
177
178 #endif //UTIME_H
179