]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: add a new "date" fetch to return the current date
authorWilly Tarreau <w@1wt.eu>
Thu, 25 Jul 2013 12:28:25 +0000 (14:28 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 25 Jul 2013 13:00:37 +0000 (15:00 +0200)
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.

doc/configuration.txt
src/sample.c

index eab682775825b528e161684a8947cd6fd7ae8706..58e4fa5711a801b730fdda368583883dff8510c9 100644 (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
index 607239269ba1dd5e7bbeb3b8963dcb33aece8a0b..f8fa717cdd05e58c6eed307c600196582463323b 100644 (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 */ },
 }};