Skip to content
Snippets Groups Projects
Commit ca87aad0 authored by Daniel Salzman's avatar Daniel Salzman
Browse files

Merge branch 'parser_time_units' into 'master'

yparser: new time unit specifiers

See merge request !1703
parents ef97313f fad9ce9f
No related branches found
No related tags found
1 merge request!1703yparser: new time unit specifiers
Pipeline #129275 passed
...@@ -20,9 +20,10 @@ the following symbols: ...@@ -20,9 +20,10 @@ the following symbols:
- ``STR`` – Textual string - ``STR`` – Textual string
- ``HEXSTR`` – Hexadecimal string (with ``0x`` prefix) - ``HEXSTR`` – Hexadecimal string (with ``0x`` prefix)
- ``BOOL`` – Boolean value (``on``/``off`` or ``true``/``false``) - ``BOOL`` – Boolean value (``on``/``off`` or ``true``/``false``)
- ``TIME`` – Number of seconds, an integer with possible time multiplier suffix - ``TIME`` – Number of seconds, an integer with a possible time multiplier suffix
(``s`` ~ 1, ``m`` ~ 60, ``h`` ~ 3600 or ``d`` ~ 24 * 3600) (``s`` ~ 1, ``m`` ~ 60, ``h`` ~ 3600, ``d`` ~ 24 * 3600, ``w`` ~ 7 * 24 * 3600,
- ``SIZE`` – Number of bytes, an integer with possible size multiplier suffix ``M`` ~ 30 * 24 * 3600, ``y`` ~ 365 * 24 * 3600)
- ``SIZE`` – Number of bytes, an integer with a possible size multiplier suffix
(``B`` ~ 1, ``K`` ~ 1024, ``M`` ~ 1024^2 or ``G`` ~ 1024^3) (``B`` ~ 1, ``K`` ~ 1024, ``M`` ~ 1024^2 or ``G`` ~ 1024^3)
- ``BASE64`` – Base64 encoded string - ``BASE64`` – Base64 encoded string
- ``ADDR`` – IPv4 or IPv6 address - ``ADDR`` – IPv4 or IPv6 address
......
/* Copyright (C) 2023 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -31,25 +31,31 @@ ...@@ -31,25 +31,31 @@
#include "contrib/wire_ctx.h" #include "contrib/wire_ctx.h"
enum { enum {
UNIT_BYTE = 'B', UNIT_BYTE = 'B',
UNIT_KILO = 'K', UNIT_KILO = 'K',
UNIT_MEGA = 'M', UNIT_MEGA = 'M',
UNIT_GIGA = 'G', UNIT_GIGA = 'G',
UNIT_SEC = 's', UNIT_SEC = 's',
UNIT_MIN = 'm', UNIT_MIN = 'm',
UNIT_HOUR = 'h', UNIT_HOUR = 'h',
UNIT_DAY = 'd' UNIT_DAY = 'd',
UNIT_WEEK = 'w',
UNIT_MONTH = 'M',
UNIT_YEAR = 'y',
}; };
enum { enum {
MULTI_BYTE = 1, MULTI_BYTE = 1,
MULTI_KILO = 1024, MULTI_KILO = 1024,
MULTI_MEGA = 1024 * 1024, MULTI_MEGA = 1024 * 1024,
MULTI_GIGA = 1024 * 1024 * 1024, MULTI_GIGA = 1024 * 1024 * 1024,
MULTI_SEC = 1, MULTI_SEC = 1,
MULTI_MIN = 60, MULTI_MIN = 60,
MULTI_HOUR = 3600, MULTI_HOUR = 3600,
MULTI_DAY = 24 * 3600 MULTI_DAY = 24 * 3600,
MULTI_WEEK = MULTI_DAY * 7,
MULTI_MONTH = MULTI_DAY * 30,
MULTI_YEAR = MULTI_DAY * 365,
}; };
static wire_ctx_t copy_in( static wire_ctx_t copy_in(
...@@ -186,6 +192,15 @@ static int remove_unit( ...@@ -186,6 +192,15 @@ static int remove_unit(
case UNIT_DAY: case UNIT_DAY:
multiplier = MULTI_DAY; multiplier = MULTI_DAY;
break; break;
case UNIT_WEEK:
multiplier = MULTI_WEEK;
break;
case UNIT_MONTH:
multiplier = MULTI_MONTH;
break;
case UNIT_YEAR:
multiplier = MULTI_YEAR;
break;
default: default:
return KNOT_EINVAL; return KNOT_EINVAL;
} }
...@@ -295,9 +310,18 @@ static void add_unit( ...@@ -295,9 +310,18 @@ static void add_unit(
} else if (*number < MULTI_DAY) { } else if (*number < MULTI_DAY) {
multiplier = MULTI_HOUR; multiplier = MULTI_HOUR;
new_unit = UNIT_HOUR; new_unit = UNIT_HOUR;
} else { } else if (*number < MULTI_WEEK) {
multiplier = MULTI_DAY; multiplier = MULTI_DAY;
new_unit = UNIT_DAY; new_unit = UNIT_DAY;
} else if (*number < MULTI_MONTH) {
multiplier = MULTI_WEEK;
new_unit = UNIT_WEEK;
} else if (*number < MULTI_YEAR) {
multiplier = MULTI_MONTH;
new_unit = UNIT_MONTH;
} else {
multiplier = MULTI_YEAR;
new_unit = UNIT_YEAR;
} }
} }
......
/* Copyright (C) 2023 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -326,7 +326,10 @@ int main(int argc, char *argv[]) ...@@ -326,7 +326,10 @@ int main(int argc, char *argv[])
int_test("11s", 11LL * 1, YP_STIME, min, max); int_test("11s", 11LL * 1, YP_STIME, min, max);
int_test("11m", 11LL * 60, YP_STIME, min, max); int_test("11m", 11LL * 60, YP_STIME, min, max);
int_test("11h", 11LL * 3600, YP_STIME, min, max); int_test("11h", 11LL * 3600, YP_STIME, min, max);
int_test("11d", 11LL * 24 * 3600, YP_STIME, min, max); int_test("6d", 6LL * 24 * 3600, YP_STIME, min, max);
int_test("4w", 4LL * 7 * 24 * 3600, YP_STIME, min, max);
int_test("11M", 11LL * 30 * 24 * 3600, YP_STIME, min, max);
int_test("2y", 2LL * 365 * 24 * 3600, YP_STIME, min, max);
int_test("1025B", 1025LL, YP_SSIZE, min, max); int_test("1025B", 1025LL, YP_SSIZE, min, max);
int_test("61s", 61LL, YP_STIME, min, max); int_test("61s", 61LL, YP_STIME, min, max);
int_bad_test("20000000001", KNOT_ERANGE, YP_SNONE, min, max); int_bad_test("20000000001", KNOT_ERANGE, YP_SNONE, min, max);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment