File: scan_httpdate.c

package info (click to toggle)
libowfat 0.22-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,148 kB
  • ctags: 976
  • sloc: ansic: 10,424; makefile: 42
file content (59 lines) | stat: -rw-r--r-- 1,385 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
#include "scan.h"
#include "byte.h"
#include "case.h"
#include <time.h>

static int parsetime(const char*c,struct tm* x) {
  unsigned long tmp;
  c+=scan_ulong(c,&tmp); x->tm_hour=tmp;
  if (*c!=':') return -1; ++c;
  c+=scan_ulong(c,&tmp); x->tm_min=tmp;
  if (*c!=':') return -1; ++c;
  c+=scan_ulong(c,&tmp); x->tm_sec=tmp;
  if (*c!=' ') return -1;
  return 0;
}

unsigned int scan_httpdate(const char *in,time_t *t) {
  struct tm x;
  int i;
  unsigned long tmp;
  const char* c;
  static const char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  if (!(c=in)) return 0;
  if (c[3]==',') c+=5; else
  if (c[6]==',') c+=8; else {
    c+=4;
    for (i=0; i<12; ++i) {
      if (case_equalb(c,3,months+i*3)) {
	x.tm_mon=i; break;
      }
    }
    c+=4; if (*c==' ') ++c;
    c+=scan_ulong(c,&tmp); x.tm_mday=tmp;
    ++c;
    if (parsetime(c,&x)) return 0;
    c+=9;
    c+=scan_ulong(c,&tmp); x.tm_year=tmp-1900;
    goto done;
  }
  c+=scan_ulong(c,&tmp); x.tm_mday=tmp;
  ++c;
  for (i=0; i<12; ++i)
    if (case_equalb(c,3,months+i*3)) {
      x.tm_mon=i; break;
    }
  c+=4;
  c+=scan_ulong(c,&tmp);
  if (tmp>1000) x.tm_year=tmp-1900; else
    if (tmp<70) x.tm_year=tmp+100; else
                x.tm_year=tmp;
  ++c;
  if (parsetime(c,&x)) return 0;
  c+=9;
  if (byte_equal(c,3,"GMT")) c+=3;
done:
  x.tm_wday=x.tm_yday=x.tm_isdst=0;
  *t=mktime(&x);
  return c-in;
}