code reorg and moving files to csql base directory
[csql.git] / src / base / Time.cxx
blob93438001d036ba239a807765e24b0a01776cf1eb
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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 ***************************************************************************/
16 #include<os.h>
17 #include<DataType.h>
18 #if defined(SOLARIS)
19 #undef _TIME_H
20 #endif
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)
30 timeVal = -1;
31 return -1;
33 if(hours >= 24 | mins >= 60 | secs >= 62)
35 timeVal = -1;
36 return -1;
38 timeVal = secs + mins * 60 + hours * 3600;
39 timeVal *= 10000;
40 if(usec) timeVal += usec/100;
41 return 0;
44 int Time::get(int &hours, int &mins, int &secs) const {
45 if (timeVal < 0) return -1;
46 int s = timeVal/10000;
47 secs = s % 60;
48 s /= 60;
49 mins = s % 60;
50 s /= 60;
51 hours = s;
52 return 0;
55 int Time::addSec(int secs)
57 timeVal += (secs *10000);
58 int quit = timeVal /864000000;
59 if(0==quit) return 0;
60 timeVal %= 864000000;
61 return quit;
64 int Time::subSec(int secs)
66 timeVal -= (secs *10000);
67 int quit = timeVal /864000000;
68 if(0==quit) return 0;
69 timeVal %= 864000000;
70 return quit;
73 int Time::addMin(int mins)
75 timeVal += (mins * 60 * 10000);
76 int quit = timeVal /864000000;
77 if(0==quit) return 0;
78 timeVal %= 864000000;
79 return quit;
82 int Time::subMin(int mins)
84 timeVal -= (mins * 60 * 10000);
85 int quit = timeVal /864000000;
86 if(0==quit) return 0;
87 timeVal %= 864000000;
88 return quit;
91 int Time::addHour(int hours)
93 int day=hours/24;
94 hours%=24;
95 timeVal += (hours* 60 * 60 * 10000);
96 int quit = day+(timeVal /864000000);
97 if(0==quit) return 0;
98 timeVal %= 864000000;
99 return quit;
102 int Time::subHour(int hours)
104 timeVal -= (hours* 60 * 60 * 10000);
105 int quit = timeVal /864000000;
106 if(timeVal < 0) {
107 quit += -1;
108 if(quit<0) timeVal += ( 864000000 * quit *(-1) );
109 else timeVal +=864000000;
111 if(0==quit) return 0;
112 timeVal %= 864000000;
113 return quit;
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);
125 return 0;
128 int Time::setUsec(int us) {
129 if(us < 0 || us >= 1000000) return -1;
130 timeVal = timeVal +us/100;
131 return 0;
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) {
167 int hours,mins,secs;
168 int count;char *p;
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);
174 #else
175 struct tm *tmval = localtime(&cnow);
176 return set(hours=tmval->tm_hour,mins=tmval->tm_min,secs=tmval->tm_sec);
177 #endif
179 else{
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);