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 eab6827..58e4fa5 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -8801,6 +8801,12 @@
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
diff --git a/src/sample.c b/src/sample.c
index 6072392..f8fa717 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -1125,6 +1125,24 @@
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 @@
{ "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 */ },
}};