From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- src/include/source_date_epoch.h | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/include/source_date_epoch.h (limited to 'src/include/source_date_epoch.h') diff --git a/src/include/source_date_epoch.h b/src/include/source_date_epoch.h new file mode 100644 index 0000000..7a18d60 --- /dev/null +++ b/src/include/source_date_epoch.h @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Functions to parse the "SOURCE_DATE_EPOCH" environment variable for reproducible build hacks, see + * https://reproducible-builds.org/docs/source-date-epoch/ + *//* + * Authors: + * Patrick Storz + * + * Copyright (C) 2019 Authors + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_SOURCE_DATE_EPOCH +#define SEEN_SOURCE_DATE_EPOCH + +#include +#include +#include +#include + +namespace ReproducibleBuilds { + +/** parse current time from SOURCE_DATE_EPOCH environment variable + * + * \return current time (or zero if SOURCE_DATE_EPOCH unset) + */ +time_t now() +{ + time_t now = 0; + + char *source_date_epoch = std::getenv("SOURCE_DATE_EPOCH"); + if (source_date_epoch) { + std::istringstream iss(source_date_epoch); + iss >> now; + if (iss.fail() || !iss.eof()) { + std::cerr << "Error: Cannot parse SOURCE_DATE_EPOCH as integer\n"; + std::terminate(); + } + } + + return now; +} + +/** like ReproducibleBuilds::now() but returns a ISO 8601 formatted string + * + * \return current time as ISO 8601 formatted string (or empty string if SOURCE_DATE_EPOCH unset) + */ +Glib::ustring now_iso_8601() +{ + Glib::ustring now_formatted; + + time_t now = ReproducibleBuilds::now(); + if (now) { + const tm *now_struct; + char buffer[25]; + + now_struct = gmtime(&now); + if (strftime(buffer, 25, "%Y-%m-%dT%H:%M:%S", now_struct)) { + now_formatted = buffer; + } + } + + return now_formatted; +} + +} // namespace ReproducibleBuilds + +#endif // SEEN_SOURCE_DATE_EPOCH -- cgit v1.2.3