code reorg and moving files to csql base directory
[csql.git] / src / base / Timestamp.cxx
blobafe4ab9eed1126c1ce7a22f43e2d2bdf7e27ddf6
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
22 int TimeStamp::parseFrom(const char *s) {
23 int hours,mins,secs;
24 int month,day,year;
25 int count;char *p;
26 if(strcmp(s,"now")==0 || strcmp(s,"NOW")==0){
27 time_t cnow = ::time(NULL);
28 #if defined(SOLARIS) && defined(REMOTE_SOLARIS)
29 struct std::tm *tmval = (struct std::tm*) localtime(&cnow);
30 setDate(year=tmval->tm_year+1900,month=tmval->tm_mon+1,day=tmval->tm_mday);
31 return setTime(hours=tmval->tm_hour,mins=tmval->tm_min,secs=tmval->tm_sec);
32 #else
33 struct tm *tmval = localtime(&cnow);
34 setDate(year=tmval->tm_year+1900,month=tmval->tm_mon+1,day=tmval->tm_mday);
35 return setTime(hours=tmval->tm_hour,mins=tmval->tm_min,secs=tmval->tm_sec);
36 #endif
38 else{
39 p=(char*)s;
40 while(*p!='\0'){
41 if(*p=='-'){
42 count = sscanf(s,"%d-%d-%d %d:%d:%d",&year,&month,&day, &hours, &mins, &secs);
43 break;
45 if(*p=='/'){
46 count = sscanf(s,"%d/%d/%d %d:%d:%d",&year,&month,&day, &hours, &mins, &secs);
47 break;
49 p++;
51 if (count < 5) return -1;
52 if (count == 5) secs = 0;
53 if (year < 100) year += 1900;
55 if (!date.isValidDate(year, month, day))
56 return -1;
58 setDate(year,month,day);
60 if (!time.isValidTime(hours,mins,secs))
61 return -1;
62 return setTime(hours,mins,secs);
66 void TimeStamp::addHour(int hours )
68 int day = time.addHour(hours);
69 if(day!=0){ date.addDay(day);}
72 void TimeStamp::subHour(int hours )
74 int day = time.subHour(hours);
75 if(day!=0){ date.subDay(day);}
78 void TimeStamp::addMin(int noOfMins )
80 int day = time.addMin(noOfMins);
81 if(day!=0){ date.addDay(day);}
84 void TimeStamp::subMin(int noOfMins)
86 int day = time.subMin(noOfMins);
87 if(day!=0) {date.subDay(day);}
90 void TimeStamp::addSec(int noOfSecs)
92 int day = time.addSec(noOfSecs);
93 if(day!=0){ date.addDay(day);}
96 void TimeStamp::subSec(int noOfSecs)
98 int day = time.subSec(noOfSecs);
99 if(day!=0){ date.subDay(day);}
102 int TimeStamp::secDiff(TimeStamp &ts)
104 int day1= date.getCalDay();
105 int day2= Date(ts).getCalDay();
106 int sec1= time.getCalSec();
107 int sec2= Time(ts).getCalSec();
108 return (((day1*24*60*60)+sec1) - ((day2*24*60*60)+sec2));
111 int TimeStamp::minDiff(TimeStamp &ts)
113 int day1= date.getCalDay();
114 int day2= Date(ts).getCalDay();
115 int sec1= time.getCalSec();
116 int sec2= Time(ts).getCalSec();
117 int secdiff = (((day1*24*60*60)+sec1) - ((day2*24*60*60)+sec2));
119 return (secdiff/60);
122 int TimeStamp::hourDiff(TimeStamp &ts)
124 int day1= date.getCalDay();
125 int day2= Date(ts).getCalDay();
126 int sec1= time.getCalSec();
127 int sec2= Time(ts).getCalSec();
128 int secdiff = (((day1*24*60*60)+sec1) - ((day2*24*60*60)+sec2));
129 return (secdiff/3600);
132 int TimeStamp::dayDiff(TimeStamp &ts)
134 int day1= date.getCalDay();
135 int day2= Date(ts).getCalDay();
136 int sec1= time.getCalSec();
137 int sec2= Time(ts).getCalSec();
138 int secdiff = (((day1*24*60*60)+sec1) - ((day2*24*60*60)+sec2));
139 return (secdiff/(3600*24));
143 int TimeStamp::monthDiff(TimeStamp &ts)
145 int day1= date.getCalDay();
146 int day2= Date(ts).getCalDay();
147 int sec1= time.getCalSec();
148 int sec2= Time(ts).getCalSec();
149 int secdiff = (((day1*24*60*60)+sec1) - ((day2*24*60*60)+sec2));
150 return (secdiff/(3600*24*30));
154 int TimeStamp::yearDiff(TimeStamp &ts)
156 int day1= date.getCalDay();
157 int day2= Date(ts).getCalDay();
158 int sec1= time.getCalSec();
159 int sec2= Time(ts).getCalSec();
160 int secdiff = (((day1*24*60*60)+sec1) - ((day2*24*60*60)+sec2));
161 return (secdiff/(3600*24*30*12));
164 void TimeStamp::changeToCsqlFormat(char *src)
166 char dst[20];
167 char *srcPtr = src;
168 char *dstPtr = dst;
169 dst[0]='\0';
170 srcPtr = src + 7;
171 strncpy(dstPtr, srcPtr, 4);
172 dst[4] = '-';
173 dstPtr = &dst[5];
174 srcPtr = src + 3;
175 if(strncasecmp(srcPtr,"JAN",3) == 0) strncpy(dstPtr, "01", 2);
176 else if(strncasecmp(srcPtr,"FEB",3)== 0) strncpy(dstPtr, "02", 2);
177 else if(strncasecmp(srcPtr,"MAR",3)== 0) strncpy(dstPtr, "03", 2);
178 else if(strncasecmp(srcPtr,"APR",3)== 0) strncpy(dstPtr, "04", 2);
179 else if(strncasecmp(srcPtr,"MAY",3)== 0) strncpy(dstPtr, "05", 2);
180 else if(strncasecmp(srcPtr,"JUN",3)== 0) strncpy(dstPtr, "06", 2);
181 else if(strncasecmp(srcPtr,"JUL",3)== 0) strncpy(dstPtr, "07", 2);
182 else if(strncasecmp(srcPtr,"AUG",3)== 0) strncpy(dstPtr, "08", 2);
183 else if(strncasecmp(srcPtr,"SEP",3)== 0) strncpy(dstPtr, "09", 2);
184 else if(strncasecmp(srcPtr,"OCT",3)== 0) strncpy(dstPtr, "10", 2);
185 else if(strncasecmp(srcPtr,"NOV",3)== 0) strncpy(dstPtr, "11", 2);
186 else if(strncasecmp(srcPtr,"DEC",3)== 0) strncpy(dstPtr, "12", 2);
187 dst[7]='-';
188 dstPtr = &dst[8];
189 strncpy(dstPtr, src, 2);
190 dstPtr = &dst[10];
191 srcPtr = src + 11;
192 strncpy(dstPtr, srcPtr, 9);
193 dst[19]='\0';
194 strcpy(src, dst);
195 return;
198 int operator< (const TimeStamp &d1, const TimeStamp &d2)
199 { return (d1.date != d2.date) ? d1.date < d2.date : d1.time < d2.time; }
200 int operator> (const TimeStamp &d1, const TimeStamp &d2)
201 { return (d1.date != d2.date) ? d1.date > d2.date : d1.time > d2.time; }
202 int operator<=(const TimeStamp &d1, const TimeStamp &d2)
203 { return (d1.date != d2.date) ? d1.date < d2.date : d1.time <= d2.time; }
204 int operator>=(const TimeStamp &d1, const TimeStamp &d2)
205 { return (d1.date != d2.date) ? d1.date > d2.date : d1.time >= d2.time; }
206 int operator==(const TimeStamp &d1, const TimeStamp &d2)
207 { return d1.date == d2.date && d1.time == d2.time; }
208 int operator!=(const TimeStamp &d1, const TimeStamp &d2)
209 { return d1.date != d2.date || d1.time != d2.time; }