[PATCH] TIME + DURATION -> TIME (was Re: [PATCH] Implement std.rfcdate)

Federico G. Schwindt fgsch at lodoss.net
Thu Oct 11 12:18:43 CEST 2012


On Thu, 11 Oct 2012 09:05:03 +0000
Poul-Henning Kamp <phk at critter.freebsd.dk> wrote:

> In message <CAJV_h0Y6rOZ79cGdtYY7mOgk8Y9R0k0R5c5vGvnY4nPfhhB+qg at mail.gmail.com>
> , Federico Schwindt writes:
> 
> >2. now and now + duration behave differently (string vs duration)
> >This is for consistency.
> 
> Then this is the problem we should fix.
> 
> Probably what we should do is move now() to vmod_std so it gets 
> a proper type.

Ok, see below.

f.-

 bin/varnishtest/tests/m00009.vtc       |   22 ++++++++++++++++++++++
 doc/sphinx/reference/vmod_std.rst      |   11 +++++++++++
 lib/libvcl/vcc_expr.c                  |    4 +++-
 lib/libvmod_std/vmod.vcc               |    1 +
 lib/libvmod_std/vmod_std_conversions.c |    6 ++++++
 5 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/bin/varnishtest/tests/m00009.vtc b/bin/varnishtest/tests/m00009.vtc
new file mode 100644
index 0000000..c1f4716
--- /dev/null
+++ b/bin/varnishtest/tests/m00009.vtc
@@ -0,0 +1,22 @@
+varnishtest "test vmod_std.unixtime conversion"
+
+server s1 {
+	rxreq
+	txresp
+} -start
+
+varnish v1 -vcl+backend {
+	import std from "${topbuild}/lib/libvmod_std/.libs/libvmod_std.so" ;
+	sub vcl_deliver {
+		set resp.http.now = now;
+		set resp.http.now_unixtime = std.unixtime(now);
+		set resp.http.future = now + 30m;
+		set resp.http.future_unixtime = std.unixtime(now + 30m);
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.status == 200
+} -run
diff --git a/doc/sphinx/reference/vmod_std.rst b/doc/sphinx/reference/vmod_std.rst
index 984331e..4215a79 100644
--- a/doc/sphinx/reference/vmod_std.rst
+++ b/doc/sphinx/reference/vmod_std.rst
@@ -138,6 +138,17 @@ Description
 Example
 	if (std.integer(beresp.http.x-foo, 0) > 5) { ... }
 
+unixtime
+--------
+Prototype
+	unixtime(TIME t)
+Return value
+	Real
+Description
+	Converts TIME *t* to a REAL number.
+Example
+	set beresp.http.x-unixtime = std.unixtime(now);
+
 collect
 -------
 Prototype
diff --git a/lib/libvcl/vcc_expr.c b/lib/libvcl/vcc_expr.c
index 72c26a9..d79608c 100644
--- a/lib/libvcl/vcc_expr.c
+++ b/lib/libvcl/vcc_expr.c
@@ -856,7 +856,9 @@ vcc_expr_add(struct vcc *tl, struct expr **e, enum var_type fmt)
 			vcc_ErrWhere2(tl, tk, tl->t);
 			return;
 		}
-		if (tk->tok == '+')
+		if (tk->tok == '+' && (*e)->fmt == TIME)
+			*e = vcc_expr_edit(TIME, "(\v1+\v2)", *e, e2);
+		else if (tk->tok == '+')
 			*e = vcc_expr_edit(f2, "(\v1+\v2)", *e, e2);
 		else if (f2 == TIME && e2->fmt == TIME)
 			*e = vcc_expr_edit(DURATION, "(\v1-\v2)", *e, e2);
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index 3238164..4cc9e5a 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -36,3 +36,4 @@ Function STRING fileread(PRIV_CALL, STRING)
 Function DURATION duration(STRING, DURATION)
 Function INT integer(STRING, INT)
 Function VOID collect(HEADER)
+Function REAL unixtime(TIME)
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index e728611..54cc7d8 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -117,3 +117,9 @@ vmod_integer(struct req *req, const char *p, long i)
 
 	return (r);
 }
+
+double __match_proto__(td_std_unixtime)
+vmod_unixtime(struct req *req, double t)
+{
+	return (t);
+}



More information about the varnish-dev mailing list