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 <davlet.panech@windriver.com>
Change-Id: Ia89f15aaecd1391514a8d11912c4e382a2ad565e
This commit is contained in:
Davlet Panech 2022-09-09 10:37:59 -04:00
parent 42576256a5
commit 22cf75db16
1 changed files with 24 additions and 1 deletions

View File

@ -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"