Reputation: 877
I am trying to write the code to convert the date to desired format and increase 300 seconds in every run.
Date is in form 2020-12-19 00:35:00+00:000 want it to be converted to form 2020-12-19T00:35:00Z and after every run increase add five minutes to the timestamp.
for run in $(seq 0 $REPEAT)
do
echo "======================"
VARIABLE=$( expr 300 '*' $run)
DATE=$(date --iso-8601=seconds -d "2020-19-19T00:35:00 + $VARIABLE seconds")
echo $DATE
for file in ${FILELIST[@]};
do
echo "Running fine"
done
done
But i am getting the output as:
2020-03-09T03:05:01+05:30
I want the output to be 2020-12-19T00:35:00Z then 2020-12-19T00:40:00Z and so on.
Upvotes: 0
Views: 527
Reputation: 27360
As pointed out by Poshi your script used the date 2020-19-19
which does not exist. Once we fix that we are very close. There are only three things left to do:
-d "isoFormatWithoutTimezome + 3 seconds
seems to be misinterpreted as
-d "(isoFormat withTimezone='+3') seconds"
. With this interpretation the trailing seconds
doesn't really make sense but seems to add a single second to the date. To prevent false interpretation as a time zone specify the time zone after the date and before the + 3 seconds
:
-d "2020-19-19T00:35:00Z + 3 seconds"
.
Setting the timezone for the output is possible with date
's environment variable TZ
. For UTC we can set the TZ
to the empty string.
date -I
shows UTC as +00:00
instead of Z
. To change the output format we could either supply a custom date String +...Z
or simply do a string replacement sed s/+00:00$/Z/
.
By the way: The preferred naming convention for variables in bash is NOT ALLCAPS, as allcaps variables are by convention environment variables and have a high chance colliding with already existing environment variables.
#! /usr/bin/env bash
repeat=5 # does 6 repeats! (retained behavior from your original script)
for ((run = 0; run <= repeat; run++)); do
date=$(TZ= date -Is -d "2020-12-19T00:35:00Z + $((run * 300)) sec" | sed 's/+00:00$/Z/')
echo "$date"
# do stuff
done
prints
2020-12-19T00:35:00Z
2020-12-19T00:40:00Z
2020-12-19T00:45:00Z
2020-12-19T00:50:00Z
2020-12-19T00:55:00Z
2020-12-19T01:00:00Z
Upvotes: 0
Reputation: 5762
Month number 19 does not exist. Try with following approach:
# Get the number of seconds since Epoch
secs=$(date +%s --date="2020-9-19T00:35:00")
for run in $(seq 0 $REPEAT)
do
echo "======================"
# Next increment
VARIABLE=$((300*run))
# Add the increment seconds and translate to the desired format
DATE=$(date --iso-8601=seconds -d "@$((secs+VARIABLE))")
echo $DATE
for file in ${FILELIST[@]};
do
echo "Running fine"
done
done
BTW, if you really want to have Z
timezone, you will have to work a little bit the output format.
Upvotes: 1