Commit 1ac441bc authored by Federico G. Schwindt's avatar Federico G. Schwindt Committed by Lasse Karstensen

Add std.time()

Converts an date in various formats to a VCL_TIME.
parent 12a6d79c
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
......@@ -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
========
......
......@@ -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));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment