| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "env-util.h" |
| 6 |
#include "random-util.h" |
| 7 |
#include "serialize.h" |
| 8 |
#include "string-util.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "tests.h" |
| 11 |
#include "time-util.h" |
| 12 |
|
| 13 |
#define TRIAL 100u |
| 14 |
|
| 15 |
static void set_timezone(const char *tz) { |
| 16 |
ASSERT_OK(set_unset_env("TZ", tz, /* overwrite= */ true)); |
| 17 |
tzset(); |
| 18 |
log_info("TZ=%s, tzname[0]=%s, tzname[1]=%s", strna(getenv("TZ")), strempty(get_tzname(/* dst= */ false)), strempty(get_tzname(/* dst= */ true))); |
| 19 |
} |
| 20 |
|
| 21 |
TEST(parse_sec) { |
| 22 |
usec_t u; |
| 23 |
|
| 24 |
assert_se(parse_sec("5s", &u) >= 0); |
| 25 |
assert_se(u == 5 * USEC_PER_SEC); |
| 26 |
assert_se(parse_sec("5s500ms", &u) >= 0); |
| 27 |
assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC); |
| 28 |
assert_se(parse_sec(" 5s 500ms ", &u) >= 0); |
| 29 |
assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC); |
| 30 |
assert_se(parse_sec(" 5.5s ", &u) >= 0); |
| 31 |
assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC); |
| 32 |
assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0); |
| 33 |
assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500); |
| 34 |
assert_se(parse_sec(" .22s ", &u) >= 0); |
| 35 |
assert_se(u == 220 * USEC_PER_MSEC); |
| 36 |
assert_se(parse_sec(" .50y ", &u) >= 0); |
| 37 |
assert_se(u == USEC_PER_YEAR / 2); |
| 38 |
assert_se(parse_sec("2.5", &u) >= 0); |
| 39 |
assert_se(u == 2500 * USEC_PER_MSEC); |
| 40 |
assert_se(parse_sec(".7", &u) >= 0); |
| 41 |
assert_se(u == 700 * USEC_PER_MSEC); |
| 42 |
assert_se(parse_sec("23us", &u) >= 0); |
| 43 |
assert_se(u == 23); |
| 44 |
assert_se(parse_sec("23μs", &u) >= 0); /* greek small letter mu */ |
| 45 |
assert_se(u == 23); |
| 46 |
assert_se(parse_sec("23µs", &u) >= 0); /* micro symbol */ |
| 47 |
assert_se(u == 23); |
| 48 |
assert_se(parse_sec("infinity", &u) >= 0); |
| 49 |
assert_se(u == USEC_INFINITY); |
| 50 |
assert_se(parse_sec(" infinity ", &u) >= 0); |
| 51 |
assert_se(u == USEC_INFINITY); |
| 52 |
assert_se(parse_sec("+3.1s", &u) >= 0); |
| 53 |
assert_se(u == 3100 * USEC_PER_MSEC); |
| 54 |
assert_se(parse_sec("3.1s.2", &u) >= 0); |
| 55 |
assert_se(u == 3300 * USEC_PER_MSEC); |
| 56 |
assert_se(parse_sec("3.1 .2", &u) >= 0); |
| 57 |
assert_se(u == 3300 * USEC_PER_MSEC); |
| 58 |
assert_se(parse_sec("3.1 sec .2 sec", &u) >= 0); |
| 59 |
assert_se(u == 3300 * USEC_PER_MSEC); |
| 60 |
assert_se(parse_sec("3.1 sec 1.2 sec", &u) >= 0); |
| 61 |
assert_se(u == 4300 * USEC_PER_MSEC); |
| 62 |
|
| 63 |
assert_se(parse_sec(" xyz ", &u) < 0); |
| 64 |
assert_se(parse_sec("", &u) < 0); |
| 65 |
assert_se(parse_sec(" . ", &u) < 0); |
| 66 |
assert_se(parse_sec(" 5. ", &u) < 0); |
| 67 |
assert_se(parse_sec(".s ", &u) < 0); |
| 68 |
assert_se(parse_sec("-5s ", &u) < 0); |
| 69 |
assert_se(parse_sec("-0.3s ", &u) < 0); |
| 70 |
assert_se(parse_sec("-0.0s ", &u) < 0); |
| 71 |
assert_se(parse_sec("-0.-0s ", &u) < 0); |
| 72 |
assert_se(parse_sec("0.-0s ", &u) < 0); |
| 73 |
assert_se(parse_sec("3.-0s ", &u) < 0); |
| 74 |
assert_se(parse_sec(" infinity .7", &u) < 0); |
| 75 |
assert_se(parse_sec(".3 infinity", &u) < 0); |
| 76 |
assert_se(parse_sec("3.+1s", &u) < 0); |
| 77 |
assert_se(parse_sec("3. 1s", &u) < 0); |
| 78 |
assert_se(parse_sec("3.s", &u) < 0); |
| 79 |
assert_se(parse_sec("12.34.56", &u) < 0); |
| 80 |
assert_se(parse_sec("12..34", &u) < 0); |
| 81 |
assert_se(parse_sec("..1234", &u) < 0); |
| 82 |
assert_se(parse_sec("1234..", &u) < 0); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(parse_sec_fix_0) { |
| 86 |
usec_t u; |
| 87 |
|
| 88 |
assert_se(parse_sec_fix_0("5s", &u) >= 0); |
| 89 |
assert_se(u == 5 * USEC_PER_SEC); |
| 90 |
assert_se(parse_sec_fix_0("0s", &u) >= 0); |
| 91 |
assert_se(u == USEC_INFINITY); |
| 92 |
assert_se(parse_sec_fix_0("0", &u) >= 0); |
| 93 |
assert_se(u == USEC_INFINITY); |
| 94 |
assert_se(parse_sec_fix_0(" 0", &u) >= 0); |
| 95 |
assert_se(u == USEC_INFINITY); |
| 96 |
} |
| 97 |
|
| 98 |
TEST(parse_sec_def_infinity) { |
| 99 |
usec_t u; |
| 100 |
|
| 101 |
assert_se(parse_sec_def_infinity("5s", &u) >= 0); |
| 102 |
assert_se(u == 5 * USEC_PER_SEC); |
| 103 |
assert_se(parse_sec_def_infinity("", &u) >= 0); |
| 104 |
assert_se(u == USEC_INFINITY); |
| 105 |
assert_se(parse_sec_def_infinity(" ", &u) >= 0); |
| 106 |
assert_se(u == USEC_INFINITY); |
| 107 |
assert_se(parse_sec_def_infinity("0s", &u) >= 0); |
| 108 |
assert_se(u == 0); |
| 109 |
assert_se(parse_sec_def_infinity("0", &u) >= 0); |
| 110 |
assert_se(u == 0); |
| 111 |
assert_se(parse_sec_def_infinity(" 0", &u) >= 0); |
| 112 |
assert_se(u == 0); |
| 113 |
assert_se(parse_sec_def_infinity("-5s", &u) < 0); |
| 114 |
} |
| 115 |
|
| 116 |
TEST(parse_time) { |
| 117 |
usec_t u; |
| 118 |
|
| 119 |
assert_se(parse_time("5", &u, 1) >= 0); |
| 120 |
assert_se(u == 5); |
| 121 |
|
| 122 |
assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0); |
| 123 |
assert_se(u == 5 * USEC_PER_MSEC); |
| 124 |
|
| 125 |
assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0); |
| 126 |
assert_se(u == 5 * USEC_PER_SEC); |
| 127 |
|
| 128 |
assert_se(parse_time("5s", &u, 1) >= 0); |
| 129 |
assert_se(u == 5 * USEC_PER_SEC); |
| 130 |
|
| 131 |
assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0); |
| 132 |
assert_se(u == 5 * USEC_PER_SEC); |
| 133 |
|
| 134 |
assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0); |
| 135 |
assert_se(u == 5 * USEC_PER_SEC); |
| 136 |
|
| 137 |
assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE); |
| 138 |
assert_se(parse_time("1.1111111111111y", &u, 1) >= 0); |
| 139 |
} |
| 140 |
|
| 141 |
TEST(parse_nsec) { |
| 142 |
nsec_t u; |
| 143 |
|
| 144 |
assert_se(parse_nsec("5s", &u) >= 0); |
| 145 |
assert_se(u == 5 * NSEC_PER_SEC); |
| 146 |
assert_se(parse_nsec("5s500ms", &u) >= 0); |
| 147 |
assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); |
| 148 |
assert_se(parse_nsec(" 5s 500ms ", &u) >= 0); |
| 149 |
assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); |
| 150 |
assert_se(parse_nsec(" 5.5s ", &u) >= 0); |
| 151 |
assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); |
| 152 |
assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0); |
| 153 |
assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC); |
| 154 |
assert_se(parse_nsec(" .22s ", &u) >= 0); |
| 155 |
assert_se(u == 220 * NSEC_PER_MSEC); |
| 156 |
assert_se(parse_nsec(" .50y ", &u) >= 0); |
| 157 |
assert_se(u == NSEC_PER_YEAR / 2); |
| 158 |
assert_se(parse_nsec("2.5", &u) >= 0); |
| 159 |
assert_se(u == 2); |
| 160 |
assert_se(parse_nsec(".7", &u) >= 0); |
| 161 |
assert_se(u == 0); |
| 162 |
assert_se(parse_nsec("infinity", &u) >= 0); |
| 163 |
assert_se(u == NSEC_INFINITY); |
| 164 |
assert_se(parse_nsec(" infinity ", &u) >= 0); |
| 165 |
assert_se(u == NSEC_INFINITY); |
| 166 |
assert_se(parse_nsec("+3.1s", &u) >= 0); |
| 167 |
assert_se(u == 3100 * NSEC_PER_MSEC); |
| 168 |
assert_se(parse_nsec("3.1s.2", &u) >= 0); |
| 169 |
assert_se(u == 3100 * NSEC_PER_MSEC); |
| 170 |
assert_se(parse_nsec("3.1 .2s", &u) >= 0); |
| 171 |
assert_se(u == 200 * NSEC_PER_MSEC + 3); |
| 172 |
assert_se(parse_nsec("3.1 sec .2 sec", &u) >= 0); |
| 173 |
assert_se(u == 3300 * NSEC_PER_MSEC); |
| 174 |
assert_se(parse_nsec("3.1 sec 1.2 sec", &u) >= 0); |
| 175 |
assert_se(u == 4300 * NSEC_PER_MSEC); |
| 176 |
|
| 177 |
assert_se(parse_nsec(" xyz ", &u) < 0); |
| 178 |
assert_se(parse_nsec("", &u) < 0); |
| 179 |
assert_se(parse_nsec(" . ", &u) < 0); |
| 180 |
assert_se(parse_nsec(" 5. ", &u) < 0); |
| 181 |
assert_se(parse_nsec(".s ", &u) < 0); |
| 182 |
assert_se(parse_nsec(" infinity .7", &u) < 0); |
| 183 |
assert_se(parse_nsec(".3 infinity", &u) < 0); |
| 184 |
assert_se(parse_nsec("-5s ", &u) < 0); |
| 185 |
assert_se(parse_nsec("-0.3s ", &u) < 0); |
| 186 |
assert_se(parse_nsec("-0.0s ", &u) < 0); |
| 187 |
assert_se(parse_nsec("-0.-0s ", &u) < 0); |
| 188 |
assert_se(parse_nsec("0.-0s ", &u) < 0); |
| 189 |
assert_se(parse_nsec("3.-0s ", &u) < 0); |
| 190 |
assert_se(parse_nsec(" infinity .7", &u) < 0); |
| 191 |
assert_se(parse_nsec(".3 infinity", &u) < 0); |
| 192 |
assert_se(parse_nsec("3.+1s", &u) < 0); |
| 193 |
assert_se(parse_nsec("3. 1s", &u) < 0); |
| 194 |
assert_se(parse_nsec("3.s", &u) < 0); |
| 195 |
assert_se(parse_nsec("12.34.56", &u) < 0); |
| 196 |
assert_se(parse_nsec("12..34", &u) < 0); |
| 197 |
assert_se(parse_nsec("..1234", &u) < 0); |
| 198 |
assert_se(parse_nsec("1234..", &u) < 0); |
| 199 |
assert_se(parse_nsec("1111111111111y", &u) == -ERANGE); |
| 200 |
assert_se(parse_nsec("1.111111111111y", &u) >= 0); |
| 201 |
} |
| 202 |
|
| 203 |
static void test_format_timespan_one(usec_t x, usec_t accuracy) { |
| 204 |
char l[FORMAT_TIMESPAN_MAX]; |
| 205 |
const char *t; |
| 206 |
usec_t y; |
| 207 |
|
| 208 |
log_debug(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy); |
| 209 |
|
| 210 |
assert_se(t = format_timespan(l, sizeof l, x, accuracy)); |
| 211 |
log_debug(" = <%s>", t); |
| 212 |
|
| 213 |
assert_se(parse_sec(t, &y) >= 0); |
| 214 |
log_debug(" = "USEC_FMT, y); |
| 215 |
|
| 216 |
if (accuracy <= 0) |
| 217 |
accuracy = 1; |
| 218 |
|
| 219 |
assert_se(x / accuracy == y / accuracy); |
| 220 |
} |
| 221 |
|
| 222 |
static void test_format_timespan_accuracy(usec_t accuracy) { |
| 223 |
log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy); |
| 224 |
|
| 225 |
test_format_timespan_one(0, accuracy); |
| 226 |
test_format_timespan_one(1, accuracy); |
| 227 |
test_format_timespan_one(1*USEC_PER_SEC, accuracy); |
| 228 |
test_format_timespan_one(999*USEC_PER_MSEC, accuracy); |
| 229 |
test_format_timespan_one(1234567, accuracy); |
| 230 |
test_format_timespan_one(12, accuracy); |
| 231 |
test_format_timespan_one(123, accuracy); |
| 232 |
test_format_timespan_one(1234, accuracy); |
| 233 |
test_format_timespan_one(12345, accuracy); |
| 234 |
test_format_timespan_one(123456, accuracy); |
| 235 |
test_format_timespan_one(1234567, accuracy); |
| 236 |
test_format_timespan_one(12345678, accuracy); |
| 237 |
test_format_timespan_one(1200000, accuracy); |
| 238 |
test_format_timespan_one(1230000, accuracy); |
| 239 |
test_format_timespan_one(1234000, accuracy); |
| 240 |
test_format_timespan_one(1234500, accuracy); |
| 241 |
test_format_timespan_one(1234560, accuracy); |
| 242 |
test_format_timespan_one(1234567, accuracy); |
| 243 |
test_format_timespan_one(986087, accuracy); |
| 244 |
test_format_timespan_one(500 * USEC_PER_MSEC, accuracy); |
| 245 |
test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy); |
| 246 |
test_format_timespan_one(USEC_INFINITY, accuracy); |
| 247 |
} |
| 248 |
|
| 249 |
TEST(format_timespan) { |
| 250 |
test_format_timespan_accuracy(1); |
| 251 |
test_format_timespan_accuracy(USEC_PER_MSEC); |
| 252 |
test_format_timespan_accuracy(USEC_PER_SEC); |
| 253 |
|
| 254 |
/* See issue #23928. */ |
| 255 |
_cleanup_free_ char *buf = NULL; |
| 256 |
assert_se(buf = new(char, 5)); |
| 257 |
assert_se(buf == format_timespan(buf, 5, 100005, 1000)); |
| 258 |
} |
| 259 |
|
| 260 |
TEST(verify_timezone) { |
| 261 |
assert_se(verify_timezone("Europe/Berlin", LOG_DEBUG) == 0); |
| 262 |
assert_se(verify_timezone("Australia/Sydney", LOG_DEBUG) == 0); |
| 263 |
assert_se(verify_timezone("Europe/Do not exist", LOG_DEBUG) == -EINVAL); |
| 264 |
assert_se(verify_timezone("Europe/DoNotExist", LOG_DEBUG) == -ENOENT); |
| 265 |
assert_se(verify_timezone("/DoNotExist", LOG_DEBUG) == -EINVAL); |
| 266 |
assert_se(verify_timezone("DoNotExist/", LOG_DEBUG) == -EINVAL); |
| 267 |
} |
| 268 |
|
| 269 |
TEST(timezone_is_valid) { |
| 270 |
assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR)); |
| 271 |
assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR)); |
| 272 |
assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR)); |
| 273 |
} |
| 274 |
|
| 275 |
TEST(get_timezones) { |
| 276 |
_cleanup_strv_free_ char **zones = NULL; |
| 277 |
int r; |
| 278 |
|
| 279 |
r = get_timezones(&zones); |
| 280 |
assert_se(r == 0); |
| 281 |
|
| 282 |
STRV_FOREACH(zone, zones) { |
| 283 |
r = verify_timezone(*zone, LOG_ERR); |
| 284 |
log_debug_errno(r, "verify_timezone(\"%s\"): %m", *zone); |
| 285 |
assert_se(r >= 0 || r == -ENOENT); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
TEST(usec_add) { |
| 290 |
assert_se(usec_add(0, 0) == 0); |
| 291 |
assert_se(usec_add(1, 4) == 5); |
| 292 |
assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY); |
| 293 |
assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY); |
| 294 |
assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3); |
| 295 |
assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY); |
| 296 |
assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY); |
| 297 |
assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY); |
| 298 |
} |
| 299 |
|
| 300 |
TEST(usec_sub_unsigned) { |
| 301 |
assert_se(usec_sub_unsigned(0, 0) == 0); |
| 302 |
assert_se(usec_sub_unsigned(0, 2) == 0); |
| 303 |
assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0); |
| 304 |
assert_se(usec_sub_unsigned(1, 0) == 1); |
| 305 |
assert_se(usec_sub_unsigned(1, 1) == 0); |
| 306 |
assert_se(usec_sub_unsigned(1, 2) == 0); |
| 307 |
assert_se(usec_sub_unsigned(1, 3) == 0); |
| 308 |
assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0); |
| 309 |
assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1); |
| 310 |
assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2); |
| 311 |
assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3); |
| 312 |
assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1); |
| 313 |
assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0); |
| 314 |
assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0); |
| 315 |
assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY); |
| 316 |
assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY); |
| 317 |
assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY); |
| 318 |
assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY); |
| 319 |
} |
| 320 |
|
| 321 |
TEST(usec_sub_signed) { |
| 322 |
assert_se(usec_sub_signed(0, 0) == 0); |
| 323 |
assert_se(usec_sub_signed(4, 1) == 3); |
| 324 |
assert_se(usec_sub_signed(4, 4) == 0); |
| 325 |
assert_se(usec_sub_signed(4, 5) == 0); |
| 326 |
|
| 327 |
assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY); |
| 328 |
assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY); |
| 329 |
assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY); |
| 330 |
assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY); |
| 331 |
|
| 332 |
assert_se(usec_sub_signed(0, INT64_MAX) == 0); |
| 333 |
assert_se(usec_sub_signed(0, -INT64_MAX) == INT64_MAX); |
| 334 |
assert_se(usec_sub_signed(0, INT64_MIN) == (usec_t) INT64_MAX + 1); |
| 335 |
assert_se(usec_sub_signed(0, -(INT64_MIN+1)) == 0); |
| 336 |
|
| 337 |
assert_se(usec_sub_signed(USEC_INFINITY, INT64_MAX) == USEC_INFINITY); |
| 338 |
assert_se(usec_sub_signed(USEC_INFINITY, -INT64_MAX) == USEC_INFINITY); |
| 339 |
assert_se(usec_sub_signed(USEC_INFINITY, INT64_MIN) == USEC_INFINITY); |
| 340 |
assert_se(usec_sub_signed(USEC_INFINITY, -(INT64_MIN+1)) == USEC_INFINITY); |
| 341 |
|
| 342 |
assert_se(usec_sub_signed(USEC_INFINITY-1, INT64_MAX) == USEC_INFINITY-1-INT64_MAX); |
| 343 |
assert_se(usec_sub_signed(USEC_INFINITY-1, -INT64_MAX) == USEC_INFINITY); |
| 344 |
assert_se(usec_sub_signed(USEC_INFINITY-1, INT64_MIN) == USEC_INFINITY); |
| 345 |
assert_se(usec_sub_signed(USEC_INFINITY-1, -(INT64_MIN+1)) == USEC_INFINITY-1-((usec_t) (-(INT64_MIN+1)))); |
| 346 |
} |
| 347 |
|
| 348 |
TEST(format_timestamp) { |
| 349 |
for (unsigned i = 0; i < TRIAL; i++) { |
| 350 |
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; |
| 351 |
usec_t x, y; |
| 352 |
|
| 353 |
x = random_u64_range(USEC_TIMESTAMP_FORMATTABLE_MAX - USEC_PER_SEC) + USEC_PER_SEC; |
| 354 |
|
| 355 |
assert_se(format_timestamp(buf, sizeof(buf), x)); |
| 356 |
log_debug("%s", buf); |
| 357 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 358 |
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); |
| 359 |
|
| 360 |
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UNIX)); |
| 361 |
log_debug("%s", buf); |
| 362 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 363 |
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); |
| 364 |
|
| 365 |
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC)); |
| 366 |
log_debug("%s", buf); |
| 367 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 368 |
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); |
| 369 |
|
| 370 |
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US)); |
| 371 |
log_debug("%s", buf); |
| 372 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 373 |
assert_se(x == y); |
| 374 |
|
| 375 |
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US_UTC)); |
| 376 |
log_debug("%s", buf); |
| 377 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 378 |
assert_se(x == y); |
| 379 |
|
| 380 |
if (x > 2 * USEC_PER_DAY) { |
| 381 |
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_DATE)); |
| 382 |
log_debug("%s", buf); |
| 383 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 384 |
assert_se(y > usec_sub_unsigned(x, 2 * USEC_PER_DAY) && y < usec_add(x, 2 * USEC_PER_DAY)); |
| 385 |
} |
| 386 |
|
| 387 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 388 |
log_debug("%s", buf); |
| 389 |
assert_se(parse_timestamp(buf, &y) >= 0); |
| 390 |
|
| 391 |
/* The two calls above will run with a slightly different local time. Make sure we are in the same |
| 392 |
* range however, but give enough leeway that this is unlikely to explode. And of course, |
| 393 |
* format_timestamp_relative() scales the accuracy with the distance from the current time up to one |
| 394 |
* month, cover for that too. */ |
| 395 |
assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
static void test_format_timestamp_impl(usec_t x) { |
| 400 |
const char *xx = FORMAT_TIMESTAMP(x); |
| 401 |
ASSERT_NOT_NULL(xx); |
| 402 |
|
| 403 |
/* Because of the timezone change, format_timestamp() may set timezone that is currently unused. |
| 404 |
* E.g. Africa/Juba uses EAT since Sat Jan 15 10:00:00 2000 and until Sun Jan 31 20:59:59 2021, but |
| 405 |
* now CAT/CAST is used there (see zdump for more details). In such cases, format_timestamp() may set |
| 406 |
* the timezone used at the specified time (which happens when built with musl), but it may not match |
| 407 |
* the timezone currently used, thus we may not parse back the timestamp. */ |
| 408 |
|
| 409 |
const char *space; |
| 410 |
ASSERT_NOT_NULL(space = strrchr(xx, ' ')); |
| 411 |
|
| 412 |
const char *tz = space + 1; |
| 413 |
if (!streq_ptr(tz, get_tzname(/* dst= */ false)) && |
| 414 |
!streq_ptr(tz, get_tzname(/* dst= */ true)) && |
| 415 |
parse_gmtoff(tz, NULL) < 0) { |
| 416 |
|
| 417 |
log_warning("@" USEC_FMT " → %s, timezone '%s' is currently unused, ignoring.", x, xx, tz); |
| 418 |
|
| 419 |
/* Verify the generated string except for the timezone part. Of course, in most cases, parsed |
| 420 |
* time does not match with the input, hence only check if it is parsable. */ |
| 421 |
ASSERT_OK(parse_timestamp(strndupa_safe(xx, space - xx), NULL)); |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
usec_t y; |
| 426 |
ASSERT_OK(parse_timestamp(xx, &y)); |
| 427 |
const char *yy = FORMAT_TIMESTAMP(y); |
| 428 |
ASSERT_NOT_NULL(yy); |
| 429 |
|
| 430 |
usec_t x_sec = x / USEC_PER_SEC; |
| 431 |
usec_t y_sec = y / USEC_PER_SEC; |
| 432 |
|
| 433 |
if (x_sec == y_sec && streq(xx, yy)) |
| 434 |
return; /* Yay! */ |
| 435 |
|
| 436 |
/* When the timezone is built with rearguard being enabled (e.g. old Ubuntu and RHEL), the timezone |
| 437 |
* Africa/Windhoek may provide time shifted 1 hour from the original. See |
| 438 |
* https://github.com/systemd/systemd/issues/28472 and https://github.com/systemd/systemd/pull/35471. |
| 439 |
* Also, the same may happen on MSK timezone (e.g. Europe/Volgograd or Europe/Kirov). */ |
| 440 |
bool ignore = |
| 441 |
(streq_ptr(getenv("TZ"), "Africa/Windhoek") || |
| 442 |
STRPTR_IN_SET(get_tzname(/* dst= */ false), "CAT", "EAT", "MSK", "WET")) && |
| 443 |
(x_sec > y_sec ? x_sec - y_sec : y_sec - x_sec) == 3600; |
| 444 |
|
| 445 |
log_full(ignore ? LOG_WARNING : LOG_ERR, |
| 446 |
"@" USEC_FMT " → %s → @" USEC_FMT " → %s%s", |
| 447 |
x, xx, y, yy, |
| 448 |
ignore ? ", ignoring." : ""); |
| 449 |
|
| 450 |
ASSERT_TRUE(ignore); |
| 451 |
} |
| 452 |
|
| 453 |
static void test_format_timestamp_loop(void) { |
| 454 |
test_format_timestamp_impl(USEC_PER_DAY + USEC_PER_SEC); |
| 455 |
test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX_32BIT-1); |
| 456 |
test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX_32BIT); |
| 457 |
test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX-1); |
| 458 |
test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX); |
| 459 |
|
| 460 |
/* Two cases which trigger https://github.com/systemd/systemd/issues/28472 */ |
| 461 |
test_format_timestamp_impl(1504938962980066); |
| 462 |
test_format_timestamp_impl(1509482094632752); |
| 463 |
|
| 464 |
/* With tzdata-2025c, the timestamp (randomly?) fails on MSK time zone (e.g. Europe/Volgograd). */ |
| 465 |
test_format_timestamp_impl(1414277092997572); |
| 466 |
|
| 467 |
for (unsigned i = 0; i < TRIAL; i++) { |
| 468 |
usec_t x; |
| 469 |
|
| 470 |
x = random_u64_range(USEC_TIMESTAMP_FORMATTABLE_MAX - USEC_PER_SEC) + USEC_PER_SEC; |
| 471 |
test_format_timestamp_impl(x); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
TEST(FORMAT_TIMESTAMP) { |
| 476 |
test_format_timestamp_loop(); |
| 477 |
} |
| 478 |
|
| 479 |
static void test_format_timestamp_with_tz_one(const char *tz) { |
| 480 |
if (!timezone_is_valid(tz, LOG_DEBUG)) |
| 481 |
return; |
| 482 |
|
| 483 |
SAVE_TIMEZONE; |
| 484 |
set_timezone(tz); |
| 485 |
|
| 486 |
test_format_timestamp_loop(); |
| 487 |
} |
| 488 |
|
| 489 |
TEST(FORMAT_TIMESTAMP_with_tz) { |
| 490 |
_cleanup_strv_free_ char **timezones = NULL; |
| 491 |
|
| 492 |
test_format_timestamp_with_tz_one("UTC"); |
| 493 |
|
| 494 |
if (!slow_tests_enabled()) |
| 495 |
return (void) log_tests_skipped("slow tests are disabled"); |
| 496 |
|
| 497 |
assert_se(get_timezones(&timezones) >= 0); |
| 498 |
STRV_FOREACH(tz, timezones) |
| 499 |
test_format_timestamp_with_tz_one(*tz); |
| 500 |
} |
| 501 |
|
| 502 |
TEST(format_timestamp_relative_full) { |
| 503 |
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; |
| 504 |
usec_t x; |
| 505 |
|
| 506 |
/* Years and months */ |
| 507 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH); |
| 508 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 509 |
log_debug("%s", buf); |
| 510 |
ASSERT_STREQ(buf, "1 year 1 month ago"); |
| 511 |
|
| 512 |
x = now(CLOCK_MONOTONIC) + (1*USEC_PER_YEAR + 1.5*USEC_PER_MONTH); |
| 513 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_MONOTONIC, false)); |
| 514 |
log_debug("%s", buf); |
| 515 |
ASSERT_STREQ(buf, "1 year 1 month left"); |
| 516 |
|
| 517 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH); |
| 518 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 519 |
log_debug("%s", buf); |
| 520 |
ASSERT_STREQ(buf, "1 year 2 months ago"); |
| 521 |
|
| 522 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH); |
| 523 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 524 |
log_debug("%s", buf); |
| 525 |
ASSERT_STREQ(buf, "2 years 1 month ago"); |
| 526 |
|
| 527 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH); |
| 528 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 529 |
log_debug("%s", buf); |
| 530 |
ASSERT_STREQ(buf, "2 years 2 months ago"); |
| 531 |
|
| 532 |
/* Months and days */ |
| 533 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY); |
| 534 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 535 |
log_debug("%s", buf); |
| 536 |
ASSERT_STREQ(buf, "1 month 1 day ago"); |
| 537 |
|
| 538 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY); |
| 539 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 540 |
log_debug("%s", buf); |
| 541 |
ASSERT_STREQ(buf, "1 month 2 days ago"); |
| 542 |
|
| 543 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY); |
| 544 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 545 |
log_debug("%s", buf); |
| 546 |
ASSERT_STREQ(buf, "2 months 1 day ago"); |
| 547 |
|
| 548 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY); |
| 549 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 550 |
log_debug("%s", buf); |
| 551 |
ASSERT_STREQ(buf, "2 months 2 days ago"); |
| 552 |
|
| 553 |
/* Weeks and days */ |
| 554 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY); |
| 555 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 556 |
log_debug("%s", buf); |
| 557 |
ASSERT_STREQ(buf, "1 week 1 day ago"); |
| 558 |
|
| 559 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY); |
| 560 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 561 |
log_debug("%s", buf); |
| 562 |
ASSERT_STREQ(buf, "1 week 2 days ago"); |
| 563 |
|
| 564 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY); |
| 565 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 566 |
log_debug("%s", buf); |
| 567 |
ASSERT_STREQ(buf, "2 weeks 1 day ago"); |
| 568 |
|
| 569 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY); |
| 570 |
assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); |
| 571 |
log_debug("%s", buf); |
| 572 |
ASSERT_STREQ(buf, "2 weeks 2 days ago"); |
| 573 |
} |
| 574 |
|
| 575 |
TEST(format_timestamp_relative) { |
| 576 |
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; |
| 577 |
usec_t x; |
| 578 |
|
| 579 |
/* Only testing timestamps in the past so we don't need to add some delta to account for time passing |
| 580 |
* by while we are running the tests (unless we're running on potatoes and 24 hours somehow passes |
| 581 |
* between our call to now() and format_timestamp_relative's call to now()). */ |
| 582 |
|
| 583 |
/* Years and months */ |
| 584 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH); |
| 585 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 586 |
log_debug("%s", buf); |
| 587 |
ASSERT_STREQ(buf, "1 year 1 month ago"); |
| 588 |
|
| 589 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH); |
| 590 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 591 |
log_debug("%s", buf); |
| 592 |
ASSERT_STREQ(buf, "1 year 2 months ago"); |
| 593 |
|
| 594 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH); |
| 595 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 596 |
log_debug("%s", buf); |
| 597 |
ASSERT_STREQ(buf, "2 years 1 month ago"); |
| 598 |
|
| 599 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH); |
| 600 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 601 |
log_debug("%s", buf); |
| 602 |
ASSERT_STREQ(buf, "2 years 2 months ago"); |
| 603 |
|
| 604 |
/* Months and days */ |
| 605 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY); |
| 606 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 607 |
log_debug("%s", buf); |
| 608 |
ASSERT_STREQ(buf, "1 month 1 day ago"); |
| 609 |
|
| 610 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY); |
| 611 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 612 |
log_debug("%s", buf); |
| 613 |
ASSERT_STREQ(buf, "1 month 2 days ago"); |
| 614 |
|
| 615 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY); |
| 616 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 617 |
log_debug("%s", buf); |
| 618 |
ASSERT_STREQ(buf, "2 months 1 day ago"); |
| 619 |
|
| 620 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY); |
| 621 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 622 |
log_debug("%s", buf); |
| 623 |
ASSERT_STREQ(buf, "2 months 2 days ago"); |
| 624 |
|
| 625 |
/* Weeks and days */ |
| 626 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY); |
| 627 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 628 |
log_debug("%s", buf); |
| 629 |
ASSERT_STREQ(buf, "1 week 1 day ago"); |
| 630 |
|
| 631 |
x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY); |
| 632 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 633 |
log_debug("%s", buf); |
| 634 |
ASSERT_STREQ(buf, "1 week 2 days ago"); |
| 635 |
|
| 636 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY); |
| 637 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 638 |
log_debug("%s", buf); |
| 639 |
ASSERT_STREQ(buf, "2 weeks 1 day ago"); |
| 640 |
|
| 641 |
x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY); |
| 642 |
assert_se(format_timestamp_relative(buf, sizeof(buf), x)); |
| 643 |
log_debug("%s", buf); |
| 644 |
ASSERT_STREQ(buf, "2 weeks 2 days ago"); |
| 645 |
} |
| 646 |
|
| 647 |
static void test_format_timestamp_one(usec_t val, TimestampStyle style, const char *result) { |
| 648 |
char buf[FORMAT_TIMESTAMP_MAX]; |
| 649 |
const char *t; |
| 650 |
|
| 651 |
t = format_timestamp_style(buf, sizeof(buf), val, style); |
| 652 |
ASSERT_STREQ(t, result); |
| 653 |
} |
| 654 |
|
| 655 |
TEST(format_timestamp_range) { |
| 656 |
test_format_timestamp_one(0, TIMESTAMP_UTC, NULL); |
| 657 |
test_format_timestamp_one(0, TIMESTAMP_DATE, NULL); |
| 658 |
test_format_timestamp_one(0, TIMESTAMP_US_UTC, NULL); |
| 659 |
|
| 660 |
test_format_timestamp_one(1, TIMESTAMP_UTC, "Thu 1970-01-01 00:00:00 UTC"); |
| 661 |
test_format_timestamp_one(1, TIMESTAMP_DATE, "Thu 1970-01-01"); |
| 662 |
test_format_timestamp_one(1, TIMESTAMP_US_UTC, "Thu 1970-01-01 00:00:00.000001 UTC"); |
| 663 |
|
| 664 |
test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_UTC, "Thu 1970-01-01 00:00:01 UTC"); |
| 665 |
test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_DATE, "Thu 1970-01-01"); |
| 666 |
test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_US_UTC, "Thu 1970-01-01 00:00:01.000000 UTC"); |
| 667 |
|
| 668 |
#if SIZEOF_TIME_T == 8 |
| 669 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_UTC, "Thu 9999-12-30 23:59:59 UTC"); |
| 670 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_DATE, "Thu 9999-12-30"); |
| 671 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_UTC, "--- XXXX-XX-XX XX:XX:XX UTC"); |
| 672 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_US_UTC, "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC"); |
| 673 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_DATE, "--- XXXX-XX-XX"); |
| 674 |
#elif SIZEOF_TIME_T == 4 |
| 675 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_UTC, "Mon 2038-01-18 03:14:07 UTC"); |
| 676 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_DATE, "Mon 2038-01-18"); |
| 677 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_UTC, "--- XXXX-XX-XX XX:XX:XX UTC"); |
| 678 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_US_UTC, "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC"); |
| 679 |
test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_DATE, "--- XXXX-XX-XX"); |
| 680 |
#endif |
| 681 |
|
| 682 |
test_format_timestamp_one(USEC_INFINITY, TIMESTAMP_UTC, NULL); |
| 683 |
} |
| 684 |
|
| 685 |
TEST(parse_gmtoff) { |
| 686 |
long t; |
| 687 |
|
| 688 |
ASSERT_OK(parse_gmtoff("+14", &t)); |
| 689 |
ASSERT_EQ(t, (long) (14 * USEC_PER_HOUR / USEC_PER_SEC)); |
| 690 |
ASSERT_OK(parse_gmtoff("-09", &t)); |
| 691 |
ASSERT_EQ(t, - (long) (9 * USEC_PER_HOUR / USEC_PER_SEC)); |
| 692 |
ASSERT_OK(parse_gmtoff("+1400", &t)); |
| 693 |
ASSERT_EQ(t, (long) (14 * USEC_PER_HOUR / USEC_PER_SEC)); |
| 694 |
ASSERT_OK(parse_gmtoff("-0900", &t)); |
| 695 |
ASSERT_EQ(t, - (long) (9 * USEC_PER_HOUR / USEC_PER_SEC)); |
| 696 |
ASSERT_OK(parse_gmtoff("+14:00", &t)); |
| 697 |
ASSERT_EQ(t, (long) (14 * USEC_PER_HOUR / USEC_PER_SEC)); |
| 698 |
ASSERT_OK(parse_gmtoff("-09:00", &t)); |
| 699 |
ASSERT_EQ(t, - (long) (9 * USEC_PER_HOUR / USEC_PER_SEC)); |
| 700 |
|
| 701 |
ASSERT_ERROR(parse_gmtoff("", &t), EINVAL); |
| 702 |
ASSERT_ERROR(parse_gmtoff("UTC", &t), EINVAL); |
| 703 |
ASSERT_ERROR(parse_gmtoff("09", &t), EINVAL); |
| 704 |
ASSERT_ERROR(parse_gmtoff("0900", &t), EINVAL); |
| 705 |
ASSERT_ERROR(parse_gmtoff("?0900", &t), EINVAL); |
| 706 |
ASSERT_ERROR(parse_gmtoff("?0900", &t), EINVAL); |
| 707 |
ASSERT_ERROR(parse_gmtoff("+0900abc", &t), EINVAL); |
| 708 |
ASSERT_ERROR(parse_gmtoff("+0900 ", &t), EINVAL); |
| 709 |
ASSERT_ERROR(parse_gmtoff("+090000", &t), EINVAL); |
| 710 |
ASSERT_ERROR(parse_gmtoff("+0900:00", &t), EINVAL); |
| 711 |
ASSERT_ERROR(parse_gmtoff("+0900.00", &t), EINVAL); |
| 712 |
} |
| 713 |
|
| 714 |
static void test_parse_timestamp_one(const char *str, usec_t max_diff, usec_t expected) { |
| 715 |
usec_t usec = USEC_INFINITY; |
| 716 |
int r; |
| 717 |
|
| 718 |
r = parse_timestamp(str, &usec); |
| 719 |
log_debug("/* %s(%s): max_diff="USEC_FMT", expected="USEC_FMT", result="USEC_FMT" */", __func__, str, max_diff, expected, usec); |
| 720 |
assert_se(r >= 0); |
| 721 |
assert_se(usec >= expected); |
| 722 |
assert_se(usec_sub_unsigned(usec, expected) <= max_diff); |
| 723 |
} |
| 724 |
|
| 725 |
static bool time_is_zero(usec_t usec) { |
| 726 |
const char *s; |
| 727 |
|
| 728 |
s = FORMAT_TIMESTAMP(usec); |
| 729 |
return strstr(s, " 00:00:00 "); |
| 730 |
} |
| 731 |
|
| 732 |
static bool timezone_equal(usec_t today, usec_t target) { |
| 733 |
const char *s, *t, *sz, *tz; |
| 734 |
|
| 735 |
s = FORMAT_TIMESTAMP(today); |
| 736 |
t = FORMAT_TIMESTAMP(target); |
| 737 |
assert_se(sz = strrchr(s, ' ')); |
| 738 |
assert_se(tz = strrchr(t, ' ')); |
| 739 |
log_debug("%s("USEC_FMT", "USEC_FMT") -> %s, %s", __func__, today, target, s, t); |
| 740 |
return streq(sz, tz); |
| 741 |
} |
| 742 |
|
| 743 |
static void test_parse_timestamp_impl(const char *tz) { |
| 744 |
usec_t today, today2, now_usec; |
| 745 |
|
| 746 |
/* Invalid: Ensure that systemctl reboot --when=show and --when=cancel |
| 747 |
* will not result in ambiguities */ |
| 748 |
assert_se(parse_timestamp("show", NULL) == -EINVAL); |
| 749 |
assert_se(parse_timestamp("cancel", NULL) == -EINVAL); |
| 750 |
|
| 751 |
/* UTC */ |
| 752 |
test_parse_timestamp_one("Thu 1970-01-01 00:01 UTC", 0, USEC_PER_MINUTE); |
| 753 |
test_parse_timestamp_one("Thu 1970-01-01 00:00:01 UTC", 0, USEC_PER_SEC); |
| 754 |
test_parse_timestamp_one("Thu 1970-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000); |
| 755 |
test_parse_timestamp_one("Thu 1970-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000); |
| 756 |
|
| 757 |
test_parse_timestamp_one("Thu 70-01-01 00:01 UTC", 0, USEC_PER_MINUTE); |
| 758 |
test_parse_timestamp_one("Thu 70-01-01 00:00:01 UTC", 0, USEC_PER_SEC); |
| 759 |
test_parse_timestamp_one("Thu 70-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000); |
| 760 |
test_parse_timestamp_one("Thu 70-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000); |
| 761 |
|
| 762 |
test_parse_timestamp_one("1970-01-01 00:01 UTC", 0, USEC_PER_MINUTE); |
| 763 |
test_parse_timestamp_one("1970-01-01 00:00:01 UTC", 0, USEC_PER_SEC); |
| 764 |
test_parse_timestamp_one("1970-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000); |
| 765 |
test_parse_timestamp_one("1970-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000); |
| 766 |
|
| 767 |
test_parse_timestamp_one("70-01-01 00:01 UTC", 0, USEC_PER_MINUTE); |
| 768 |
test_parse_timestamp_one("70-01-01 00:00:01 UTC", 0, USEC_PER_SEC); |
| 769 |
test_parse_timestamp_one("70-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000); |
| 770 |
test_parse_timestamp_one("70-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000); |
| 771 |
|
| 772 |
/* Examples from RFC3339 */ |
| 773 |
test_parse_timestamp_one("1985-04-12T23:20:50.52Z", 0, 482196050 * USEC_PER_SEC + 520000); |
| 774 |
test_parse_timestamp_one("1996-12-19T16:39:57-08:00", 0, 851042397 * USEC_PER_SEC + 000000); |
| 775 |
test_parse_timestamp_one("1996-12-20T00:39:57Z", 0, 851042397 * USEC_PER_SEC + 000000); |
| 776 |
test_parse_timestamp_one("1990-12-31T23:59:60Z", 0, 662688000 * USEC_PER_SEC + 000000); |
| 777 |
test_parse_timestamp_one("1990-12-31T15:59:60-08:00", 0, 662688000 * USEC_PER_SEC + 000000); |
| 778 |
assert_se(parse_timestamp("1937-01-01T12:00:27.87+00:20", NULL) == -ERANGE); /* we don't support pre-epoch timestamps */ |
| 779 |
/* We accept timestamps without seconds as well */ |
| 780 |
test_parse_timestamp_one("1996-12-20T00:39Z", 0, (851042397 - 57) * USEC_PER_SEC + 000000); |
| 781 |
test_parse_timestamp_one("1990-12-31T15:59-08:00", 0, (662688000-60) * USEC_PER_SEC + 000000); |
| 782 |
/* We drop day-of-week before parsing the timestamp */ |
| 783 |
test_parse_timestamp_one("Thu 1970-01-01T00:01 UTC", 0, USEC_PER_MINUTE); |
| 784 |
test_parse_timestamp_one("Thu 1970-01-01T00:00:01 UTC", 0, USEC_PER_SEC); |
| 785 |
test_parse_timestamp_one("Thu 1970-01-01T00:01Z", 0, USEC_PER_MINUTE); |
| 786 |
test_parse_timestamp_one("Thu 1970-01-01T00:00:01Z", 0, USEC_PER_SEC); |
| 787 |
/* RFC3339-style timezones can be welded to all formats */ |
| 788 |
assert_se(parse_timestamp("today UTC", &today) == 0); |
| 789 |
assert_se(parse_timestamp("todayZ", &today2) == 0); |
| 790 |
assert_se(today == today2); |
| 791 |
assert_se(parse_timestamp("today +0200", &today) == 0); |
| 792 |
assert_se(parse_timestamp("today+02:00", &today2) == 0); |
| 793 |
assert_se(today == today2); |
| 794 |
|
| 795 |
/* https://ijmacd.github.io/rfc3339-iso8601/ */ |
| 796 |
test_parse_timestamp_one("2023-09-06 12:49:27-00:00", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 797 |
test_parse_timestamp_one("2023-09-06 12:49:27.284-00:00", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 798 |
test_parse_timestamp_one("2023-09-06 12:49:27.284029Z", 0, 1694004567 * USEC_PER_SEC + 284029); |
| 799 |
test_parse_timestamp_one("2023-09-06 12:49:27.284Z", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 800 |
test_parse_timestamp_one("2023-09-06 12:49:27.28Z", 0, 1694004567 * USEC_PER_SEC + 280000); |
| 801 |
test_parse_timestamp_one("2023-09-06 12:49:27.2Z", 0, 1694004567 * USEC_PER_SEC + 200000); |
| 802 |
test_parse_timestamp_one("2023-09-06 12:49:27Z", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 803 |
test_parse_timestamp_one("2023-09-06 14:49:27+02:00", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 804 |
test_parse_timestamp_one("2023-09-06 14:49:27.2+02:00", 0, 1694004567 * USEC_PER_SEC + 200000); |
| 805 |
test_parse_timestamp_one("2023-09-06 14:49:27.28+02:00", 0, 1694004567 * USEC_PER_SEC + 280000); |
| 806 |
test_parse_timestamp_one("2023-09-06 14:49:27.284+02:00", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 807 |
test_parse_timestamp_one("2023-09-06 14:49:27.284029+02:00", 0, 1694004567 * USEC_PER_SEC + 284029); |
| 808 |
test_parse_timestamp_one("2023-09-06T12:49:27+00:00", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 809 |
test_parse_timestamp_one("2023-09-06T12:49:27-00:00", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 810 |
test_parse_timestamp_one("2023-09-06T12:49:27.284+00:00", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 811 |
test_parse_timestamp_one("2023-09-06T12:49:27.284-00:00", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 812 |
test_parse_timestamp_one("2023-09-06T12:49:27.284029Z", 0, 1694004567 * USEC_PER_SEC + 284029); |
| 813 |
test_parse_timestamp_one("2023-09-06T12:49:27.284Z", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 814 |
test_parse_timestamp_one("2023-09-06T12:49:27.28Z", 0, 1694004567 * USEC_PER_SEC + 280000); |
| 815 |
test_parse_timestamp_one("2023-09-06T12:49:27.2Z", 0, 1694004567 * USEC_PER_SEC + 200000); |
| 816 |
test_parse_timestamp_one("2023-09-06T12:49:27Z", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 817 |
test_parse_timestamp_one("2023-09-06T14:49:27+02:00", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 818 |
test_parse_timestamp_one("2023-09-06T14:49:27.284+02:00", 0, 1694004567 * USEC_PER_SEC + 284000); |
| 819 |
test_parse_timestamp_one("2023-09-06T14:49:27.284029+02:00", 0, 1694004567 * USEC_PER_SEC + 284029); |
| 820 |
test_parse_timestamp_one("2023-09-06T21:34:27+08:45", 0, 1694004567 * USEC_PER_SEC + 000000); |
| 821 |
|
| 822 |
if (timezone_is_valid("Asia/Tokyo", LOG_DEBUG)) { |
| 823 |
/* Asia/Tokyo (+0900) */ |
| 824 |
test_parse_timestamp_one("Thu 1970-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE); |
| 825 |
test_parse_timestamp_one("Thu 1970-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC); |
| 826 |
test_parse_timestamp_one("Thu 1970-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 827 |
test_parse_timestamp_one("Thu 1970-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 828 |
|
| 829 |
test_parse_timestamp_one("Thu 70-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE); |
| 830 |
test_parse_timestamp_one("Thu 70-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC); |
| 831 |
test_parse_timestamp_one("Thu 70-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 832 |
test_parse_timestamp_one("Thu 70-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 833 |
|
| 834 |
test_parse_timestamp_one("1970-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE); |
| 835 |
test_parse_timestamp_one("1970-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC); |
| 836 |
test_parse_timestamp_one("1970-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 837 |
test_parse_timestamp_one("1970-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 838 |
|
| 839 |
test_parse_timestamp_one("70-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE); |
| 840 |
test_parse_timestamp_one("70-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC); |
| 841 |
test_parse_timestamp_one("70-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 842 |
test_parse_timestamp_one("70-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000); |
| 843 |
} |
| 844 |
|
| 845 |
if (streq_ptr(tz, "Asia/Tokyo")) { |
| 846 |
/* JST (+0900) */ |
| 847 |
test_parse_timestamp_one("Thu 1970-01-01 09:01 JST", 0, USEC_PER_MINUTE); |
| 848 |
test_parse_timestamp_one("Thu 1970-01-01 09:00:01 JST", 0, USEC_PER_SEC); |
| 849 |
test_parse_timestamp_one("Thu 1970-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000); |
| 850 |
test_parse_timestamp_one("Thu 1970-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000); |
| 851 |
|
| 852 |
test_parse_timestamp_one("Thu 70-01-01 09:01 JST", 0, USEC_PER_MINUTE); |
| 853 |
test_parse_timestamp_one("Thu 70-01-01 09:00:01 JST", 0, USEC_PER_SEC); |
| 854 |
test_parse_timestamp_one("Thu 70-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000); |
| 855 |
test_parse_timestamp_one("Thu 70-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000); |
| 856 |
|
| 857 |
test_parse_timestamp_one("1970-01-01 09:01 JST", 0, USEC_PER_MINUTE); |
| 858 |
test_parse_timestamp_one("1970-01-01 09:00:01 JST", 0, USEC_PER_SEC); |
| 859 |
test_parse_timestamp_one("1970-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000); |
| 860 |
test_parse_timestamp_one("1970-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000); |
| 861 |
|
| 862 |
test_parse_timestamp_one("70-01-01 09:01 JST", 0, USEC_PER_MINUTE); |
| 863 |
test_parse_timestamp_one("70-01-01 09:00:01 JST", 0, USEC_PER_SEC); |
| 864 |
test_parse_timestamp_one("70-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000); |
| 865 |
test_parse_timestamp_one("70-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000); |
| 866 |
} |
| 867 |
|
| 868 |
if (timezone_is_valid("America/New_York", LOG_DEBUG)) { |
| 869 |
/* America/New_York (-0500) */ |
| 870 |
test_parse_timestamp_one("Wed 1969-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE); |
| 871 |
test_parse_timestamp_one("Wed 1969-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC); |
| 872 |
test_parse_timestamp_one("Wed 1969-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000); |
| 873 |
test_parse_timestamp_one("Wed 1969-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000); |
| 874 |
|
| 875 |
test_parse_timestamp_one("Wed 69-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE); |
| 876 |
test_parse_timestamp_one("Wed 69-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC); |
| 877 |
test_parse_timestamp_one("Wed 69-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000); |
| 878 |
test_parse_timestamp_one("Wed 69-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000); |
| 879 |
|
| 880 |
test_parse_timestamp_one("1969-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE); |
| 881 |
test_parse_timestamp_one("1969-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC); |
| 882 |
test_parse_timestamp_one("1969-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000); |
| 883 |
test_parse_timestamp_one("1969-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000); |
| 884 |
|
| 885 |
test_parse_timestamp_one("69-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE); |
| 886 |
test_parse_timestamp_one("69-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC); |
| 887 |
test_parse_timestamp_one("69-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000); |
| 888 |
test_parse_timestamp_one("69-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000); |
| 889 |
} |
| 890 |
|
| 891 |
if (streq_ptr(tz, "America/New_York")) { |
| 892 |
/* EST (-0500) */ |
| 893 |
test_parse_timestamp_one("Wed 1969-12-31 19:01 EST", 0, USEC_PER_MINUTE); |
| 894 |
test_parse_timestamp_one("Wed 1969-12-31 19:00:01 EST", 0, USEC_PER_SEC); |
| 895 |
test_parse_timestamp_one("Wed 1969-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000); |
| 896 |
test_parse_timestamp_one("Wed 1969-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000); |
| 897 |
|
| 898 |
test_parse_timestamp_one("Wed 69-12-31 19:01 EST", 0, USEC_PER_MINUTE); |
| 899 |
test_parse_timestamp_one("Wed 69-12-31 19:00:01 EST", 0, USEC_PER_SEC); |
| 900 |
test_parse_timestamp_one("Wed 69-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000); |
| 901 |
test_parse_timestamp_one("Wed 69-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000); |
| 902 |
|
| 903 |
test_parse_timestamp_one("1969-12-31 19:01 EST", 0, USEC_PER_MINUTE); |
| 904 |
test_parse_timestamp_one("1969-12-31 19:00:01 EST", 0, USEC_PER_SEC); |
| 905 |
test_parse_timestamp_one("1969-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000); |
| 906 |
test_parse_timestamp_one("1969-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000); |
| 907 |
|
| 908 |
test_parse_timestamp_one("69-12-31 19:01 EST", 0, USEC_PER_MINUTE); |
| 909 |
test_parse_timestamp_one("69-12-31 19:00:01 EST", 0, USEC_PER_SEC); |
| 910 |
test_parse_timestamp_one("69-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000); |
| 911 |
test_parse_timestamp_one("69-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000); |
| 912 |
} |
| 913 |
|
| 914 |
if (timezone_is_valid("NZ", LOG_DEBUG)) { |
| 915 |
/* NZ (+1200) */ |
| 916 |
test_parse_timestamp_one("Thu 1970-01-01 12:01 NZ", 0, USEC_PER_MINUTE); |
| 917 |
test_parse_timestamp_one("Thu 1970-01-01 12:00:01 NZ", 0, USEC_PER_SEC); |
| 918 |
test_parse_timestamp_one("Thu 1970-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000); |
| 919 |
test_parse_timestamp_one("Thu 1970-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000); |
| 920 |
|
| 921 |
test_parse_timestamp_one("Thu 70-01-01 12:01 NZ", 0, USEC_PER_MINUTE); |
| 922 |
test_parse_timestamp_one("Thu 70-01-01 12:00:01 NZ", 0, USEC_PER_SEC); |
| 923 |
test_parse_timestamp_one("Thu 70-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000); |
| 924 |
test_parse_timestamp_one("Thu 70-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000); |
| 925 |
|
| 926 |
test_parse_timestamp_one("1970-01-01 12:01 NZ", 0, USEC_PER_MINUTE); |
| 927 |
test_parse_timestamp_one("1970-01-01 12:00:01 NZ", 0, USEC_PER_SEC); |
| 928 |
test_parse_timestamp_one("1970-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000); |
| 929 |
test_parse_timestamp_one("1970-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000); |
| 930 |
|
| 931 |
test_parse_timestamp_one("70-01-01 12:01 NZ", 0, USEC_PER_MINUTE); |
| 932 |
test_parse_timestamp_one("70-01-01 12:00:01 NZ", 0, USEC_PER_SEC); |
| 933 |
test_parse_timestamp_one("70-01-01 12:00:01.001 NZ", 0, USEC_PER_SEC + 1000); |
| 934 |
test_parse_timestamp_one("70-01-01 12:00:01.0010 NZ", 0, USEC_PER_SEC + 1000); |
| 935 |
} |
| 936 |
|
| 937 |
/* -06 */ |
| 938 |
test_parse_timestamp_one("Wed 1969-12-31 18:01 -06", 0, USEC_PER_MINUTE); |
| 939 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -06", 0, USEC_PER_SEC); |
| 940 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000); |
| 941 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000); |
| 942 |
|
| 943 |
test_parse_timestamp_one("Wed 69-12-31 18:01 -06", 0, USEC_PER_MINUTE); |
| 944 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01 -06", 0, USEC_PER_SEC); |
| 945 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000); |
| 946 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000); |
| 947 |
|
| 948 |
test_parse_timestamp_one("1969-12-31 18:01 -06", 0, USEC_PER_MINUTE); |
| 949 |
test_parse_timestamp_one("1969-12-31 18:00:01 -06", 0, USEC_PER_SEC); |
| 950 |
test_parse_timestamp_one("1969-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000); |
| 951 |
test_parse_timestamp_one("1969-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000); |
| 952 |
|
| 953 |
test_parse_timestamp_one("69-12-31 18:01 -06", 0, USEC_PER_MINUTE); |
| 954 |
test_parse_timestamp_one("69-12-31 18:00:01 -06", 0, USEC_PER_SEC); |
| 955 |
test_parse_timestamp_one("69-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000); |
| 956 |
test_parse_timestamp_one("69-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000); |
| 957 |
|
| 958 |
/* -0600 */ |
| 959 |
test_parse_timestamp_one("Wed 1969-12-31 18:01 -0600", 0, USEC_PER_MINUTE); |
| 960 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -0600", 0, USEC_PER_SEC); |
| 961 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000); |
| 962 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000); |
| 963 |
|
| 964 |
test_parse_timestamp_one("Wed 69-12-31 18:01 -0600", 0, USEC_PER_MINUTE); |
| 965 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01 -0600", 0, USEC_PER_SEC); |
| 966 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000); |
| 967 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000); |
| 968 |
|
| 969 |
test_parse_timestamp_one("1969-12-31 18:01 -0600", 0, USEC_PER_MINUTE); |
| 970 |
test_parse_timestamp_one("1969-12-31 18:00:01 -0600", 0, USEC_PER_SEC); |
| 971 |
test_parse_timestamp_one("1969-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000); |
| 972 |
test_parse_timestamp_one("1969-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000); |
| 973 |
|
| 974 |
test_parse_timestamp_one("69-12-31 18:01 -0600", 0, USEC_PER_MINUTE); |
| 975 |
test_parse_timestamp_one("69-12-31 18:00:01 -0600", 0, USEC_PER_SEC); |
| 976 |
test_parse_timestamp_one("69-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000); |
| 977 |
test_parse_timestamp_one("69-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000); |
| 978 |
|
| 979 |
/* -06:00 */ |
| 980 |
test_parse_timestamp_one("Wed 1969-12-31 18:01 -06:00", 0, USEC_PER_MINUTE); |
| 981 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -06:00", 0, USEC_PER_SEC); |
| 982 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000); |
| 983 |
test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000); |
| 984 |
|
| 985 |
test_parse_timestamp_one("Wed 69-12-31 18:01 -06:00", 0, USEC_PER_MINUTE); |
| 986 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01 -06:00", 0, USEC_PER_SEC); |
| 987 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000); |
| 988 |
test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000); |
| 989 |
|
| 990 |
test_parse_timestamp_one("1969-12-31 18:01 -06:00", 0, USEC_PER_MINUTE); |
| 991 |
test_parse_timestamp_one("1969-12-31 18:00:01 -06:00", 0, USEC_PER_SEC); |
| 992 |
test_parse_timestamp_one("1969-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000); |
| 993 |
test_parse_timestamp_one("1969-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000); |
| 994 |
|
| 995 |
test_parse_timestamp_one("69-12-31 18:01 -06:00", 0, USEC_PER_MINUTE); |
| 996 |
test_parse_timestamp_one("69-12-31 18:00:01 -06:00", 0, USEC_PER_SEC); |
| 997 |
test_parse_timestamp_one("69-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000); |
| 998 |
test_parse_timestamp_one("69-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000); |
| 999 |
|
| 1000 |
/* without date */ |
| 1001 |
assert_se(parse_timestamp("today", &today) == 0); |
| 1002 |
if (time_is_zero(today)) { |
| 1003 |
test_parse_timestamp_one("00:01", 0, today + USEC_PER_MINUTE); |
| 1004 |
test_parse_timestamp_one("00:00:01", 0, today + USEC_PER_SEC); |
| 1005 |
test_parse_timestamp_one("00:00:01.001", 0, today + USEC_PER_SEC + 1000); |
| 1006 |
test_parse_timestamp_one("00:00:01.0010", 0, today + USEC_PER_SEC + 1000); |
| 1007 |
|
| 1008 |
if (timezone_equal(today, today + USEC_PER_DAY) && time_is_zero(today + USEC_PER_DAY)) |
| 1009 |
test_parse_timestamp_one("tomorrow", 0, today + USEC_PER_DAY); |
| 1010 |
if (timezone_equal(today, today - USEC_PER_DAY) && time_is_zero(today - USEC_PER_DAY)) |
| 1011 |
test_parse_timestamp_one("yesterday", 0, today - USEC_PER_DAY); |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* with timezone */ |
| 1015 |
if (tz) { |
| 1016 |
_cleanup_free_ char *s = NULL; |
| 1017 |
|
| 1018 |
ASSERT_NOT_NULL((s = strjoin("Fri 2012-11-23 23:02:15 ", tz))); |
| 1019 |
ASSERT_OK(parse_timestamp(s, NULL)); |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* relative */ |
| 1023 |
assert_se(parse_timestamp("now", &now_usec) == 0); |
| 1024 |
test_parse_timestamp_one("+5hours", USEC_PER_MINUTE, now_usec + 5 * USEC_PER_HOUR); |
| 1025 |
if (now_usec >= 10 * USEC_PER_DAY) |
| 1026 |
test_parse_timestamp_one("-10days", USEC_PER_MINUTE, now_usec - 10 * USEC_PER_DAY); |
| 1027 |
test_parse_timestamp_one("2weeks left", USEC_PER_MINUTE, now_usec + 2 * USEC_PER_WEEK); |
| 1028 |
if (now_usec >= 30 * USEC_PER_MINUTE) |
| 1029 |
test_parse_timestamp_one("30minutes ago", USEC_PER_MINUTE, now_usec - 30 * USEC_PER_MINUTE); |
| 1030 |
} |
| 1031 |
|
| 1032 |
TEST(parse_timestamp) { |
| 1033 |
test_parse_timestamp_impl(NULL); |
| 1034 |
} |
| 1035 |
|
| 1036 |
static void test_parse_timestamp_with_tz_one(const char *tz) { |
| 1037 |
if (!timezone_is_valid(tz, LOG_DEBUG)) |
| 1038 |
return; |
| 1039 |
|
| 1040 |
SAVE_TIMEZONE; |
| 1041 |
set_timezone(tz); |
| 1042 |
|
| 1043 |
test_parse_timestamp_impl(tz); |
| 1044 |
} |
| 1045 |
|
| 1046 |
TEST(parse_timestamp_with_tz) { |
| 1047 |
_cleanup_strv_free_ char **timezones = NULL; |
| 1048 |
|
| 1049 |
test_parse_timestamp_with_tz_one("UTC"); |
| 1050 |
|
| 1051 |
if (!slow_tests_enabled()) |
| 1052 |
return (void) log_tests_skipped("slow tests are disabled"); |
| 1053 |
|
| 1054 |
assert_se(get_timezones(&timezones) >= 0); |
| 1055 |
STRV_FOREACH(tz, timezones) |
| 1056 |
test_parse_timestamp_with_tz_one(*tz); |
| 1057 |
} |
| 1058 |
|
| 1059 |
TEST(deserialize_dual_timestamp) { |
| 1060 |
int r; |
| 1061 |
dual_timestamp t; |
| 1062 |
|
| 1063 |
r = deserialize_dual_timestamp("1234 5678", &t); |
| 1064 |
assert_se(r == 0); |
| 1065 |
assert_se(t.realtime == 1234); |
| 1066 |
assert_se(t.monotonic == 5678); |
| 1067 |
|
| 1068 |
r = deserialize_dual_timestamp("1234x 5678", &t); |
| 1069 |
assert_se(r == -EINVAL); |
| 1070 |
|
| 1071 |
r = deserialize_dual_timestamp("1234 5678y", &t); |
| 1072 |
assert_se(r == -EINVAL); |
| 1073 |
|
| 1074 |
r = deserialize_dual_timestamp("-1234 5678", &t); |
| 1075 |
assert_se(r == -EINVAL); |
| 1076 |
|
| 1077 |
r = deserialize_dual_timestamp("1234 -5678", &t); |
| 1078 |
assert_se(r == -EINVAL); |
| 1079 |
|
| 1080 |
/* Check that output wasn't modified. */ |
| 1081 |
assert_se(t.realtime == 1234); |
| 1082 |
assert_se(t.monotonic == 5678); |
| 1083 |
|
| 1084 |
r = deserialize_dual_timestamp("+123 567", &t); |
| 1085 |
assert_se(r == 0); |
| 1086 |
assert_se(t.realtime == 123); |
| 1087 |
assert_se(t.monotonic == 567); |
| 1088 |
|
| 1089 |
/* Check that we get "infinity" on overflow. */ |
| 1090 |
r = deserialize_dual_timestamp("18446744073709551617 0", &t); |
| 1091 |
assert_se(r == 0); |
| 1092 |
assert_se(t.realtime == USEC_INFINITY); |
| 1093 |
assert_se(t.monotonic == 0); |
| 1094 |
} |
| 1095 |
|
| 1096 |
static void assert_similar(usec_t a, usec_t b) { |
| 1097 |
usec_t d; |
| 1098 |
|
| 1099 |
if (a > b) |
| 1100 |
d = a - b; |
| 1101 |
else |
| 1102 |
d = b - a; |
| 1103 |
|
| 1104 |
assert_se(d < 10*USEC_PER_SEC); |
| 1105 |
} |
| 1106 |
|
| 1107 |
TEST(usec_shift_clock) { |
| 1108 |
usec_t rt, mn, bt; |
| 1109 |
|
| 1110 |
rt = now(CLOCK_REALTIME); |
| 1111 |
mn = now(CLOCK_MONOTONIC); |
| 1112 |
bt = now(CLOCK_BOOTTIME); |
| 1113 |
|
| 1114 |
assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY); |
| 1115 |
|
| 1116 |
/* Silence static analyzers */ |
| 1117 |
assert_cc(9 * USEC_PER_HOUR <= USEC_INFINITY); |
| 1118 |
assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR); |
| 1119 |
assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_BOOTTIME), bt + 2*USEC_PER_HOUR); |
| 1120 |
assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR); |
| 1121 |
|
| 1122 |
assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR); |
| 1123 |
assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_BOOTTIME), bt + 5*USEC_PER_HOUR); |
| 1124 |
assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR); |
| 1125 |
|
| 1126 |
assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, CLOCK_BOOTTIME, CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR); |
| 1127 |
assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, CLOCK_BOOTTIME, CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR); |
| 1128 |
assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, CLOCK_BOOTTIME, CLOCK_BOOTTIME) == bt + 9*USEC_PER_HOUR); |
| 1129 |
|
| 1130 |
if (mn > USEC_PER_MINUTE) { |
| 1131 |
assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC); |
| 1132 |
assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, CLOCK_BOOTTIME), bt - 50 * USEC_PER_SEC); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
TEST(in_utc_timezone) { |
| 1137 |
SAVE_TIMEZONE; |
| 1138 |
|
| 1139 |
assert_se(setenv("TZ", "UTC", 1) >= 0); |
| 1140 |
assert_se(in_utc_timezone()); |
| 1141 |
ASSERT_STREQ(get_tzname(/* dst= */ false), "UTC"); |
| 1142 |
ASSERT_STREQ(get_tzname(/* dst= */ true), "UTC"); |
| 1143 |
assert_se(timezone == 0); |
| 1144 |
assert_se(daylight == 0); |
| 1145 |
|
| 1146 |
assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0); |
| 1147 |
assert_se(!in_utc_timezone()); |
| 1148 |
ASSERT_STREQ(get_tzname(/* dst= */ false), "CET"); |
| 1149 |
ASSERT_STREQ(get_tzname(/* dst= */ true), "CEST"); |
| 1150 |
} |
| 1151 |
|
| 1152 |
TEST(map_clock_usec) { |
| 1153 |
usec_t nowr, x, y, z; |
| 1154 |
|
| 1155 |
x = nowr = now(CLOCK_REALTIME); /* right now */ |
| 1156 |
y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC); |
| 1157 |
z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME); |
| 1158 |
/* Converting forth and back will introduce inaccuracies, since we cannot query both clocks atomically, but it should be small. Even on the slowest CI smaller than 1h */ |
| 1159 |
|
| 1160 |
assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR); |
| 1161 |
|
| 1162 |
assert_se(nowr < USEC_INFINITY - USEC_PER_DAY*7); /* overflow check */ |
| 1163 |
x = nowr + USEC_PER_DAY*7; /* 1 week from now */ |
| 1164 |
y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC); |
| 1165 |
assert_se(timestamp_is_set(y)); |
| 1166 |
z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME); |
| 1167 |
assert_se(timestamp_is_set(z)); |
| 1168 |
assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR); |
| 1169 |
|
| 1170 |
assert_se(nowr > USEC_PER_DAY * 7); /* underflow check */ |
| 1171 |
x = nowr - USEC_PER_DAY*7; /* 1 week ago */ |
| 1172 |
y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC); |
| 1173 |
if (y != 0) { /* might underflow if machine is not up long enough for the monotonic clock to be beyond 1w */ |
| 1174 |
assert_se(y < USEC_INFINITY); |
| 1175 |
z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME); |
| 1176 |
assert_se(timestamp_is_set(z)); |
| 1177 |
assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
static void test_timezone_offset_change_one(const char *utc, const char *pretty) { |
| 1182 |
usec_t x, y, z; |
| 1183 |
char *s; |
| 1184 |
|
| 1185 |
assert_se(parse_timestamp(utc, &x) >= 0); |
| 1186 |
|
| 1187 |
s = FORMAT_TIMESTAMP_STYLE(x, TIMESTAMP_UTC); |
| 1188 |
assert_se(parse_timestamp(s, &y) >= 0); |
| 1189 |
log_debug("%s -> " USEC_FMT " -> %s -> " USEC_FMT, utc, x, s, y); |
| 1190 |
ASSERT_STREQ(s, utc); |
| 1191 |
assert_se(x == y); |
| 1192 |
|
| 1193 |
assert_se(parse_timestamp(pretty, &y) >= 0); |
| 1194 |
s = FORMAT_TIMESTAMP_STYLE(y, TIMESTAMP_PRETTY); |
| 1195 |
assert_se(parse_timestamp(s, &z) >= 0); |
| 1196 |
log_debug("%s -> " USEC_FMT " -> %s -> " USEC_FMT, pretty, y, s, z); |
| 1197 |
ASSERT_STREQ(s, pretty); |
| 1198 |
assert_se(x == y); |
| 1199 |
assert_se(x == z); |
| 1200 |
} |
| 1201 |
|
| 1202 |
TEST(timezone_offset_change) { |
| 1203 |
SAVE_TIMEZONE; |
| 1204 |
|
| 1205 |
/* See issue #26370. */ |
| 1206 |
|
| 1207 |
if (timezone_is_valid("Africa/Casablanca", LOG_DEBUG)) { |
| 1208 |
set_timezone("Africa/Casablanca"); |
| 1209 |
|
| 1210 |
test_timezone_offset_change_one("Sun 2015-10-25 01:59:59 UTC", "Sun 2015-10-25 02:59:59 +01"); |
| 1211 |
test_timezone_offset_change_one("Sun 2015-10-25 02:00:00 UTC", "Sun 2015-10-25 02:00:00 +00"); |
| 1212 |
test_timezone_offset_change_one("Sun 2018-06-17 01:59:59 UTC", "Sun 2018-06-17 01:59:59 +00"); |
| 1213 |
test_timezone_offset_change_one("Sun 2018-06-17 02:00:00 UTC", "Sun 2018-06-17 03:00:00 +01"); |
| 1214 |
test_timezone_offset_change_one("Sun 2018-10-28 01:59:59 UTC", "Sun 2018-10-28 02:59:59 +01"); |
| 1215 |
test_timezone_offset_change_one("Sun 2018-10-28 02:00:00 UTC", "Sun 2018-10-28 03:00:00 +01"); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if (timezone_is_valid("Asia/Atyrau", LOG_DEBUG)) { |
| 1219 |
set_timezone("Asia/Atyrau"); |
| 1220 |
|
| 1221 |
test_timezone_offset_change_one("Sat 2004-03-27 21:59:59 UTC", "Sun 2004-03-28 01:59:59 +04"); |
| 1222 |
test_timezone_offset_change_one("Sat 2004-03-27 22:00:00 UTC", "Sun 2004-03-28 03:00:00 +05"); |
| 1223 |
test_timezone_offset_change_one("Sat 2004-10-30 21:59:59 UTC", "Sun 2004-10-31 02:59:59 +05"); |
| 1224 |
test_timezone_offset_change_one("Sat 2004-10-30 22:00:00 UTC", "Sun 2004-10-31 03:00:00 +05"); |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (timezone_is_valid("Chile/EasterIsland", LOG_DEBUG)) { |
| 1228 |
set_timezone("Chile/EasterIsland"); |
| 1229 |
|
| 1230 |
test_timezone_offset_change_one("Sun 1981-10-11 03:59:59 UTC", "Sat 1981-10-10 20:59:59 -07"); |
| 1231 |
test_timezone_offset_change_one("Sun 1981-10-11 04:00:00 UTC", "Sat 1981-10-10 22:00:00 -06"); |
| 1232 |
test_timezone_offset_change_one("Sun 1982-03-14 02:59:59 UTC", "Sat 1982-03-13 20:59:59 -06"); |
| 1233 |
test_timezone_offset_change_one("Sun 1982-03-14 03:00:00 UTC", "Sat 1982-03-13 21:00:00 -06"); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
static usec_t absdiff(usec_t a, usec_t b) { |
| 1238 |
return a > b ? a - b : b - a; |
| 1239 |
} |
| 1240 |
|
| 1241 |
TEST(mktime_or_timegm_usec) { |
| 1242 |
|
| 1243 |
usec_t n = now(CLOCK_REALTIME), m; |
| 1244 |
struct tm tm; |
| 1245 |
|
| 1246 |
assert_se(localtime_or_gmtime_usec(n, /* utc= */ false, &tm) >= 0); |
| 1247 |
assert_se(mktime_or_timegm_usec(&tm, /* utc= */ false, &m) >= 0); |
| 1248 |
assert_se(absdiff(n, m) < 2 * USEC_PER_DAY); |
| 1249 |
|
| 1250 |
assert_se(localtime_or_gmtime_usec(n, /* utc= */ true, &tm) >= 0); |
| 1251 |
assert_se(mktime_or_timegm_usec(&tm, /* utc= */ true, &m) >= 0); |
| 1252 |
assert_se(absdiff(n, m) < USEC_PER_SEC); |
| 1253 |
|
| 1254 |
/* This definitely should fail, because we refuse dates before the UNIX epoch */ |
| 1255 |
tm = (struct tm) { |
| 1256 |
.tm_mday = 15, |
| 1257 |
.tm_mon = 11, |
| 1258 |
.tm_year = 1969 - 1900, |
| 1259 |
}; |
| 1260 |
|
| 1261 |
assert_se(mktime_or_timegm_usec(&tm, /* utc= */ true, NULL) == -ERANGE); |
| 1262 |
} |
| 1263 |
|
| 1264 |
static int intro(void) { |
| 1265 |
/* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */ |
| 1266 |
assert_se(unsetenv("TZ") >= 0); |
| 1267 |
|
| 1268 |
log_info("realtime=" USEC_FMT "\n" |
| 1269 |
"monotonic=" USEC_FMT "\n" |
| 1270 |
"boottime=" USEC_FMT "\n", |
| 1271 |
now(CLOCK_REALTIME), |
| 1272 |
now(CLOCK_MONOTONIC), |
| 1273 |
now(CLOCK_BOOTTIME)); |
| 1274 |
|
| 1275 |
/* Ensure time_t is signed */ |
| 1276 |
assert_cc((time_t) -1 < (time_t) 1); |
| 1277 |
|
| 1278 |
/* Ensure TIME_T_MAX works correctly */ |
| 1279 |
uintmax_t x = TIME_T_MAX; |
| 1280 |
x++; |
| 1281 |
assert_se((time_t) x < 0); |
| 1282 |
|
| 1283 |
return EXIT_SUCCESS; |
| 1284 |
} |
| 1285 |
|
| 1286 |
TEST(parse_calendar_date) { |
| 1287 |
usec_t usec; |
| 1288 |
|
| 1289 |
/* Valid dates */ |
| 1290 |
ASSERT_OK(parse_calendar_date("2000-01-01", &usec)); |
| 1291 |
ASSERT_OK(parse_calendar_date("1970-01-01", &usec)); |
| 1292 |
ASSERT_EQ(usec, 0u); /* epoch */ |
| 1293 |
ASSERT_OK(parse_calendar_date("2000-02-29", &usec)); /* leap year */ |
| 1294 |
|
| 1295 |
/* NULL ret is allowed (validation only) */ |
| 1296 |
ASSERT_OK(parse_calendar_date("2000-06-15", NULL)); |
| 1297 |
|
| 1298 |
/* Non-normalized dates */ |
| 1299 |
ASSERT_ERROR(parse_calendar_date("2023-02-29", &usec), EINVAL); /* not a leap year */ |
| 1300 |
ASSERT_ERROR(parse_calendar_date("2023-04-31", &usec), EINVAL); /* April has 30 days */ |
| 1301 |
ASSERT_ERROR(parse_calendar_date("2023-13-01", &usec), EINVAL); /* month 13 */ |
| 1302 |
ASSERT_ERROR(parse_calendar_date("2023-00-01", &usec), EINVAL); /* month 0 */ |
| 1303 |
|
| 1304 |
/* Malformed input */ |
| 1305 |
ASSERT_ERROR(parse_calendar_date("", &usec), EINVAL); |
| 1306 |
ASSERT_ERROR(parse_calendar_date("not-a-date", &usec), EINVAL); |
| 1307 |
ASSERT_ERROR(parse_calendar_date("2023-06-15T00:00:00", &usec), EINVAL); /* trailing time */ |
| 1308 |
ASSERT_ERROR(parse_calendar_date("2023/06/15", &usec), EINVAL); /* wrong separator */ |
| 1309 |
ASSERT_ERROR(parse_calendar_date("06-15-2023", &usec), EINVAL); /* wrong order */ |
| 1310 |
} |
| 1311 |
|
| 1312 |
TEST(parse_birth_date) { |
| 1313 |
struct tm tm; |
| 1314 |
|
| 1315 |
/* Valid dates */ |
| 1316 |
ASSERT_OK(parse_birth_date("2000-06-15", &tm)); |
| 1317 |
ASSERT_EQ(tm.tm_year, 100); /* 2000 - 1900 */ |
| 1318 |
ASSERT_EQ(tm.tm_mon, 5); /* June, 0-indexed */ |
| 1319 |
ASSERT_EQ(tm.tm_mday, 15); |
| 1320 |
|
| 1321 |
/* Pre-epoch dates */ |
| 1322 |
ASSERT_OK(parse_birth_date("1960-03-25", &tm)); |
| 1323 |
ASSERT_EQ(tm.tm_year, 60); |
| 1324 |
ASSERT_EQ(tm.tm_mon, 2); |
| 1325 |
ASSERT_EQ(tm.tm_mday, 25); |
| 1326 |
|
| 1327 |
/* NULL ret is allowed (validation only) */ |
| 1328 |
ASSERT_OK(parse_birth_date("2000-01-01", NULL)); |
| 1329 |
|
| 1330 |
/* Non-date fields should not be relied upon */ |
| 1331 |
ASSERT_OK(parse_birth_date("2000-06-15", &tm)); |
| 1332 |
ASSERT_FALSE(BIRTH_DATE_IS_SET(BIRTH_DATE_UNSET)); |
| 1333 |
|
| 1334 |
/* Non-normalized dates */ |
| 1335 |
ASSERT_ERROR(parse_birth_date("2023-02-29", &tm), EINVAL); |
| 1336 |
ASSERT_ERROR(parse_birth_date("2023-04-31", &tm), EINVAL); |
| 1337 |
|
| 1338 |
/* Malformed input */ |
| 1339 |
ASSERT_ERROR(parse_birth_date("", &tm), EINVAL); |
| 1340 |
ASSERT_ERROR(parse_birth_date("not-a-date", &tm), EINVAL); |
| 1341 |
ASSERT_ERROR(parse_birth_date("2023-06-15T00:00:00", &tm), EINVAL); |
| 1342 |
} |
| 1343 |
|
| 1344 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |