]> git.ipfire.org Git - thirdparty/squid.git/blob - src/time/iso3307.cc
SourceLayout: Move time related tools to time/libtime.la (#1001)
[thirdparty/squid.git] / src / time / iso3307.cc
1 /*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "time/gadgets.h"
11
12 #if HAVE_STRING_H
13 #include <string.h>
14 #endif
15 #if HAVE_CTYPE_H
16 #include <ctype.h>
17 #endif
18
19 #define ASCII_DIGIT(c) ((c)-48)
20
21 time_t
22 Time::ParseIso3307(const char *buf)
23 {
24 /* buf is an ISO 3307 style time: YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx */
25 struct tm tms;
26 time_t t;
27 while (*buf == ' ' || *buf == '\t')
28 buf++;
29 if ((int) strlen(buf) < 14)
30 return 0;
31 memset(&tms, '\0', sizeof(struct tm));
32 tms.tm_year = (ASCII_DIGIT(buf[0]) * 1000) + (ASCII_DIGIT(buf[1]) * 100) +
33 (ASCII_DIGIT(buf[2]) * 10) + ASCII_DIGIT(buf[3]) - 1900;
34 tms.tm_mon = (ASCII_DIGIT(buf[4]) * 10) + ASCII_DIGIT(buf[5]) - 1;
35 tms.tm_mday = (ASCII_DIGIT(buf[6]) * 10) + ASCII_DIGIT(buf[7]);
36 tms.tm_hour = (ASCII_DIGIT(buf[8]) * 10) + ASCII_DIGIT(buf[9]);
37 tms.tm_min = (ASCII_DIGIT(buf[10]) * 10) + ASCII_DIGIT(buf[11]);
38 tms.tm_sec = (ASCII_DIGIT(buf[12]) * 10) + ASCII_DIGIT(buf[13]);
39 #if HAVE_TIMEGM
40 t = timegm(&tms);
41 #elif HAVE_MKTIME
42 t = mktime(&tms);
43 #else
44 t = (time_t) 0;
45 #endif
46 return t;
47 }
48