aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/serializer/validating/time-zone.cxx
blob: 15ab27ca7f9d9e82eec809664d349b6a595269d5 (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
// file      : xsde/cxx/serializer/validating/time-zone.cxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <stdio.h> // sprintf/snprintf

#include <xsde/cxx/config.hxx>

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

namespace xsde
{
  namespace cxx
  {
    namespace serializer
    {
      namespace validating
      {
        namespace bits
        {
          int
          serialize_time_zone (char* s, const time_zone& z)
          {
            // time-zone := Z|(+|-)HH:MM
            //

            short h = z.zone_hours ();
            short m = z.zone_minutes ();

            if (h == 0 && m == 0)
            {
              *s = 'Z';
              return 1;
            }
            else
            {
              const char* fmt = "+%.2u:%.2u";

              if (h < 0)
              {
                fmt = "-%.2u:%.2u";
                h = -h;
                m = -m;
              }

              if (h >= 0 && h <= 14 && m >= 0 && m <= 59)
              {
#ifdef XSDE_SNPRINTF
                int n = snprintf (s, 7, fmt, h, m);
#else
                int n = sprintf (s, fmt, h, m);
#endif
                if (n > 0 && n < 7)
                  return n;
              }

              return 0;
            }
          }
        }
      }
    }
  }
}