]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/iso3307.c
SourceFormat Enforcement
[thirdparty/squid.git] / lib / iso3307.c
1 #include "squid.h"
2 #include "util.h"
3
4 #if HAVE_STDIO_H
5 #include <stdio.h>
6 #endif
7 #if HAVE_STRING_H
8 #include <string.h>
9 #endif
10 #if HAVE_CTYPE_H
11 #include <ctype.h>
12 #endif
13 #if HAVE_TIME_H
14 #include <time.h>
15 #endif
16
17 #define ASCII_DIGIT(c) ((c)-48)
18
19 time_t
20 parse_iso3307_time(const char *buf)
21 {
22 /* buf is an ISO 3307 style time: YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx */
23 struct tm tms;
24 time_t t;
25 while (*buf == ' ' || *buf == '\t')
26 buf++;
27 if ((int) strlen(buf) < 14)
28 return 0;
29 memset(&tms, '\0', sizeof(struct tm));
30 tms.tm_year = (ASCII_DIGIT(buf[0]) * 1000) + (ASCII_DIGIT(buf[1]) * 100) +
31 (ASCII_DIGIT(buf[2]) * 10) + ASCII_DIGIT(buf[3]) - 1900;
32 tms.tm_mon = (ASCII_DIGIT(buf[4]) * 10) + ASCII_DIGIT(buf[5]) - 1;
33 tms.tm_mday = (ASCII_DIGIT(buf[6]) * 10) + ASCII_DIGIT(buf[7]);
34 tms.tm_hour = (ASCII_DIGIT(buf[8]) * 10) + ASCII_DIGIT(buf[9]);
35 tms.tm_min = (ASCII_DIGIT(buf[10]) * 10) + ASCII_DIGIT(buf[11]);
36 tms.tm_sec = (ASCII_DIGIT(buf[12]) * 10) + ASCII_DIGIT(buf[13]);
37 #if HAVE_TIMEGM
38 t = timegm(&tms);
39 #elif HAVE_MKTIME
40 t = mktime(&tms);
41 #else
42 t = (time_t) 0;
43 #endif
44 return t;
45 }