1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
23 #define MAX_VALID_SECONDS (60 * 60 * 24 -1)
24 Time::Time(int hours
, int mins
, int secs
, int usec
)
25 { set(hours
,mins
,secs
, usec
); }
27 int Time::set(int hours
, int mins
, int secs
, int usec
) {
28 if((hours
| mins
| secs
| usec
) < 0)
33 if(hours
>= 24 | mins
>= 60 | secs
>= 62)
38 timeVal
= secs
+ mins
* 60 + hours
* 3600;
40 if(usec
) timeVal
+= usec
/100;
44 int Time::get(int &hours
, int &mins
, int &secs
) const {
45 if (timeVal
< 0) return -1;
46 int s
= timeVal
/10000;
55 int Time::addSec(int secs
)
57 timeVal
+= (secs
*10000);
58 int quit
= timeVal
/864000000;
64 int Time::subSec(int secs
)
66 timeVal
-= (secs
*10000);
67 int quit
= timeVal
/864000000;
73 int Time::addMin(int mins
)
75 timeVal
+= (mins
* 60 * 10000);
76 int quit
= timeVal
/864000000;
82 int Time::subMin(int mins
)
84 timeVal
-= (mins
* 60 * 10000);
85 int quit
= timeVal
/864000000;
91 int Time::addHour(int hours
)
95 timeVal
+= (hours
* 60 * 60 * 10000);
96 int quit
= day
+(timeVal
/864000000);
102 int Time::subHour(int hours
)
104 timeVal
-= (hours
* 60 * 60 * 10000);
105 int quit
= timeVal
/864000000;
108 if(quit
<0) timeVal
+= ( 864000000 * quit
*(-1) );
109 else timeVal
+=864000000;
111 if(0==quit
) return 0;
112 timeVal
%= 864000000;
116 int Time::seconds() const { return (timeVal
/10000) % 60; }
117 int Time::minutes() const { return (timeVal
/(60*10000)) % 60; }
118 int Time::hours() const { return timeVal
/ (3600*10000); }
119 int Time::msec() const { return (timeVal
% 10000) / 10; }
120 int Time::usec() const { return (timeVal
% 10000) * 100; }
122 int Time::setMsec(int ms
) {
123 if(ms
< 0 || ms
>= 1000) return -1;
124 timeVal
= timeVal
+(10*ms
);
128 int Time::setUsec(int us
) {
129 if(us
< 0 || us
>= 1000000) return -1;
130 timeVal
= timeVal
+us
/100;
134 bool Time::isValid() const
135 { return timeVal
>= 0 && timeVal
<= (10000*(MAX_VALID_SECONDS
+1)-1); }
137 bool Time::isValidTime(int hours
, int mins
, int secs
) {
138 return (hours
>= 0 && hours
< 24) &&
139 (mins
>= 0 && mins
< 60) &&
140 (secs
>= 0 && secs
< 60);
143 Time
operator+(const Time
&t1
, int seconds
)
144 { return Time(t1
.timeVal
+ seconds
*10000); }
145 Time
operator+(int seconds
, const Time
&t1
)
146 { return Time(t1
.timeVal
+ seconds
*10000); }
147 Time
operator-(const Time
&t1
, int seconds
)
148 { return Time(t1
.timeVal
- seconds
*10000); }
150 int operator-(const Time
&t1
, const Time
& t2
)
151 { return (t1
.timeVal
- t2
.timeVal
)/10000; }
153 int operator<(const Time
&t1
,const Time
&t2
)
154 { return t1
.timeVal
< t2
.timeVal
; }
155 int operator>(const Time
&t1
,const Time
&t2
)
156 { return t1
.timeVal
> t2
.timeVal
; }
157 int operator<=(const Time
&t1
,const Time
&t2
)
158 { return t1
.timeVal
<= t2
.timeVal
; }
159 int operator>=(const Time
&t1
,const Time
&t2
)
160 { return t1
.timeVal
>= t2
.timeVal
; }
161 int operator==(const Time
&t1
,const Time
&t2
)
162 { return t1
.timeVal
== t2
.timeVal
; }
163 int operator!=(const Time
&t1
,const Time
&t2
)
164 { return t1
.timeVal
!= t2
.timeVal
; }
166 int Time::parseFrom(const char *s
) {
169 if(strcmp(s
,"now")==0 || strcmp(s
,"NOW")==0){
170 time_t cnow
= ::time(NULL
);
171 #if defined(SOLARIS) && defined(REMOTE_SOLARIS)
172 struct std::tm
*tmval
= (struct std::tm
*) localtime(&cnow
);
173 return set(hours
=tmval
->tm_hour
,mins
=tmval
->tm_min
,secs
=tmval
->tm_sec
);
175 struct tm
*tmval
= localtime(&cnow
);
176 return set(hours
=tmval
->tm_hour
,mins
=tmval
->tm_min
,secs
=tmval
->tm_sec
);
180 count
= sscanf(s
,"%d:%d:%d",&hours
,&mins
,&secs
);
181 if (count
< 2) return -1;
182 if (count
== 2) secs
= 0;
184 if (!isValidTime(hours
,mins
,secs
)) return -1;
185 return set(hours
,mins
,secs
);