aboutsummaryrefslogtreecommitdiffstats
path: root/src/datetime-parser.c
blob: 50694dad531edb5b62f798a56d5cea403169e05b (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * blogc: A blog compiler.
 * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_TIME_H
#include <time.h>
#endif /* HAVE_TIME_H */

#include <string.h>

#include "error.h"
#include <squareball.h>
#include "datetime-parser.h"


typedef enum {
    DATETIME_FIRST_YEAR = 1,
    DATETIME_SECOND_YEAR,
    DATETIME_THIRD_YEAR,
    DATETIME_FOURTH_YEAR,
    DATETIME_FIRST_HYPHEN,
    DATETIME_FIRST_MONTH,
    DATETIME_SECOND_MONTH,
    DATETIME_SECOND_HYPHEN,
    DATETIME_FIRST_DAY,
    DATETIME_SECOND_DAY,
    DATETIME_SPACE,
    DATETIME_FIRST_HOUR,
    DATETIME_SECOND_HOUR,
    DATETIME_FIRST_COLON,
    DATETIME_FIRST_MINUTE,
    DATETIME_SECOND_MINUTE,
    DATETIME_SECOND_COLON,
    DATETIME_FIRST_SECOND,
    DATETIME_SECOND_SECOND,
    DATETIME_DONE,
} blogc_datetime_state_t;


char*
blogc_convert_datetime(const char *orig, const char *format,
    blogc_error_t **err)
{
    if (err == NULL || *err != NULL)
        return NULL;

#ifndef HAVE_TIME_H

    *err = blogc_error_new(BLOGC_WARNING_DATETIME_PARSER,
        "Your operating system does not supports the datetime functionalities "
        "used by blogc. Sorry.");
    return NULL;

#else

    struct tm t;
    memset(&t, 0, sizeof(struct tm));
    t.tm_isdst = -1;

    blogc_datetime_state_t state = DATETIME_FIRST_YEAR;
    int tmp = 0;
    int diff = '0';

    for (unsigned int i = 0; orig[i] != '\0'; i++) {
        char c = orig[i];

        switch (state) {

            case DATETIME_FIRST_YEAR:
                if (c >= '0' && c <= '9') {
                    tmp += (c - diff) * 1000;
                    state = DATETIME_SECOND_YEAR;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid first digit of year. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_SECOND_YEAR:
                if (c >= '0' && c <= '9') {
                    tmp += (c - diff) * 100;
                    state = DATETIME_THIRD_YEAR;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid second digit of year. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_THIRD_YEAR:
                if (c >= '0' && c <= '9') {
                    tmp += (c - diff) * 10;
                    state = DATETIME_FOURTH_YEAR;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid third digit of year. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_FOURTH_YEAR:
                if (c >= '0' && c <= '9') {
                    tmp += c - diff - 1900;
                    if (tmp < 0) {
                        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                            "Invalid year. Found %d, must be >= 1900.",
                            tmp + 1900);
                        break;
                    }
                    t.tm_year = tmp;
                    state = DATETIME_FIRST_HYPHEN;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid fourth digit of year. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_FIRST_HYPHEN:
                if (c == '-') {
                    tmp = 0;
                    state = DATETIME_FIRST_MONTH;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid separator between year and month. "
                    "Found '%c', must be '-'.", c);
                break;

            case DATETIME_FIRST_MONTH:
                if (c >= '0' && c <= '1') {
                    tmp += (c - diff) * 10;
                    state = DATETIME_SECOND_MONTH;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid first digit of month. "
                    "Found '%c', must be integer >= 0 and <= 1.", c);
                break;

            case DATETIME_SECOND_MONTH:
                if (c >= '0' && c <= '9') {
                    tmp += c - diff - 1;
                    if (tmp < 0 || tmp > 11) {
                        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                            "Invalid month. Found %d, must be >= 1 and <= 12.",
                            tmp + 1);
                        break;
                    }
                    t.tm_mon = tmp;
                    state = DATETIME_SECOND_HYPHEN;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid second digit of month. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_SECOND_HYPHEN:
                if (c == '-') {
                    tmp = 0;
                    state = DATETIME_FIRST_DAY;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid separator between month and day. "
                    "Found '%c', must be '-'.", c);
                break;

            case DATETIME_FIRST_DAY:
                if (c >= '0' && c <= '3') {
                    tmp += (c - diff) * 10;
                    state = DATETIME_SECOND_DAY;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid first digit of day. "
                    "Found '%c', must be integer >= 0 and <= 3.", c);
                break;

            case DATETIME_SECOND_DAY:
                if (c >= '0' && c <= '9') {
                    tmp += c - diff;
                    if (tmp < 1 || tmp > 31) {
                        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                            "Invalid day. Found %d, must be >= 1 and <= 31.",
                            tmp);
                        break;
                    }
                    t.tm_mday = tmp;
                    state = DATETIME_SPACE;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid second digit of day. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_SPACE:
                if (c == ' ') {
                    tmp = 0;
                    state = DATETIME_FIRST_HOUR;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid separator between date and time. "
                    "Found '%c', must be ' ' (empty space).", c);
                break;

            case DATETIME_FIRST_HOUR:
                if (c >= '0' && c <= '2') {
                    tmp += (c - diff) * 10;
                    state = DATETIME_SECOND_HOUR;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid first digit of hours. "
                    "Found '%c', must be integer >= 0 and <= 2.", c);
                break;

            case DATETIME_SECOND_HOUR:
                if (c >= '0' && c <= '9') {
                    tmp += c - diff;
                    if (tmp < 0 || tmp > 23) {
                        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                            "Invalid hours. Found %d, must be >= 0 and <= 23.",
                            tmp);
                        break;
                    }
                    t.tm_hour = tmp;
                    state = DATETIME_FIRST_COLON;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid second digit of hours. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_FIRST_COLON:
                if (c == ':') {
                    tmp = 0;
                    state = DATETIME_FIRST_MINUTE;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid separator between hours and minutes. "
                    "Found '%c', must be ':'.", c);
                break;

            case DATETIME_FIRST_MINUTE:
                if (c >= '0' && c <= '5') {
                    tmp += (c - diff) * 10;
                    state = DATETIME_SECOND_MINUTE;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid first digit of minutes. "
                    "Found '%c', must be integer >= 0 and <= 5.", c);
                break;

            case DATETIME_SECOND_MINUTE:
                if (c >= '0' && c <= '9') {
                    tmp += c - diff;
                    if (tmp < 0 || tmp > 59) {
                        // this won't happen because we are restricting the digits
                        // to 00-59 already, but lets keep the code here for
                        // reference.
                        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                            "Invalid minutes. Found %d, must be >= 0 and <= 59.",
                            tmp);
                        break;
                    }
                    t.tm_min = tmp;
                    state = DATETIME_SECOND_COLON;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid second digit of minutes. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_SECOND_COLON:
                if (c == ':') {
                    tmp = 0;
                    state = DATETIME_FIRST_SECOND;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid separator between minutes and seconds. "
                    "Found '%c', must be ':'.", c);
                break;

            case DATETIME_FIRST_SECOND:
                if (c >= '0' && c <= '6') {
                    tmp += (c - diff) * 10;
                    state = DATETIME_SECOND_SECOND;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid first digit of seconds. "
                    "Found '%c', must be integer >= 0 and <= 6.", c);
                break;

            case DATETIME_SECOND_SECOND:
                if (c >= '0' && c <= '9') {
                    tmp += c - diff;
                    if (tmp < 0 || tmp > 60) {
                        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                            "Invalid seconds. Found %d, must be >= 0 and <= 60.",
                            tmp);
                        break;
                    }
                    t.tm_sec = tmp;
                    state = DATETIME_DONE;
                    break;
                }
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid second digit of seconds. "
                    "Found '%c', must be integer >= 0 and <= 9.", c);
                break;

            case DATETIME_DONE:
                // well, its done ;)
                break;
        }

        if (*err != NULL)
            return NULL;
    }

    if (*err == NULL) {
        switch (state) {
            case DATETIME_FIRST_YEAR:
            case DATETIME_SECOND_YEAR:
            case DATETIME_THIRD_YEAR:
            case DATETIME_FOURTH_YEAR:
            case DATETIME_FIRST_HYPHEN:
            case DATETIME_FIRST_MONTH:
            case DATETIME_SECOND_MONTH:
            case DATETIME_SECOND_HYPHEN:
            case DATETIME_FIRST_DAY:
            case DATETIME_SECOND_DAY:
            case DATETIME_FIRST_HOUR:
            case DATETIME_SECOND_HOUR:
            case DATETIME_FIRST_MINUTE:
            case DATETIME_SECOND_MINUTE:
            case DATETIME_FIRST_SECOND:
            case DATETIME_SECOND_SECOND:
                *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
                    "Invalid datetime string. "
                    "Found '%s', formats allowed are: 'yyyy-mm-dd hh:mm:ss', "
                    "'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and 'yyyy-mm-dd'.",
                    orig);
                return NULL;

            case DATETIME_SPACE:
            case DATETIME_FIRST_COLON:
            case DATETIME_SECOND_COLON:
            case DATETIME_DONE:
                break;  // these states are ok
        }
    }

    mktime(&t);

    char buf[1024];
    if (0 == strftime(buf, sizeof(buf), format, &t)) {
        *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER,
            "Failed to format DATE variable, FORMAT is too long: %s",
            format);
        return NULL;
    }

    return sb_strdup(buf);

#endif
}