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 ***************************************************************************/
19 #define SmallestValJulDate (1721426)
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
) {
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
);
41 struct tm
*tmval
= localtime(&cnow
);
42 return set(year
=tmval
->tm_year
+1900,month
=tmval
->tm_mon
+1,day
=tmval
->tm_mday
);
49 count
= sscanf(s
,"%d-%d-%d",&year
,&month
,&day
);
53 count
= sscanf(s
,"%d/%d/%d",&year
,&month
,&day
);
58 if (count
< 3) return -1;
59 if (year
< 100) year
+= 1900;
60 if (!isValidDate(year
, month
, day
))
62 return set(year
,month
,day
);
66 void Date::changeToCsqlFormat(char *src
)
73 strncpy(dstPtr
, srcPtr
, 4);
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);
91 strncpy(dstPtr
, src
, 2);
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
;
116 void Date::subMonth(int noOfMons
)
118 int year
, month
, day
;
122 year
-= (noOfMons
/12);
124 month
-= (noOfMons
%12);
131 void Date::addYear(int noOfYrs
)
133 int year
, month
, day
;
139 void Date::subYear(int noOfYrs
)
141 int year
, month
, day
;
148 int Date::dayOfMonth() const {
149 int year
, month
, day
;
153 int Date::month() const {
154 int year
, month
, day
;
158 int Date::year() const {
159 int year
, month
, day
;
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 {
242 int tmp
= daysBeforeMonth
[month
-1];
243 if (month
>= 3 && isLeapYear(year
))
248 int Date::daysInMonth(int month
, int year
) {
250 int tmp
= days_per_month
[month
];
251 if (month
== 1 && isLeapYear(year
)) 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
;
272 else { m
+= 9; --y
; }
275 j
= (146097*c
)/4 + (1461*ya
)/4 + (153*m
+2)/5 + d
+ 1721119;
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
) {
292 if (j
< SmallestValJulDate
)
293 { year
= month
= day
= 0; return -1; }
296 y
= (4 * j
- 1)/146097;
297 j
= 4 * j
- 1 - 146097 * y
;
299 j
= (4 * d
+ 3)/1461;
300 d
= 4 * d
+ 3 - 1461 * j
;
303 d
= 5 * d
- 3 - 153 * m
;
307 else { m
-= 9; ++y
; }