netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [nft PATCH] meta: Rewrite hour_type_print()
@ 2019-11-12 18:59 Phil Sutter
  2019-11-12 21:39 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Sutter @ 2019-11-12 18:59 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

There was no point in this recursively called __hour_type_print_r() at
all, it takes only four lines of code to split the number of seconds
into hours, minutes and seconds.

While being at it, inverse the conditional to reduce indenting for the
largest part of the function's body. Also introduce SECONDS_PER_DAY
macro to avoid magic numbers.

Fixes: f8f32deda31df ("meta: Introduce new conditions 'time', 'day' and 'hour'")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/meta.c | 49 +++++++++++++++++++------------------------------
 1 file changed, 19 insertions(+), 30 deletions(-)

diff --git a/src/meta.c b/src/meta.c
index f54b818e4e911..69a897a926869 100644
--- a/src/meta.c
+++ b/src/meta.c
@@ -490,46 +490,35 @@ static void day_type_print(const struct expr *expr, struct output_ctx *octx)
 	return symbolic_constant_print(&day_type_tbl, expr, true, octx);
 }
 
-static void __hour_type_print_r(int hours, int minutes, int seconds, char *out, size_t buflen)
-{
-	if (minutes == 60)
-		return __hour_type_print_r(++hours, 0, seconds, out, buflen);
-	else if (minutes > 60)
-		return __hour_type_print_r((int) (minutes / 60), minutes % 60, seconds, out, buflen);
-
-	if (seconds == 60)
-		return __hour_type_print_r(hours, ++minutes, 0, out, buflen);
-	else if (seconds > 60)
-		return __hour_type_print_r(hours, (int) (seconds / 60), seconds % 60, out, buflen);
-
-	if (seconds == 0)
-		snprintf(out, buflen, "%02d:%02d", hours, minutes);
-	else
-		snprintf(out, buflen, "%02d:%02d:%02d", hours, minutes, seconds);
-}
+#define SECONDS_PER_DAY	(60 * 60 * 24)
 
 static void hour_type_print(const struct expr *expr, struct output_ctx *octx)
 {
-	uint32_t seconds = mpz_get_uint32(expr->value);
+	uint32_t seconds = mpz_get_uint32(expr->value), minutes, hours;
 	struct tm *cur_tm;
-	char out[32];
 	time_t ts;
 
-	if (!nft_output_seconds(octx)) {
-		/* Obtain current tm, so that we can add tm_gmtoff */
-		ts = time(NULL);
-		cur_tm = localtime(&ts);
+	if (nft_output_seconds(octx)) {
+		expr_basetype(expr)->print(expr, octx);
+		return;
+	}
 
-		if (cur_tm)
-			seconds = (seconds + cur_tm->tm_gmtoff) % 86400;
+	/* Obtain current tm, so that we can add tm_gmtoff */
+	ts = time(NULL);
+	cur_tm = localtime(&ts);
 
-		__hour_type_print_r(0, 0, seconds, out, sizeof(out));
-		nft_print(octx, "\"%s\"", out);
+	if (cur_tm)
+		seconds = (seconds + cur_tm->tm_gmtoff) % SECONDS_PER_DAY;
 
-		return;
-	}
+	minutes = seconds / 60;
+	seconds %= 60;
+	hours = minutes / 60;
+	minutes %= 60;
 
-	expr_basetype(expr)->print(expr, octx);
+	nft_print(octx, "\"%02d:%02d", hours, minutes);
+	if (seconds)
+		nft_print(octx, ":%02d", seconds);
+	nft_print(octx, "\"");
 }
 
 static struct error_record *hour_type_parse(struct parse_ctx *ctx,
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [nft PATCH] meta: Rewrite hour_type_print()
  2019-11-12 18:59 [nft PATCH] meta: Rewrite hour_type_print() Phil Sutter
@ 2019-11-12 21:39 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2019-11-12 21:39 UTC (permalink / raw)
  To: Phil Sutter; +Cc: netfilter-devel

On Tue, Nov 12, 2019 at 07:59:31PM +0100, Phil Sutter wrote:
> There was no point in this recursively called __hour_type_print_r() at
> all, it takes only four lines of code to split the number of seconds
> into hours, minutes and seconds.
> 
> While being at it, inverse the conditional to reduce indenting for the
> largest part of the function's body. Also introduce SECONDS_PER_DAY
> macro to avoid magic numbers.
> 
> Fixes: f8f32deda31df ("meta: Introduce new conditions 'time', 'day' and 'hour'")
> Signed-off-by: Phil Sutter <phil@nwl.cc>

Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-11-12 21:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-12 18:59 [nft PATCH] meta: Rewrite hour_type_print() Phil Sutter
2019-11-12 21:39 ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).