From 3776ba9756de63fad533167baa2db77088ba1817 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 6 Jul 2021 12:26:03 +0100 Subject: [PATCH] formula: add a `time` method for use as the build time A number of formulae use a variant of `Time.now` to generate the build time of the formula. Let's define a method to make sure that: 1. it uses `SOURCE_DATE_EPOCH` whenever available; and, 2. it uses `utc`. Both should help with build reproducibility. See Homebrew/homebrew-core#80608. --- Library/Homebrew/formula.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index d7793cdfaf..4dd7cfd2f1 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1551,6 +1551,18 @@ class Formula "@loader_path/../lib" end + # Creates a new `Time` object for use in the formula as the build time. + # + # @see https://www.rubydoc.info/stdlib/time/Time Time + sig { returns(Time) } + def time + if ENV["SOURCE_DATE_EPOCH"].present? + Time.at(ENV["SOURCE_DATE_EPOCH"].to_i).utc + else + Time.now.utc + end + end + # an array of all core {Formula} names # @private def self.core_names