code reorg and moving files to csql base directory
[csql.git] / src / base / Date.cxx
bloba54fe4af918ae40d065bc79341915f2f3c026733
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>
19 #define SmallestValJulDate (1721426)
20 #if defined(SOLARIS)
21 #undef _TIME_H
22 #endif
25 Date::Date(int year, int month, int day)
26 { YMDToJulian(year, month, day, julianDate); }
27 int Date::set(int year, int month, int day)
28 { return YMDToJulian(year,month,day,julianDate); }
29 int Date::get(int &year, int &month, int &day) const
30 { return julianToYMD(julianDate,year,month,day); }
32 int Date::parseFrom(const char *s) {
33 int month,day,year;
34 int count;char *p;
35 if(strcmp(s,"now")==0 || strcmp(s,"NOW")==0){
36 time_t cnow = ::time(NULL);
37 #if defined(SOLARIS) && defined(REMOTE_SOLARIS)
38 struct std::tm *tmval = (struct std::tm*) localtime(&cnow);
39 return set(year=tmval->tm_year+1900, month=tmval->tm_mon+1, day=tmval->tm_mday);
40 #else
41 struct tm *tmval = localtime(&cnow);
42 return set(year=tmval->tm_year+1900,month=tmval->tm_mon+1,day=tmval->tm_mday);
43 #endif
45 else{
46 p=(char*)s;
47 while(*p!='\0'){
48 if(*p=='-'){
49 count = sscanf(s,"%d-%d-%d",&year,&month,&day);
50 break;
52 if(*p=='/'){
53 count = sscanf(s,"%d/%d/%d",&year,&month,&day);
54 break;
56 p++;
58 if (count < 3) return -1;
59 if (year < 100) year += 1900;
60 if (!isValidDate(year, month, day))
61 return -1;
62 return set(year,month,day);
66 void Date::changeToCsqlFormat(char *src)
68 char dst[10]="";
69 char *srcPtr = src;
70 char *dstPtr = dst;
71 dst[0]='\0';
72 srcPtr = src + 7;
73 strncpy(dstPtr, srcPtr, 4);
74 dst[4] = '-';
75 dstPtr = &dst[5];
76 srcPtr = src + 3;
77 if(strncasecmp(srcPtr,"JAN",3) == 0) strncpy(dstPtr, "01", 2);
78 else if(strncasecmp(srcPtr,"FEB",3)== 0) strncpy(dstPtr, "02", 2);
79 else if(strncasecmp(srcPtr,"MAR",3)== 0) strncpy(dstPtr, "03", 2);
80 else if(strncasecmp(srcPtr,"APR",3)== 0) strncpy(dstPtr, "04", 2);
81 else if(strncasecmp(srcPtr,"MAY",3)== 0) strncpy(dstPtr, "05", 2);
82 else if(strncasecmp(srcPtr,"JUN",3)== 0) strncpy(dstPtr, "06", 2);
83 else if(strncasecmp(srcPtr,"JUL",3)== 0) strncpy(dstPtr, "07", 2);
84 else if(strncasecmp(srcPtr,"AUG",3)== 0) strncpy(dstPtr, "08", 2);
85 else if(strncasecmp(srcPtr,"SEP",3)== 0) strncpy(dstPtr, "09", 2);
86 else if(strncasecmp(srcPtr,"OCT",3)== 0) strncpy(dstPtr, "10", 2);
87 else if(strncasecmp(srcPtr,"NOV",3)== 0) strncpy(dstPtr, "11", 2);
88 else if(strncasecmp(srcPtr,"DEC",3)== 0) strncpy(dstPtr, "12", 2);
89 dst[7]='-';
90 dstPtr = &dst[8];
91 strncpy(dstPtr, src, 2);
92 dst[10] = '\0';
93 strcpy(src, dst);
94 return;
96 void Date::addDay(int noOfDays)
98 julianDate = julianDate + noOfDays;
100 void Date::subDay(int noOfDays)
102 julianDate = julianDate - noOfDays;
105 void Date::addMonth(int noOfMons)
107 int year, month, day;
108 get(year,month,day);
109 month += noOfMons;
110 if(month > 12){
111 year += (month /12);
112 month %= 12;
114 set(year,month,day);
116 void Date::subMonth(int noOfMons)
118 int year, month, day;
119 get(year,month,day);
120 if(noOfMons>12)
122 year -= (noOfMons/12);
124 month -= (noOfMons%12);
125 if(month<0){
126 year -= 1;
127 month += 12;
129 set(year,month,day);
131 void Date::addYear(int noOfYrs )
133 int year, month, day;
134 get(year,month,day);
135 year += noOfYrs;
136 set(year,month,day);
139 void Date::subYear(int noOfYrs )
141 int year, month, day;
142 get(year,month,day);
143 year -= noOfYrs;
144 set(year,month,day);
148 int Date::dayOfMonth() const {
149 int year, month, day;
150 get(year,month,day);
151 return day;
153 int Date::month() const {
154 int year, month, day;
155 get(year,month,day);
156 return month;
158 int Date::year() const {
159 int year, month, day;
160 get(year,month,day);
161 return year;
164 int Date::dayOfWeek() const { return dayOfWeek(julianDate); }
166 const char *Date::dayOfWeekName() const
167 { return dayOfWeekName(dayOfWeek(julianDate)); }
169 const char *Date::dayOfWeekAbbr() const
170 { return dayOfWeekAbbr(dayOfWeek(julianDate)); }
172 static const char *day_names[] = {
173 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
175 static const char *day_abbrs[] = {
176 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
179 static const char *month_names[] = {
180 "January", "February", "March", "April", "May", "June",
181 "July", "August", "September", "October", "November", "December"
183 static const char *month_abbrs[] = {
184 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
185 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
189 static int daysBeforeMonth[] = {
190 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
192 static int days_per_month[] = {
193 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
196 const char * Date::dayOfWeekName(int day)
197 { return (day < 1 || day > 7) ? 0 : day_names[day-1]; }
199 const char * Date::dayOfWeekAbbr(int day)
200 { return (day < 1 || day > 7) ? 0 : day_abbrs[day-1]; }
202 const char * Date::monthName() const { return monthName(month()); }
203 const char * Date::monthAbbr() const { return monthAbbr(month()); }
204 const char * Date::monthName(int month)
205 { return (month < 1 || month > 12) ? 0 : month_names[month-1]; }
206 const char * Date::monthAbbr(int month)
207 { return (month < 1 || month > 12) ? 0 : month_abbrs[month-1]; }
208 Date operator+(const Date &d1, int days)
209 { return Date(d1.julianDate + days); }
210 Date operator+(int days, const Date &d1)
211 { return Date(d1.julianDate + days); }
212 Date operator-(const Date &d1, int days)
213 { return Date(d1.julianDate - days); }
215 int operator-(const Date &d1, const Date& d2)
216 { return d1.julianDate - d2.julianDate; }
218 int operator<(const Date &d1 ,const Date &d2 )
219 { return d1.julianDate < d2.julianDate; }
220 int operator>(const Date &d1 ,const Date &d2 )
221 { return d1.julianDate > d2.julianDate; }
222 int operator<=(const Date &d1 ,const Date &d2 )
223 { return d1.julianDate <= d2.julianDate; }
224 int operator>=(const Date &d1 ,const Date &d2 )
225 { return d1.julianDate >= d2.julianDate; }
226 int operator==(const Date &d1 ,const Date &d2 )
227 { return d1.julianDate == d2.julianDate; }
228 int operator!=(const Date &d1 ,const Date &d2 )
229 { return d1.julianDate != d2.julianDate; }
231 bool Date::isValid() const
232 { return julianDate >= SmallestValJulDate; }
234 bool Date::isLeapYear(int year)
236 return (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0));
239 int Date::dayOfYear() const {
240 int year,month,day;
241 get(year,month,day);
242 int tmp = daysBeforeMonth[month-1];
243 if (month >= 3 && isLeapYear(year))
244 tmp++;
245 return tmp + day;
248 int Date::daysInMonth(int month, int year) {
249 --month;
250 int tmp = days_per_month[month];
251 if (month == 1 && isLeapYear(year)) tmp++;
252 return tmp;
255 bool Date::isValidDate(int year, int month, int day) {
257 if (year < 1 || year > 9999) return false;
258 if (month < 1 || month > 12) return false;
259 return (day >= 1) && (day <= daysInMonth(month,year));
262 // Algorithm Author: Robert G. Tantzen
263 int Date::YMDToJulian(int year, int mon, int day, JulianRep &jul) {
264 if (!isValidDate(year, mon, day))
265 { jul = (JulianRep)0; return -1; }
267 // year, month, day are assumed to be valid
268 int m = mon, d = day, y = year;
269 int c, ya, j;
271 if (m > 2) m -= 3;
272 else { m += 9; --y; }
273 c = y/100;
274 ya = y - (100 * c);
275 j = (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5 + d + 1721119;
276 jul = (JulianRep)j;
277 return 0;
280 // Julian date converter. Takes a julian date (the number of days since some
281 // distant epoch or other), and returns month, day of month, and year in
282 // integer references.
283 // Algorithm Author: Robert G. Tantzen
284 int Date::dayOfWeek(JulianRep jul) {
285 return (int)((jul+1)%7+1);
288 int Date::julianToYMD(JulianRep jul, int &year, int &month, int &day) {
289 int j = jul;
290 int d, m, y;
292 if (j < SmallestValJulDate)
293 { year = month = day = 0; return -1; }
295 j -= 1721119;
296 y = (4 * j - 1)/146097;
297 j = 4 * j - 1 - 146097 * y;
298 d = j/4;
299 j = (4 * d + 3)/1461;
300 d = 4 * d + 3 - 1461 * j;
301 d = (d + 4)/4;
302 m = (5 * d - 3)/153;
303 d = 5 * d - 3 - 153 * m;
304 d = (d + 5) / 5;
305 y = 100 * y + j;
306 if (m < 10) m += 3;
307 else { m -= 9; ++y; }
308 month = m;
309 day = d;
310 year = y;
311 return 0;