Question

  1. I have a file which contains time in YYYYMMDDhhmmss.sss
  2. I am fetching only hours/minutes from the file using the following command start=grep -i "XYZ" | head -1 | awk '{print $3}' | cut -c9-12
  3. The start variable would contain number of hours/minutes (Example: - 1041 [HHMM])
  4. My task is to increment this time by 60 minutes.

Please help me to do so. I am not using system date.

Here's what i tried, start=grep -i "XYZ" | head -1 | awk '{print $3}' | cut -c9-12 end=$(($start) + 3600 )

But This logic is wrong as it will add like a normal number. Also converting the time to seconds would be a tedious job. Is there any way to increment via system commands ? Please suggest.

Was it helpful?

Solution

60 minutes is 1 hour, so all you need to do is increment the hour by one. This can be done by adding 100 to the HHMM time as shown below:

start="$1"

# add 100 to the start time to increment the hour
newTime=$((10#$start+100))

# check if we have crossed midnight
if (( newTime >= 2400 )); then
    newTime=$((newTime-2400))
fi

#pad with leading zero
newTime="$(printf %04d $newTime)"

echo "$newTime"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top