From 22cf75db16b1e8f4fc70ba3107a169ee16075c0a Mon Sep 17 00:00:00 2001 From: Davlet Panech Date: Fri, 9 Sep 2022 10:37:59 -0400 Subject: [PATCH] Set BUILD_DATE to normalized $TIMESTAMP Make sure BUILD_DATE field in the BUILD file reflects the actual build timestamp, in a fixed format and UTC time zone. Signed-off-by: Davlet Panech Change-Id: Ia89f15aaecd1391514a8d11912c4e382a2ad565e --- scripts/configure-build.sh | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/scripts/configure-build.sh b/scripts/configure-build.sh index 7130322..aa140d8 100755 --- a/scripts/configure-build.sh +++ b/scripts/configure-build.sh @@ -11,6 +11,29 @@ source $(dirname "$0")/lib/job_utils.sh load_build_env +# Convert $TIMESTAMP to a string similar to +# 2022-09-09 14:29:05 +0000 +# Accepts timestamps similar to: +# 2022-09-08_18-00-07 +# 20220909T034458Z +normalize_timestamp() { + local str="$1" + local norm + # $1:yr $2:mon $3:day $4 $5:hr $6:min $7:sec $8:TZ + str="$(echo "$str" | sed -r 's/^\s*([0-9]{4})-?([0-9]{2})-?([0-9]{2})([T_-]|\s+)([0-9]{2})-?([0-9]{2})-?([0-9]{2})\s*([zZ]|([+-][0-9]{4}))?.*$/\1-\2-\3T\5:\6:\7\8/')" + str="$(echo "$str" | sed -r 's/([0-9])[zZ]$/\1+0000/')" + if ! echo "$str" | grep -q -E '[+-][0-9]{4}$' ; then + local tz_offset + tz_offset="$(date --date="$str" '+%z')" || return 1 + str+="$tz_offset" + fi + date="$(date --date="$str" --utc +'%Y-%m-%d %H:%M:%S +0000')" || return 1 + echo "$date" +} + +build_date="$(normalize_timestamp "$TIMESTAMP")" || exit 1 + + ###################################################### # BUILD file ###################################################### @@ -38,7 +61,7 @@ JOB="$MASTER_JOB_NAME" BUILD_BY="$USER" BUILD_NUMBER="$MASTER_BUILD_NUMBER" BUILD_HOST="$HOSTNAME" -BUILD_DATE="$(date '+%F %T %z')" +BUILD_DATE="$build_date" _END notice "$build_info_file"