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.
This commit is contained in:
Willy Tarreau 2013-07-25 14:28:25 +02:00
parent 5b8ad22228
commit 6236d3abe4
2 changed files with 25 additions and 0 deletions

View File

@ -8801,6 +8801,12 @@ connslots([<backend>]) : integer
then this fetch clearly does not make sense, in which case the value returned
will be -1.
date([<offset>]) : 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(<name>) : string
Returns a string containing the value of environment variable <name>. As a
reminder, environment variables are per-process and are sampled when the

View File

@ -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 <const> 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 */ },
}};