0

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.

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Um, pretty sure that you can add seconds in date -d, at least with GNU date. date -d 'now + 3600 seconds' prints the date in one hour (=3600s) from now.
You are right, @Socowi. I had some issue while trying to add this date and I assumed it was not possible. Input format for date is not easy.
Yeah, you never know what -d accepts and what it rejects. Even after reading the (sparse) documentation I still have to guess and test what works and what doesn't.
In fact, asking for "2020-9-19T00:35:00 + 1 second" does not work.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.