1 /*
2 * Copyright (c) 2002-2019 Balabit
3 * Copyright (c) 1998-2019 Balázs Scheidler
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * As an additional exemption you are allowed to compile & link against the
20 * OpenSSL libraries as published by the OpenSSL project. See the file
21 * COPYING for details.
22 *
23 */
24 #include "timeutils/format.h"
25 #include "timeutils/cache.h"
26 #include "timeutils/names.h"
27 #include "timeutils/conv.h"
28 #include "str-format.h"
29
30 static void
_append_frac_digits(glong usecs,GString * target,gint frac_digits)31 _append_frac_digits(glong usecs, GString *target, gint frac_digits)
32 {
33 usecs = usecs % 1000000;
34
35 if (frac_digits > 0)
36 {
37 gulong x;
38
39 g_string_append_c(target, '.');
40 for (x = 100000; frac_digits && x; x = x / 10)
41 {
42 g_string_append_c(target, (usecs / x) + '0');
43 usecs = usecs % x;
44 frac_digits--;
45 }
46 }
47 }
48
49 void
append_format_zone_info(GString * target,glong gmtoff)50 append_format_zone_info(GString *target, glong gmtoff)
51 {
52 g_string_append_c(target, gmtoff < 0 ? '-' : '+');
53 format_uint32_padded(target, 2, '0', 10, (gmtoff < 0 ? -gmtoff : gmtoff) / 3600);
54 g_string_append_c(target, ':');
55 format_uint32_padded(target, 2, '0', 10, ((gmtoff < 0 ? -gmtoff : gmtoff) % 3600) / 60);
56 }
57
58 void
append_format_unix_time(const UnixTime * ut,GString * target,gint ts_format,glong zone_offset,gint frac_digits)59 append_format_unix_time(const UnixTime *ut, GString *target, gint ts_format, glong zone_offset, gint frac_digits)
60 {
61 WallClockTime wct = WALL_CLOCK_TIME_INIT;
62
63 if (ts_format == TS_FMT_UNIX)
64 {
65 format_uint32_padded(target, 0, 0, 10, (int) ut->ut_sec);
66 _append_frac_digits(ut->ut_usec, target, frac_digits);
67 }
68 else
69 {
70 convert_unix_time_to_wall_clock_time_with_tz_override(ut, &wct, zone_offset);
71 append_format_wall_clock_time(&wct, target, ts_format, frac_digits);
72 }
73 }
74
75 void
format_unix_time(const UnixTime * stamp,GString * target,gint ts_format,glong zone_offset,gint frac_digits)76 format_unix_time(const UnixTime *stamp, GString *target, gint ts_format, glong zone_offset, gint frac_digits)
77 {
78 g_string_truncate(target, 0);
79 append_format_unix_time(stamp, target, ts_format, zone_offset, frac_digits);
80 }
81
82 /**
83 * unix_time_format:
84 * @stamp: Timestamp to format
85 * @target: Target storage for formatted timestamp
86 * @ts_format: Specifies basic timestamp format (TS_FMT_BSD, TS_FMT_ISO)
87 * @zone_offset: Specifies custom zone offset if @tz_convert == TZ_CNV_CUSTOM
88 *
89 * Emits the formatted version of @stamp into @target as specified by
90 * @ts_format and @tz_convert.
91 **/
92 void
append_format_wall_clock_time(const WallClockTime * wct,GString * target,gint ts_format,gint frac_digits)93 append_format_wall_clock_time(const WallClockTime *wct, GString *target, gint ts_format, gint frac_digits)
94 {
95 UnixTime ut = UNIX_TIME_INIT;
96
97 switch (ts_format)
98 {
99 case TS_FMT_BSD:
100 g_string_append_len(target, month_names_abbrev[wct->wct_mon], MONTH_NAME_ABBREV_LEN);
101 g_string_append_c(target, ' ');
102 format_uint32_padded(target, 2, ' ', 10, wct->wct_mday);
103 g_string_append_c(target, ' ');
104 format_uint32_padded(target, 2, '0', 10, wct->wct_hour);
105 g_string_append_c(target, ':');
106 format_uint32_padded(target, 2, '0', 10, wct->wct_min);
107 g_string_append_c(target, ':');
108 format_uint32_padded(target, 2, '0', 10, wct->wct_sec);
109 _append_frac_digits(wct->wct_usec, target, frac_digits);
110 break;
111 case TS_FMT_ISO:
112 format_uint32_padded(target, 0, 0, 10, wct->wct_year + 1900);
113 g_string_append_c(target, '-');
114 format_uint32_padded(target, 2, '0', 10, wct->wct_mon + 1);
115 g_string_append_c(target, '-');
116 format_uint32_padded(target, 2, '0', 10, wct->wct_mday);
117 g_string_append_c(target, 'T');
118 format_uint32_padded(target, 2, '0', 10, wct->wct_hour);
119 g_string_append_c(target, ':');
120 format_uint32_padded(target, 2, '0', 10, wct->wct_min);
121 g_string_append_c(target, ':');
122 format_uint32_padded(target, 2, '0', 10, wct->wct_sec);
123
124 _append_frac_digits(wct->wct_usec, target, frac_digits);
125 append_format_zone_info(target, wct->wct_gmtoff);
126 break;
127 case TS_FMT_FULL:
128 format_uint32_padded(target, 0, 0, 10, wct->wct_year + 1900);
129 g_string_append_c(target, ' ');
130 g_string_append_len(target, month_names_abbrev[wct->wct_mon], MONTH_NAME_ABBREV_LEN);
131 g_string_append_c(target, ' ');
132 format_uint32_padded(target, 2, ' ', 10, wct->wct_mday);
133 g_string_append_c(target, ' ');
134 format_uint32_padded(target, 2, '0', 10, wct->wct_hour);
135 g_string_append_c(target, ':');
136 format_uint32_padded(target, 2, '0', 10, wct->wct_min);
137 g_string_append_c(target, ':');
138 format_uint32_padded(target, 2, '0', 10, wct->wct_sec);
139
140 _append_frac_digits(wct->wct_usec, target, frac_digits);
141 break;
142 case TS_FMT_UNIX:
143 convert_wall_clock_time_to_unix_time(wct, &ut);
144 append_format_unix_time(&ut, target, TS_FMT_UNIX, wct->wct_gmtoff, frac_digits);
145 break;
146 default:
147 g_assert_not_reached();
148 break;
149 }
150 }
151