0

let we have 3 time

time1=11:34:45

time2:12:39:32

target_time:12:45:48

so how to find that the target time is greater or equal to time1 or time2?

required output:

the target time 12:45:48 is greater or equal to 12:39:32

3
  • 1
    It's unclear where the times are stored (in a file, in three files, in separate shell variables, or in variables in some other language, with or without the time1 and time2 etc. tag). Commented Dec 26, 2022 at 12:07
  • 23:59:59 will be greater than or equal to time1 and time2. Are you given time3? If so, what should happen if it isn't greater than or equal to time1 and time2? Commented Dec 26, 2022 at 12:14
  • time1 and time2 are stored in variable where as target_time is user input time. all three time are in same script Commented Dec 27, 2022 at 9:31

1 Answer 1

1

You can begin by converting the time to a common format that would be much easier to compare, like in seconds.

This can be done like so in a bash function:

time_to_seconds() {
    IFS=: read -r hours minutes seconds <<< "$1"
    echo $(( hours * 3600 + minutes * 60 + seconds ))
}

IFS=: tells bash to separate strings by colons, so that the hours, minutes, and seconds can be read using read.

After that, you can convert your time variables to seconds like this:

time1_secs=$(time_to_seconds "$time1")
time2_secs=$(time_to_seconds "$time2")
target_time_secs=$(time_to_seconds "$target_time")

Then it's just a matter of doing comparisons, like this:

if [ $target_time_secs -ge $time2_secs ]; then
    echo "the target time $target_time is greater or equal to $time2"
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.