-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalendarFormC.cc
183 lines (156 loc) · 5.43 KB
/
CalendarFormC.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// $Id: CalendarFormC.cc 2641 2007-09-02 21:31:02Z flaterco $
/* Calendar Manage construction, organization, and printing of calendars.
Copyright (C) 1998 David Flater.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.hh"
#include "Calendar.hh"
#include "CalendarFormC.hh"
// Controls for how many columns you get in calendar mode, CSV format.
// If you get more events than there is room for, they are discarded.
// A value of x for numMaxMin means that you get x columns for the max
// times, x columns for the max heights, x columns for the min times,
// x columns for the min heights, and 2x columns for slacks.
static const unsigned numMaxMin (5U);
// A value of x for numRiseSet means that you get x columns for
// sunrise, x columns for sunset, x columns for moonrise, and x
// columns for moonset.
//
// Yes, you can have two sunsets in one day, and you don't even need
// Daylight Savings Time to do it:
//
// Isla Neny, Antarctica
// 68.2000° S, 67.0000° W
//
// 2001-01-24 12:03 AM ARST Sunset
// 2001-01-24 3:17 AM ARST Sunrise
// 2001-01-24 11:57 PM ARST Sunset
static const unsigned numRiseSet (1U);
// Moon phases and mark crossings are discarded.
CalendarFormC::CalendarFormC (Station &station,
Timestamp startTime,
Timestamp endTime,
Mode::Mode mode):
Calendar (station, startTime, endTime, mode, Format::CSV) {}
static void addCSVevent (SafeVector<TideEvent> &events,
unsigned limit,
const TideEvent &tideEvent,
const Dstr &date,
constString desc) {
// The limits on number of events are necessary for the CSV format
// to be consistent.
if (events.size() == limit) {
Dstr details ("Warning: too many ");
details += desc;
details += " events on ";
details += date;
details += "; increase ";
if (tideEvent.isSunMoonEvent())
details += "numRiseSet";
else
details += "numMaxMin";
details += " in CalendarFormC.cc";
Global::log (details, LOG_WARNING);
} else
events.push_back (tideEvent);
}
static void printCSVmaxmin (Dstr &text_out,
const SafeVector<TideEvent> &events,
unsigned limit,
const Dstr &timezone) {
Dstr t;
for (unsigned i=0; i<limit; ++i) {
text_out += ',';
if (i < events.size()) {
events[i].eventTime.printTime (t, timezone);
text_out += t;
text_out += ',';
events[i].eventLevel.printnp (t);
text_out += t;
} else
text_out += ',';
}
}
static void printCSVother (Dstr &text_out,
const SafeVector<TideEvent> &events,
unsigned limit,
const Dstr &timezone) {
Dstr t;
for (unsigned i=0; i<limit; ++i) {
text_out += ',';
if (i < events.size()) {
events[i].eventTime.printTime (t, timezone);
text_out += t;
}
}
}
void CalendarFormC::print (Dstr &text_out) {
assert (_mode == Mode::calendar);
text_out = (char *)NULL;
Dstr stationName (_station.name);
stationName.repchar (',', Global::CSV_repchar);
for (Date loopDate (firstDay); loopDate <= lastDay; ++loopDate) {
Dstr date;
loopDate.print (date);
SafeVector<TideEvent> maxes,
mins,
slacks,
sunrises,
sunsets,
moonrises,
moonsets;
SafeVector<TideEvent> &eventVector (eventVectors[loopDate]);
for (SafeVector<TideEvent>::iterator it (eventVector.begin());
it != eventVector.end();
++it) {
TideEvent &te (*it);
switch (te.eventType) {
case TideEvent::max:
addCSVevent (maxes, numMaxMin, te, date, "max");
break;
case TideEvent::min:
addCSVevent (mins, numMaxMin, te, date, "min");
break;
case TideEvent::slackrise:
case TideEvent::slackfall:
addCSVevent (slacks, 2*numMaxMin, te, date, "slack");
break;
case TideEvent::sunrise:
addCSVevent (sunrises, numRiseSet, te, date, "sunrise");
break;
case TideEvent::sunset:
addCSVevent (sunsets, numRiseSet, te, date, "sunset");
break;
case TideEvent::moonrise:
addCSVevent (moonrises, numRiseSet, te, date, "moonrise");
break;
case TideEvent::moonset:
addCSVevent (moonsets, numRiseSet, te, date, "moonset");
break;
default:
;
}
}
text_out += stationName;
text_out += ',';
text_out += date;
printCSVmaxmin (text_out, maxes, numMaxMin, timezone);
printCSVmaxmin (text_out, mins, numMaxMin, timezone);
printCSVother (text_out, slacks, 2*numMaxMin, timezone);
printCSVother (text_out, sunrises, numRiseSet, timezone);
printCSVother (text_out, sunsets, numRiseSet, timezone);
printCSVother (text_out, moonrises, numRiseSet, timezone);
printCSVother (text_out, moonsets, numRiseSet, timezone);
text_out += '\n';
}
}
// Cleanup2006 Done