[4.0] 1ac441b Add std.time()

Lasse Karstensen lkarsten at varnish-software.com
Thu Jan 15 16:35:44 CET 2015


commit 1ac441bc03670b4b4fbe76d5638dfb0753921e83
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Tue Dec 16 11:05:56 2014 +0000

    Add std.time()
    
    Converts an date in various formats to a VCL_TIME.

diff --git a/bin/varnishtest/tests/m00020.vtc b/bin/varnishtest/tests/m00020.vtc
new file mode 100644
index 0000000..3611cc4
--- /dev/null
+++ b/bin/varnishtest/tests/m00020.vtc
@@ -0,0 +1,40 @@
+varnishtest "Test std.time"
+
+server s1 {
+	rxreq
+	txresp
+} -start
+
+varnish v1 -vcl+backend {
+	import ${vmod_std};
+
+	sub vcl_deliver {
+		if (std.time(req.http.x-date, now) < now - 1y) {
+			set resp.http.x-past = 1;
+		}
+		if (std.time(req.http.x-date, now) > now + 1y) {
+			set resp.http.x-future = 1;
+		}
+	}
+} -start
+
+client c1 {
+	txreq -hdr "X-Date: Mon, 20 Dec 2010 00:00:00 GMT"
+	rxresp
+	expect resp.http.x-past == 1
+	txreq -hdr "X-Date: Monday, 20-Dec-30 00:00:00 GMT"
+	rxresp
+	expect resp.http.x-future == 1
+	txreq -hdr "X-Date: Mon Dec 20 00:00:00 2010"
+	rxresp
+	expect resp.http.x-past == 1
+	txreq -hdr "X-Date: 2030-12-20T00:00:00"
+	rxresp
+	expect resp.http.x-future == 1
+	txreq -hdr "X-Date: 1292803200.00"
+	rxresp
+	expect resp.http.x-past == 1
+	txreq -hdr "X-Date: 1923955200"
+	rxresp
+	expect resp.http.x-future == 1
+} -run
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index a88382f..1bc92d6 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -227,6 +227,25 @@ Example
 	| 	...
 	| }
 
+$Function TIME time(STRING s, TIME fallback)
+
+Description
+	Converts the string *s* to a time.  If conversion fails,
+	*fallback* will be returned.
+
+	Supported formats:
+
+	| "Sun, 06 Nov 1994 08:49:37 GMT"
+	| "Sunday, 06-Nov-94 08:49:37 GMT"
+	| "Sun Nov  6 08:49:37 1994"
+	| "1994-11-06T08:49:37"
+	| "784111777.00"
+	| "784111777"
+Example
+	| if (std.time(resp.http.last-modified, now) < now - 1w) {
+	| 	...
+	| }
+
 
 SEE ALSO
 ========
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index cdcd3cb..0771687 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -41,6 +41,7 @@
 
 #include "vrt.h"
 #include "vsa.h"
+#include "vtim.h"
 #include "vcc_if.h"
 
 VCL_DURATION __match_proto__(td_std_duration)
@@ -219,3 +220,18 @@ vmod_time2real(const struct vrt_ctx *ctx, VCL_TIME t)
 
 	return (t);
 }
+
+VCL_TIME __match_proto__(td_std_time)
+vmod_time(VRT_CTX, VCL_STRING p, VCL_TIME d)
+{
+	double r;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	if (p == NULL)
+		return (d);
+	r = VTIM_parse(p);
+	if (r)
+		return (r);
+	return (vmod_real(ctx, p, d));
+}



More information about the varnish-commit mailing list