summaryrefslogtreecommitdiffstats
path: root/lib/gettime.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:39 +0000
commit5242eef8fc54636a41701fd9d7083ba6e4a4e0b3 (patch)
treee6a0980092957865a937cc0f34446df3d5194e99 /lib/gettime.c
parentReleasing progress-linux version 1:4.13+dfsg1-5~progress7.99u1. (diff)
downloadshadow-5242eef8fc54636a41701fd9d7083ba6e4a4e0b3.tar.xz
shadow-5242eef8fc54636a41701fd9d7083ba6e4a4e0b3.zip
Merging upstream version 1:4.15.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/gettime.c')
-rw-r--r--lib/gettime.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/gettime.c b/lib/gettime.c
new file mode 100644
index 0000000..c61c88c
--- /dev/null
+++ b/lib/gettime.c
@@ -0,0 +1,47 @@
+// SPDX-FileCopyrightText: 2017, Chris Lamb
+// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#ident "$Id$"
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+
+#include "atoi/a2i.h"
+#include "defines.h"
+#include "prototypes.h"
+#include "shadowlog.h"
+
+
+/*
+ * gettime() returns the time as the number of seconds since the Epoch
+ *
+ * Like time(), gettime() returns the time as the number of seconds since the
+ * Epoch, 1970-01-01 00:00:00 +0000 (UTC), except that if the SOURCE_DATE_EPOCH
+ * environment variable is exported it will use that instead.
+ */
+/*@observer@*/time_t
+gettime(void)
+{
+ char *source_date_epoch;
+ FILE *shadow_logfd = log_get_logfd();
+ time_t fallback, epoch;
+
+ fallback = time (NULL);
+ source_date_epoch = shadow_getenv ("SOURCE_DATE_EPOCH");
+
+ if (!source_date_epoch)
+ return fallback;
+
+ if (a2i(time_t, &epoch, source_date_epoch, NULL, 10, 0, fallback) == -1) {
+ fprintf(shadow_logfd,
+ _("Environment variable $SOURCE_DATE_EPOCH: a2i(\"%s\"): %s"),
+ source_date_epoch, strerror(errno));
+ return fallback;
+ }
+ return epoch;
+}