-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdates.cpp
More file actions
308 lines (278 loc) · 7.03 KB
/
dates.cpp
File metadata and controls
308 lines (278 loc) · 7.03 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* RDNSLOGS 7.0
* Performs Reverse DNS lookups on multiple logfiles.
* This program is designed to work with the Analog Logfile analyzer.
*
* Supports compressed files
*
* Antonio C. Silvestri
* tonysilvestri@bytecodeman.com
*
***/
#pragma warning( push )
#pragma warning(disable:4786)
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "dates.h"
#include "LookupIPRoutines.h"
#include "library.h"
const int FEB = 1;
const int DEC = 11;
static int year, month, date, hour, min;
static const char *cmonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static const char *cdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static inline int dow(int m, int d, int y) {
y -= m < 3;
return (y + y/4 - y/100 + y/400 + "-bed=pen+mad."[m] + d) % 7;
}
static inline bool isLeapYear(int nYear) {
return ((nYear % 4 == 0) && (nYear % 100 != 0 || nYear % 400 == 0));
}
static inline void putc(char *&s, char c) {
*s++ = c;
}
static inline void put02d(char *&s, int d) {
putc(s, (char)((d / 10) + '0'));
putc(s, (char)((d % 10) + '0'));
}
static inline void put04d(char *&s, int d) {
putc(s, (char)((d / 1000) + '0'));
putc(s, (char)((d / 100) % 10 + '0'));
putc(s, (char)((d / 10) % 10 + '0'));
putc(s, (char)((d % 10) + '0'));
}
static inline void puts(char *&s, const char *t, int n) {
int i;
for (i = 0; i < n && *t; i++, t++)
putc(s, *t);
for (; i < n; i++)
putc(s, ' ');
}
// Much of the logic and code found in the following function has been adapted from
// Stephen Turner's analog 5.24 dates.c source located at http://www.analog.cx/
static bool parsedate(time_t starttime, const char *s, int &y, int &m, int &d, int &h, int &n) {
int monthlength[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
tm *st;
char *p;
st = localtime(&starttime);
if (isdigit(s[0]) && isdigit(s[1])) {
y = 10 * (s[0] - '0') + (s[1] - '0');
s += 2;
y += 1900;
if (y < 1970)
y += 100;
}
else if (s[0] == '+' && isdigit(s[1]) && isdigit(s[2])) {
y = st->tm_year + 1900 + 10 * (s[1] - '0') + (s[2] - '0');
s += 3;
}
else if (s[0] == '-' && isdigit(s[1]) && isdigit(s[2])) {
y = st->tm_year + 1900 - 10 * (s[1] - '0') - (s[2] - '0');
s += 3;
}
else
return true;
if (isdigit(s[0]) && isdigit(s[1])) {
m = 10 * (s[0] - '0') + (s[1] - '0') - 1;
if (m > 11 || m < 0)
return true;
s += 2;
}
else if (s[0] == '+' && isdigit(s[1]) && isdigit(s[2])) {
m = st->tm_mon + 10 * (s[1] - '0') + (s[2] - '0');
s += 3;
}
else if (s[0] == '-' && isdigit(s[1]) && isdigit(s[2])) {
m = st->tm_mon - 10 * (s[1] - '0') - (s[2] - '0');
s += 3;
}
else
return true;
while (m < 0) { /* need to do this now because about to use monthlength */
m += 12;
y--;
}
while (m > 11) {
m -= 12;
y++;
}
if (isdigit(s[0]) && isdigit(s[1])) {
d = (int)strtol(s, &p, 10);
if (d > 31 || d <= 0)
return true;
else if (d > (int)(monthlength[m]) + (m == FEB && isLeapYear(y)))
d = monthlength[m] + (m == FEB && isLeapYear(y));
} /* relative dates must be >= 2 digits but can be more */
else if (s[0] == '+' && isdigit(s[1]) && isdigit(s[2]))
d = st->tm_mday + (int)strtol(s + 1, &p, 10);
else if (s[0] == '-' && isdigit(s[1]) && isdigit(s[2]))
d = st->tm_mday - (int)strtol(s + 1, &p, 10);
else
return true;
if (*p == ':') { /* parse hour & minute */
s = p + 1;
if (isdigit(s[0]) && isdigit(s[1])) {
h = 10 * (s[0] - '0') + (s[1] - '0');
if (h > 23)
return true;
s += 2;
}
else if (s[0] == '+' && isdigit(s[1]) && isdigit(s[2])) {
h = st->tm_hour + 10 * (s[1] - '0') + (s[2] - '0');
s += 3;
}
else if (s[0] == '-' && isdigit(s[1]) && isdigit(s[2])) {
h = st->tm_hour - 10 * (s[1] - '0') - (s[2] - '0');
s += 3;
}
else
return true;
if (isdigit(s[0]) && isdigit(s[1])) {
n = 10 * (s[0] - '0') + (s[1] - '0');
if (n > 59)
return true;
s += 2;
}
else if (s[0] == '+' && isdigit(s[1]) && isdigit(s[2])) {
n = st->tm_min + 10 * (s[1] - '0') + (s[2] - '0');
s += 3;
}
else if (s[0] == '-' && isdigit(s[1]) && isdigit(s[2])) {
n = st->tm_min - 10 * (s[1] - '0') - (s[2] - '0');
s += 3;
}
else
return true;
if (s[0] != '\0')
return true;
}
else if (*p == '\0') {
h = 0;
n = 0;
}
else
return true;
while (n < 0) {
n += 60;
h--;
}
while (n > 59) {
n -= 60;
h++;
}
while (h < 0) {
h += 24;
d--;
}
while (h > 23) {
h -= 24;
d++;
}
while (d < 0) {
m--;
if (m < 0) { /* NB already adjusted m once above */
m += 12;
y--;
}
d += monthlength[m] + (m == FEB && isLeapYear(y));
}
while (d > (int)(monthlength[m]) + (m == FEB && isLeapYear(y))) {
d -= monthlength[m] + (m == FEB && isLeapYear(y));
m++;
if (m > 11) {
m -= 12;
y++;
}
}
return false;
}
//**************************************************************************************
void initTimeData(const char*tospec) {
time_t starttime;
time(&starttime);
if (parsedate(starttime, tospec, ::year, ::month, ::date, ::hour, ::min)) {
char buf[128];
sprintf(buf, "Illegal TO string specified: %s\n", tospec);
endItAll(buf);
}
}
//**************************************************************************************
bool processPercentDirectives(const char *tmp, char *tmpbuffer, int lentmpbuffer) {
bool escape = false;
bool error = false;
char *ptr = tmpbuffer;
char *end = tmpbuffer + lentmpbuffer - 1;
for (;!error && *tmp && ptr < end; tmp++) {
if (!escape) {
if (end - ptr > 1) {
if (*tmp != '%')
putc(ptr, *tmp);
else
escape = true;
}
else
error = true;
}
else {
escape = false;
switch (*tmp) {
case '%':
putc(ptr, '%');
break;
case 'D': // %D date of month
if (end - ptr > 2)
put02d(ptr, ::date);
else
error = true;
break;
case 'm': // %m month name, in English
if (end - ptr > 3)
puts(ptr, cmonths[::month], 3);
else
error = true;
break;
case 'M': // %M month number
if (end - ptr > 2)
put02d(ptr, ::month + 1);
else
error = true;
break;
case 'y': // %y two-digit year
if (end - ptr > 2)
put02d(ptr, ::year % 100);
else
error = true;
break;
case 'Y': // %Y four-digit year
if (end - ptr > 4)
put04d(ptr, ::year);
else
error = true;
break;
case 'H': // %H hour
if (end - ptr > 2)
put02d(ptr, ::hour % 100);
else
error = true;
break;
case 'n': // %n minute
if (end - ptr > 2)
put02d(ptr, ::min % 100);
else
error = true;
break;
case 'w': // %w day of week, in English
if (end - ptr > 3)
puts(ptr, cdays[dow(month + 1, date, year)], 3);
else
error = true;
break;
}
}
}
putc(ptr, '\0');
return error;
}
#pragma warning( pop )