File: format.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (150 lines) | stat: -rw-r--r-- 5,404 bytes parent folder | download | duplicates (3)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (c) 2002-2019 Balabit
 * Copyright (c) 1998-2019 Balázs Scheidler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "timeutils/format.h"
#include "timeutils/cache.h"
#include "timeutils/names.h"
#include "timeutils/conv.h"
#include "str-format.h"

static void
_append_frac_digits(glong usecs, GString *target, gint frac_digits)
{
  usecs = usecs % 1000000;

  if (frac_digits > 0)
    {
      gulong x;

      g_string_append_c(target, '.');
      for (x = 100000; frac_digits && x; x = x / 10)
        {
          g_string_append_c(target, (usecs / x) + '0');
          usecs = usecs % x;
          frac_digits--;
        }
    }
}

void
append_format_zone_info(GString *target, glong gmtoff)
{
  g_string_append_c(target, gmtoff < 0 ? '-' : '+');
  format_uint32_padded(target, 2, '0', 10, (gmtoff < 0 ? -gmtoff : gmtoff) / 3600);
  g_string_append_c(target, ':');
  format_uint32_padded(target, 2, '0', 10, ((gmtoff < 0 ? -gmtoff : gmtoff) % 3600) / 60);
}

void
append_format_unix_time(const UnixTime *ut, GString *target, gint ts_format, glong zone_offset, gint frac_digits)
{
  WallClockTime wct = WALL_CLOCK_TIME_INIT;

  if (ts_format == TS_FMT_UNIX)
    {
      format_uint32_padded(target, 0, 0, 10, (int) ut->ut_sec);
      _append_frac_digits(ut->ut_usec, target, frac_digits);
    }
  else
    {
      convert_unix_time_to_wall_clock_time_with_tz_override(ut, &wct, zone_offset);
      append_format_wall_clock_time(&wct, target, ts_format, frac_digits);
    }
}

void
format_unix_time(const UnixTime *stamp, GString *target, gint ts_format, glong zone_offset, gint frac_digits)
{
  g_string_truncate(target, 0);
  append_format_unix_time(stamp, target, ts_format, zone_offset, frac_digits);
}

/**
 * unix_time_format:
 * @stamp: Timestamp to format
 * @target: Target storage for formatted timestamp
 * @ts_format: Specifies basic timestamp format (TS_FMT_BSD, TS_FMT_ISO)
 * @zone_offset: Specifies custom zone offset if @tz_convert == TZ_CNV_CUSTOM
 *
 * Emits the formatted version of @stamp into @target as specified by
 * @ts_format and @tz_convert.
 **/
void
append_format_wall_clock_time(const WallClockTime *wct, GString *target, gint ts_format, gint frac_digits)
{
  UnixTime ut = UNIX_TIME_INIT;

  switch (ts_format)
    {
    case TS_FMT_BSD:
      g_string_append_len(target, month_names_abbrev[wct->wct_mon], MONTH_NAME_ABBREV_LEN);
      g_string_append_c(target, ' ');
      format_uint32_padded(target, 2, ' ', 10, wct->wct_mday);
      g_string_append_c(target, ' ');
      format_uint32_padded(target, 2, '0', 10, wct->wct_hour);
      g_string_append_c(target, ':');
      format_uint32_padded(target, 2, '0', 10, wct->wct_min);
      g_string_append_c(target, ':');
      format_uint32_padded(target, 2, '0', 10, wct->wct_sec);
      _append_frac_digits(wct->wct_usec, target, frac_digits);
      break;
    case TS_FMT_ISO:
      format_uint32_padded(target, 0, 0, 10, wct->wct_year + 1900);
      g_string_append_c(target, '-');
      format_uint32_padded(target, 2, '0', 10, wct->wct_mon + 1);
      g_string_append_c(target, '-');
      format_uint32_padded(target, 2, '0', 10, wct->wct_mday);
      g_string_append_c(target, 'T');
      format_uint32_padded(target, 2, '0', 10, wct->wct_hour);
      g_string_append_c(target, ':');
      format_uint32_padded(target, 2, '0', 10, wct->wct_min);
      g_string_append_c(target, ':');
      format_uint32_padded(target, 2, '0', 10, wct->wct_sec);

      _append_frac_digits(wct->wct_usec, target, frac_digits);
      append_format_zone_info(target, wct->wct_gmtoff);
      break;
    case TS_FMT_FULL:
      format_uint32_padded(target, 0, 0, 10, wct->wct_year + 1900);
      g_string_append_c(target, ' ');
      g_string_append_len(target, month_names_abbrev[wct->wct_mon], MONTH_NAME_ABBREV_LEN);
      g_string_append_c(target, ' ');
      format_uint32_padded(target, 2, ' ', 10, wct->wct_mday);
      g_string_append_c(target, ' ');
      format_uint32_padded(target, 2, '0', 10, wct->wct_hour);
      g_string_append_c(target, ':');
      format_uint32_padded(target, 2, '0', 10, wct->wct_min);
      g_string_append_c(target, ':');
      format_uint32_padded(target, 2, '0', 10, wct->wct_sec);

      _append_frac_digits(wct->wct_usec, target, frac_digits);
      break;
    case TS_FMT_UNIX:
      convert_wall_clock_time_to_unix_time(wct, &ut);
      append_format_unix_time(&ut, target, TS_FMT_UNIX, wct->wct_gmtoff, frac_digits);
      break;
    default:
      g_assert_not_reached();
      break;
    }
}