File: util_mit.cc

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (359 lines) | stat: -rw-r--r-- 11,153 bytes parent folder | download
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
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112

// This file was derived from the libwww source code of 1998/08/20. The
// copyright for the source of this derivative work can be found in the file
// COPYRIGHT_W3C.


#include "config.h"

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctype.h>

#ifndef TM_IN_SYS_TIME
#include <time.h>
#else
#include <sys/time.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>

#include "util_mit.h"

#include "debug.h"

#define _REENTRANT 1
#define MAX_TIME_STR_LEN 40 // one larger than the max rfc850 strlen. jhrg 3/7/22

using namespace std;

namespace libdap {

static const char * months[12] =
    {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };

#ifndef HAVE_STRFTIME
static const char * wkdays[7] =
    {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    };
#endif

/* Upper- and Lowercase macros

   The problem here is that toupper(x) is not defined officially unless
   isupper(x) is. These macros are CERTAINLY needed on #if defined(pyr) ||
   define(mips) or BDSI platforms. For safety, we make them mandatory.
*/

#ifndef TOLOWER
#define TOLOWER(c) tolower((int) (c))
#define TOUPPER(c) toupper((int) (c))
#endif

#if 0
static int
strncasecomp(const char *a, const char *b, int n)
{
    const char *p = a;
    const char *q = b;

    for (p = a, q = b;; p++, q++) {
        int diff;
        if (p == a + n) return 0; /*   Match up to n characters */
        if (!(*p && *q)) return *p - *q;
        diff = TOLOWER(*p) - TOLOWER(*q);
        if (diff) return diff;
    }
    /*NOTREACHED*/
    return -1; // silence gcc
}
#endif

static int
make_month(char * s, char ** ends)
{
    char * ptr = s;
    while (!isalpha((int) *ptr)) ptr++;
    if (*ptr) {
        int i;
        *ends = ptr + 3;
        for (i = 0; i < 12; i++)
            if (!strncasecmp(months[i], ptr, 3)) return i;
    }
    return 0;
}

#if !defined(HAVE_TIMEGM) && defined(HAVE_MKTIME)
static time_t
offset_from_utc()
{
    // Compute offset between localtime and GMT.
    time_t offset;
    time_t now = time(0);
#ifdef _REENTRANT
    struct tm gmt, local;
    offset = mktime(gmtime_r(&now, &gmt)) - mktime(localtime_r(&now, &local));
#else
    offset = mktime(gmtime(&now)) - mktime(localtime(&now));
#endif // _REENTRANT
    return offset;
}
#endif

/** Parse a string in GMT format to a local time time_t representation
    Four formats are accepted:
         Wkd, 00 Mon 0000 00:00:00 GMT  (rfc1123)
         Weekday, 00-Mon-00 00:00:00 GMT  (rfc850)
         Wkd Mon 00 00:00:00 0000 GMT  (ctime)
         1*DIGIT     (delta-seconds)

    Copied from libwww. 09/19/02 jhrg

    @param str The time string.
    @param expand If the time is given in delta seconds, adjust it to seconds
    since midnight 1 Jan 1970 if this is true. If false, simply convert the
    string to a time_t and return. The default value is true.
    @return The time in seconds since midnight 1 Jan 1970. Return 0 if
    an error is detected.
    */
time_t
parse_time(const char * str, bool expand)
{
    char * s;
    struct tm tm;
    time_t t;

    if (!str) return 0;

    if ((s = (char *)strchr(str, ','))) {  /* Thursday, 10-Jun-93 01:29:59 GMT */
        s++;    /* or: Thu, 10 Jan 1993 01:29:59 GMT */
        while (*s && *s == ' ') s++;
        if (strchr(s, '-')) {         /* First format */
            DBG(cerr << "Format...... Weekday, 00-Mon-00 00:00:00 GMT" << endl);
            if ((int)strnlen(s, MAX_TIME_STR_LEN) < 18) {
                DBG(cerr << "ERROR....... Not a valid time format \"" << s << "\"" << endl);
                return 0;
            }
            tm.tm_mday = strtol(s, &s, 10);
            tm.tm_mon = make_month(s, &s);
            ++s;
            tm.tm_year = strtol(s, &s, 10);
            tm.tm_hour = strtol(s, &s, 10);
            ++s;
            tm.tm_min = strtol(s, &s, 10);
            ++s;
            tm.tm_sec = strtol(s, &s, 10);
        }
        else {         /* Second format */
            DBG(cerr << "Format...... Wkd, 00 Mon 0000 00:00:00 GMT" << endl);
            if ((int)strnlen(s, MAX_TIME_STR_LEN) < 20) {
                DBG(cerr << "ERROR....... Not a valid time format \""
                    << s << "\"" << endl);
                return 0;
            }
            tm.tm_mday = strtol(s, &s, 10);
            tm.tm_mon = make_month(s, &s);
            tm.tm_year = strtol(s, &s, 10) - 1900;
            tm.tm_hour = strtol(s, &s, 10);
            ++s;
            tm.tm_min = strtol(s, &s, 10);
            ++s;
            tm.tm_sec = strtol(s, &s, 10);
        }
    }
    else if (isdigit((int) *str)) {
        if (strchr(str, 'T')) { /* ISO (limited format) date string */
            DBG(cerr << "Format...... YYYY.MM.DDThh:mmStzWkd" << endl);
            s = (char *) str;
            while (*s && *s == ' ') s++;
            if ((int)strnlen(s, MAX_TIME_STR_LEN) < 21) {
                DBG(cerr << "ERROR....... Not a valid time format \"" << s << "\"" << endl);
                return 0;
            }
            tm.tm_year = strtol(s, &s, 10) - 1900;
            ++s;
            tm.tm_mon  = strtol(s, &s, 10); tm.tm_mon--; // tm_mon is zero-based
            ++s;
            tm.tm_mday = strtol(s, &s, 10);
            ++s;
            tm.tm_hour = strtol(s, &s, 10);
            ++s;
            tm.tm_min  = strtol(s, &s, 10);
            ++s;
            tm.tm_sec  = strtol(s, &s, 10);
        }
        else {         /* delta seconds */
            t = expand ? time(NULL) + atol(str) : atol(str);
            return t;
        }
    }
    else {       /* Try the other format:  Wed Jun  9 01:29:59 1993 GMT */
        DBG(cerr << "Format...... Wkd Mon 00 00:00:00 0000 GMT" << endl);
        s = (char *) str;
        while (*s && *s == ' ') s++;
        DBG(cerr << "Trying...... The Wrong time format: " << s << endl);
        if ((int)strnlen(s, MAX_TIME_STR_LEN) < 24) {
            DBG(cerr << "ERROR....... Not a valid time format \"" << s << "\"" << endl);
            return 0;
        }
        tm.tm_mon = make_month(s, &s);
        tm.tm_mday = strtol(s, &s, 10);
        tm.tm_hour = strtol(s, &s, 10);
        ++s;
        tm.tm_min = strtol(s, &s, 10);
        ++s;
        tm.tm_sec = strtol(s, &s, 10);
        tm.tm_year = strtol(s, &s, 10) - 1900;
    }

    if (tm.tm_sec  < 0  ||  tm.tm_sec  > 59  ||
        tm.tm_min  < 0  ||  tm.tm_min  > 59  ||
        tm.tm_hour < 0  ||  tm.tm_hour > 23  ||
        tm.tm_mday < 1  ||  tm.tm_mday > 31  ||
        tm.tm_mon  < 0  ||  tm.tm_mon  > 11  ||
        tm.tm_year < 70  ||  tm.tm_year > 138) {        // Unix time will break iin 2038
        DBG(cerr << "ERROR....... Parsed illegal time" << endl);
        return 0;
    }

    /* Let mktime decide whether we have DST or not */
    tm.tm_isdst = -1;

#if defined(HAVE_TIMEGM)
    return timegm(&tm);
#elif defined(HAVE_MKTIME)
    return mktime(&tm) + offset_from_utc();
#else
#error "Neither mktime nor timegm defined"
#endif // HAVE_TIMEGM OR HAVE_MKTIME
}

/** Given a time in seconds since midnight 1 Jan 1970, return the RFC 1123
    date string. Example result string: Sun, 06 Nov 1994 08:49:37 GMT


    @param calendar Time in seconds
    @param local If true, return the local time, if false return GMT. The
    default value is false.
    @return A RFC 1123 date string. */

string date_time_str(time_t *calendar, bool local)
{
    if (!calendar) return "";

    char buf[MAX_TIME_STR_LEN];

#ifdef HAVE_STRFTIME
    if (local) {
#if defined(_REENTRANT)  || defined(SOLARIS)
        struct tm loctime;
        localtime_r(calendar, &loctime);
        strftime(buf, MAX_TIME_STR_LEN, "%a, %d %b %Y %H:%M:%S", &loctime);
#else
        struct tm *loctime = localtime(calendar);
        strftime(buf, MAX_TIME_STR_LEN, "%a, %d %b %Y %H:%M:%S", loctime);
#endif /* SOLARIS || _REENTRANT */
    }
    else {
#if defined(_REENTRANT)  || defined(SOLARIS)
        struct tm gmt;
        gmtime_r(calendar, &gmt);
        strftime(buf, MAX_TIME_STR_LEN, "%a, %d %b %Y %H:%M:%S GMT", &gmt);
#else
        struct tm *gmt = gmtime(calendar);
        strftime(buf, MAX_TIME_STR_LEN, "%a, %d %b %Y %H:%M:%S GMT", gmt);
#endif /* SOLARIS || _REENTRANT */
    }

#else  /* !HAVE_STRFTIME */

    if (local) {
#if defined(_REENTRANT)
        struct tm loctime;
        localtime_r(calendar, &loctime);
        snprintf(buf, MAX_TIME_STR_LEN, "%s, %02d %s %04d %02d:%02d:%02d",
                wkdays[loctime.tm_wday],
                loctime.tm_mday,
                months[loctime.tm_mon],
                loctime.tm_year + 1900,
                loctime.tm_hour,
                loctime.tm_min,
                loctime.tm_sec);
#else
        struct tm *loctime = localtime(calendar);
        if (!loctime)
            return "";
        snprintf(buf, MAX_TIME_STR_LEN, "%s, %02d %s %04d %02d:%02d:%02d",
                wkdays[loctime->tm_wday],
                loctime->tm_mday,
                months[loctime->tm_mon],
                loctime->tm_year + 1900,
                loctime->tm_hour,
                loctime->tm_min,
                loctime->tm_sec);
#endif /* _REENTRANT */
    }
    else {
#if defined(_REENTRANT) || defined(SOLARIS)
        struct tm gmt;
        gmtime_r(calendar, &gmt);
        snprintf(buf, MAX_TIME_STR_LEN, "%s, %02d %s %04d %02d:%02d:%02d GMT",
                wkdays[gmt.tm_wday],
                gmt.tm_mday,
                months[gmt.tm_mon],
                gmt.tm_year + 1900,
                gmt.tm_hour,
                gmt.tm_min,
                gmt.tm_sec);
#else
    struct tm *gmt = gmtime(calendar);
    if (!gmt)
    	return "";
    snprintf(buf, MAX_TIME_STR_LEN, "%s, %02d %s %04d %02d:%02d:%02d GMT",
            wkdays[gmt->tm_wday],
            gmt->tm_mday,
            months[gmt->tm_mon],
            gmt->tm_year + 1900,
            gmt->tm_hour,
            gmt->tm_min,
            gmt->tm_sec);
#endif // defined(_REENTRANT) || defined(SOLARIS)
    }
#endif
    return string(buf);
}

} // namespace libdap