aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/parser/validating/time-zone.cxx
blob: 1eecc7dc0123dfcb916274ed6aeb76d429ed8463 (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
// file      : xsde/cxx/parser/validating/time-zone.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/parser/validating/time-zone.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace parser
    {
      namespace validating
      {
        namespace bits
        {
          bool
          parse_time_zone (const char* s, size_t n, short& h, short& m)
          {
            // time_zone := Z|(+|-)HH:MM
            //
            if (n == 0)
            {
              return false;
            }
            else if (s[0] == 'Z')
            {
              if (n != 1)
                return false;

              h = 0;
              m = 0;
            }
            else
            {
              if (n != 6 || (s[0] != '-' && s[0] != '+') || s[3] != ':')
                return false;

              // Parse hours.
              //
              char d1 = s[1];
              char d2 = s[2];

              if (d1 < '0' || d1 > '9' || d2 < '0' || d2 > '9')
                return false;

              h = 10 * (d1 - '0') + (d2 - '0');

              if (h > 14)
                return false;

              // Parse minutes.
              //
              d1 = s[4];
              d2 = s[5];

              if (d1 < '0' || d1 > '9' || d2 < '0' || d2 > '9')
                return false;

              m = 10 * (d1 - '0') + (d2 - '0');

              if (m > 59 || (h == 14 && m != 0))
                return false;

              if (s[0] == '-')
              {
                h = -h;
                m = -m;
              }
            }

            return true;
          }
        }
      }
    }
  }
}