Debian Codegraph @

src/widgets/date.cxx

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 // David Freese, W1HKJ
4 //
5 // This file is part of flmsg
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20
21 #include <config.h>
22
23 #include <iostream>
24 #include <cstring>
25 #include <string>
26 #include <ctime>
27 #include <cstdio>
28 #include <fstream>
29
30 #include "date.h"
31
32 const int Date::mdays[] =
33 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
34
35 const int Date::jdays[2][13] = {
36 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
37 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
38 };
39
40 const char *Date::month_name[] =
41 {
42 "Jan",
43 "Feb",
44 "Mar",
45 "Apr",
46 "May",
47 "Jun",
48 "Jul",
49 "Aug",
50 "Sep",
51 "Oct",
52 "Nov",
53 "Dec"
54 };
55
56 bool date_local_ = false;
57
58 void Date::today()
59 {
60 time_t t;
61 struct tm *now;
62 time( &t );
63 if (date_local_)
64 now = localtime( &t );
65 else
66 now = gmtime(&t);
67 year = now->tm_year + 1900;
68 month = now->tm_mon + 1;
69 day = now->tm_mday;
70 }
71
72 Date::Date()
73 {
74 today();
75 fmt = 0;
76 }
77
78 Date::Date( int m, int d, int y )
79 {
80 setDate( m, d, y );
81 fmt = 1;
82 }
83
84 void Date::setDate( int mm, int dd, int yy )
85 {
86 if( isvalid( mm, dd, yy ) ) {
87 year = yy; month = mm; day = dd;
88 } else
89 today();
90 }
91
92 void Date::setDate( Date &dt )
93 {
94 year = dt.year;
95 month = dt.month;
96 day = dt.day;
97 }
98
99 void Date::setFormat( int iFmt )
100 {
101 fmt = iFmt;
102 }
103
104 void Date::Year( int y )
105 {
106 year = y;
107 }
108
109 int Date::Year()
110 {
111 return year;
112 }
113
114 void Date::Month( int m )
115 {
116 month = m;
117 }
118
119 int Date::Month()
120 {
121 return month;
122 }
123
124 void Date::Day( int d )
125 {
126 day = d;
127 }
128
129 int Date::Day()
130 {
131 return day;
132 }
133
134 bool Date::leapYear( int y )
135 {
136 if( y % 400 == 0 || ( y % 100 != 0 && y % 4 == 0 ) )
137 return true;
138 return false;
139 }
140
141
142 bool Date::isvalid( int m, int d, int y )
143 {
144 if( m < 1 || m > 12 ) return false;
145 if( d < 1 ) return false;
146 if( leapYear( y ) ){
147 if( m == 2 && d > 29 )
148 return false;
149 else
150 return true;
151 }
152 if( d > mdays[m] ) return false;
153 return true;
154 }
155
156 int Date::daysinmonth (int month, int leap)
157 {
158 /* Validate the month. */
159 if (month < JANUARY || month > DECEMBER)
160 return -1;
161
162 /* Return 28, 29, 30, or 31 based on month/leap. */
163 switch (month) {
164 case FEBRUARY:
165 return leap ? 29 : 28;
166 default:
167 return mdays[month];
168 }
169 }
170
171
172 int Date::dayofyear (int year, int mon, int mday)
173 {
174 /* Return day of year. */
175 return mday + jdays[isleapyear (year) ? 1 : 0][mon];
176 }
177
178 int Date::dayofepoch (int year, int mon, int mday)
179 {
180 int doe;
181 int era, cent, quad, rest;
182
183 /* break down the year into 400, 100, 4, and 1 year multiples */
184 rest = year - 1;
185 quad = rest / 4; rest %= 4;
186 cent = quad / 25; quad %= 25;
187 era = cent / 4; cent %= 4;
188
189 /* set up doe */
190 doe = dayofyear (year, mon, mday);
191 doe += era * (400 * 365 + 97);
192 doe += cent * (100 * 365 + 24);
193 doe += quad * (4 * 365 + 1);
194 doe += rest * 365;
195
196 return doe;
197 }
198
199 int Date::dayofweek (int year, int mon, int mday)
200 {
201 return dayofepoch (year, mon, mday) % 7;
202 }
203
204 void Date::previous_month ()
205 {
206 if (month == 1)
207 {
208 month = 12;
209 year--;
210 }
211 else
212 month--;
213
214 while ((day >= 1) && (!datevalid ()))
215 day--;
216 }
217
218 void Date::next_month ()
219 {
220 if (month == 12)
221 {
222 month = 1;
223 year++;
224 }
225 else
226 month++;
227
228 while ((day >= 1) && (!datevalid ()))
229 day--;
230 }
231
232 void Date::previous_year ()
233 {
234 if (month == 2 && day == 29)
235 day = 28;
236 year--;
237 }
238
239 void Date::next_year ()
240 {
241 if (month == 2 && day == 29)
242 day = 28;
243 year++;
244 }
245
246
247 char *Date::szDate (int fmt)
248 {
249 static char temp[20];
250 char szMonth[10];
251
252 switch (fmt) {
253 case 0:
254 snprintf (temp, sizeof(temp), "%4d-%02d-%02d",
255 year,
256 month,
257 day);
258 break;
259 case 1 :
260 snprintf (temp, sizeof(temp), "%02d/%02d/%02d",
261 month,
262 day,
263 year > 1999 ? year - 2000 : year - 1900);
264 break;
265 case 2 :
266 snprintf (temp, sizeof(temp), "%02d/%02d/%02d",
267 day,
268 month,
269 year > 1999 ? year - 2000 : year - 1900);
270 break;
271 case 3 :
272 snprintf (temp, sizeof(temp), "%4d-%02d-%02d",
273 year,
274 day,
275 month);
276 break;
277 case 4 :
278 snprintf (temp, sizeof(temp), "%4d%02d%02d",
279 year,
280 month,
281 day);
282 break;
283 case 5 :
284 strcpy (szMonth, month_name [month - 1]);
285 szMonth[3] = 0;
286 snprintf (temp, sizeof(temp), "%s %2d, %4d", szMonth, day, year);
287 break;
288 case 6 :
289 strcpy (szMonth, month_name [month - 1]);
290 szMonth[3] = 0;
291 for (int i = 0; i < 3; i++) szMonth[i] = toupper(szMonth[i]);
292 snprintf (temp, sizeof(temp), "%s %d", szMonth, day);
293 break;
294 default :
295 snprintf (temp, sizeof(temp), "%02d/%02d/%04d",
296 month,
297 day,
298 year);
299 break;
300 }
301 return temp;
302 }
303
304 char *Date::szDate ()
305 {
306 return szDate (fmt);
307 }
308
309 // operator functions
310
311 std::ostream &operator<<( std::ostream &output, Date &d )
312 {
313 output << d.szDate ();
314 return output;
315 }
316
317 bool Date::endOfMonth( int d )
318 {
319 if( month == 2 && leapYear( year ) )
320 return (d == 29 ); // last day of Feb in leap year
321 else
322 return (d == mdays[ month ] );
323 }
324
325 void Date::helpIncrement()
326 {
327 if( endOfMonth( day ) && month == 12 ) { // end year
328 day = 1;
329 month = 1;
330 ++year;
331 } else if ( endOfMonth( day ) ) {
332 day = 1;
333 ++month;
334 } else
335 ++day;
336 }
337
338 Date &Date::operator++()
339 {
340 helpIncrement();
341 return *this; // reference return to create an lvalue
342 }
343
344 Date Date::operator++( int )
345 {
346 Date temp = *this;
347 helpIncrement();
348 return temp; // return non-increment, saved temporary object
349 }
350
351 const Date &Date::operator+=( int ndays )
352 {
353 for( int i = 0; i < ndays; i++ )
354 helpIncrement();
355 return *this; // enables cascading
356 }
357
358
359 bool Date::operator==( const Date &d )
360 {
361 if( this->year != d.year ) return false;
362 if( this->month != d.month ) return false;
363 if( this->day != d.day ) return false;
364 return true;
365 }
366
367 bool Date::operator!=( const Date &d )
368 {
369 return ( !( *this == d ) );
370 }
371
372 bool Date::operator<( const Date &d )
373 {
374 if( this->year < d.year ) return true;
375 if( this->year > d.year ) return false;
376 if( this->month < d.month ) return true;
377 if( this->month > d.month ) return false;
378 if( this->day < d.day ) return true;
379 return false;
380 }
381
382 bool Date::operator>( const Date &d )
383 {
384 if( *this < d ) return false;
385 if( *this == d ) return false;
386 return true;
387 }
388
389 void Date::operator=( const Date &d )
390 {
391 this->year = d.year;
392 this->month = d.month;
393 this->day = d.day;
394 }
395
396 double Date::dJulian ()
397 {
398 int DaysInYear = 365;
399 if (leapYear ()) DaysInYear++;
400 return ( year + 1.0 * (dayofyear (year, month, day) - 1) / DaysInYear);
401 }