From: Willy Tarreau Date: Thu, 25 Jul 2013 12:28:25 +0000 (+0200) Subject: MINOR: sample: add a new "date" fetch to return the current date X-Git-Tag: v1.5-dev20~326 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6236d3abe4fe7e698f0fd26d6cf51f0d4c6d872a;p=thirdparty%2Fhaproxy.git MINOR: sample: add a new "date" fetch to return the current date Returns the current date as the epoch (number of seconds since 01/01/1970). If an offset value is specified, then it is a number of seconds that is added to the current date before returning the value. This is particularly useful to compute relative dates, as both positive and negative offsets are allowed. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index eab6827758..58e4fa5711 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -8801,6 +8801,12 @@ connslots([]) : integer then this fetch clearly does not make sense, in which case the value returned will be -1. +date([]) : integer + Returns the current date as the epoch (number of seconds since 01/01/1970). + If an offset value is specified, then it is a number of seconds that is added + to the current date before returning the value. This is particularly useful + to compute relative dates, as both positive and negative offsets are allowed. + env() : string Returns a string containing the value of environment variable . As a reminder, environment variables are per-process and are sampled when the diff --git a/src/sample.c b/src/sample.c index 607239269b..f8fa717cdd 100644 --- a/src/sample.c +++ b/src/sample.c @@ -1125,6 +1125,24 @@ smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt, return 1; } +/* retrieve the current local date in epoch time, and applies an optional offset + * of args[0] seconds. + */ +static int +smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt, + const struct arg *args, struct sample *smp) +{ + smp->data.uint = date.tv_sec; + + /* add offset */ + if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT)) + smp->data.uint += args[0].data.sint; + + smp->type = SMP_T_UINT; + smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; + return 1; +} + /* Note: must not be declared as its list will be overwritten. * Note: fetches that may return multiple types must be declared as the lowest * common denominator, the type that can be casted into all other ones. For @@ -1134,6 +1152,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, { { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_INTRN }, + { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN }, { /* END */ }, }};