Software & AppsOperating SystemLinux

How To Add Hours to a Specified Time in Ubuntu

Ubuntu 7

In this article, we will delve into the process of adding a specified number of hours to a given time in Ubuntu. This can be quite useful in various scenarios such as scheduling tasks or scripting. We will be using the date command in the command line for this purpose.

Quick Answer

To add hours to a specified time in Ubuntu, you can use a Bash script that utilizes the date command and the bc calculator. The script takes the given time and the number of hours to be added as parameters, converts the decimal hours to minutes, and then adds the minutes to the given time. The result is the new time after adding the specified hours.

Understanding the Date Command

The date command in Linux is a powerful tool that allows you to set or display the system date and time, among other things. It has a flexible syntax that can handle various time formats and calculations. We will leverage this command to add hours to a given time.

Prerequisites

Before we start, ensure that you have access to a terminal in Ubuntu. You will also need the bc calculator installed on your system. You can install it using the following command:

sudo apt-get install bc

Adding Hours to a Specified Time

Here is a sample script that allows you to add decimal hours to a given time:

#!/bin/bash
# Add decimal hours to given time.
# Syntax: timeadd HH:MM DECHOURS
# There MUST be 2 digits for the hours in HH:MM.
# Times must be in military time.
# Ex: timeadd 05:51 4.51
# Ex: timeadd 14:12 2.05

# If we have less than 2 parameters, show instructions and exit.
if [ $# -lt 2 ]
then
 echo "Usage: timeadd HH:MM DECHOURS"
 exit 1
fi

intime=$1
inhours=$2

# Convert decimal hours to minutes
inminutes=$(echo "scale=0; ((($inhours * 60)*10)+5)/10" | bc)

# Add minutes to the given time
now=$(date -d "$intime today + $inminutes minutes" +'%H:%M')

echo "New time is $now"

Understanding the Script

Let’s break down the script to understand what each part does:

  1. Shebang (#!/bin/bash): This line tells the system that this script should be run using the Bash shell.
  2. Usage instructions: If the script is run without the required number of parameters (less than 2), it will display usage instructions and exit.
  3. Parameter assignment: The script assigns the first parameter ($1) to the variable intime and the second parameter ($2) to the variable inhours.
  4. Decimal hours to minutes conversion: The script uses the bc calculator to convert the decimal hours to minutes, rounding to the nearest whole number.
  5. Adding minutes to the given time: The script uses the date command to add the calculated minutes to the given time, outputting the result in the format HH:MM.
  6. Output: The script echoes the new time.

Using the Script

To use this script, save it to a file, for example, timeadd.sh, make it executable using the chmod command:

chmod +x timeadd.sh

Then run it with the desired time and decimal hours as arguments:

./timeadd.sh 06:45 1.45

The script will output the new time after adding the specified hours.

Conclusion

This article has shown you how to add a specific number of hours to a given time in Ubuntu using a Bash script. The date command and bc calculator are powerful tools that can handle a wide variety of time calculations. With this knowledge, you can now manipulate time values to suit your needs in Ubuntu. Remember that this script assumes the time is in the format HH:MM and uses military time. Happy scripting!

What is the purpose of the `date` command in Ubuntu?

The date command in Ubuntu is used to set or display the system date and time. It can also handle various time formats and calculations.

How can I install the `bc` calculator in Ubuntu?

You can install the bc calculator in Ubuntu by running the command sudo apt-get install bc in the terminal. This will install the calculator on your system.

Leave a Comment

Your email address will not be published. Required fields are marked *