Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Time

Status
Not open for further replies.

plbiju

Technical User
Oct 3, 2001
11
How can I add time

Eg : consider the current time as 12:30

I want to add 5:30 to the above time ..

How to do it with a single command


 
What are you trying to do? Are you writing a script that you want to add +5 hours to a file name?
 
Yes I am writing a script , in that I want to add 5.30 to the current system time

 
/usr/bin/date [ -u ] [ [ mmdd ] HHMM | mmddHHMM [ cc ] yy

So you could do
# date 0607130003 => Jun 07 1 pm 2003. # You would have to set some variables and subtract the 5 hours.

Check out the 'man date'
 
Thanks for the response ,, As of now my script is running by assigning to variables and adding time but this is a tedios process since if the minute field is added and if it is more than 60 than hour field to be increased by 1 , Similarly for seconds . Is there any easy way by which when we add some time to the current time it has to calculate the date , time every thing



Regards
Biju
 
My next question is why do you constantly need to add 5 hours to a machine? Depending what you are trying to accomplish there may be other ways. ie. touch can change the date of files, date -a [sss.fff] to add seconds to current date (clock speed up and not an immediate change), ntpdate to keep date constant.
 
OK ..... I've been playing with TZ (Time Zone) on Solaris and you can do it like this:

I remember doing this some time back byt could not remember what the syntax was but I've got it now:

To test do the followinhg from the Solaris command line (remember to clear fred from the environment when your finished)
fred=`TZ=BST-9:30:00BST date`;echo $fred;date

running this will display ....
# fred=`TZ=BST-9:30:00BST date`;echo $fred;date
Mon Jun 9 01:24:50 BST 2003
Sun Jun 8 15:54:50 BST 2003
#

Line 1) is a list of commands (3 of them)
Line 2) is the output of the var $fred
Line 3) is teh normal date (now) to compare with.

So what your doing here is adding 5hrs + 30 min to the "current shells" TZ.

I'm sure you can use formating with the date something like ......

# fred=`TZ=BST-9:30:00BST date "+%Y%m%d %H:%M"`;echo $fred;date
20030609 01:33
Sun Jun 8 16:03:26 BST 2003

Play with it and see ...

Good Luck,
Laurie.
 
Sorry .... running away with myself ..... in your example you wanted to add 5.30 to "now" not 9.30 as in my example so to set a varable fred to the current date and time PLUS 5 hrs and 30 minutes do:

fred=`TZ=BST-5:30:00BST date`;echo $fred

Note in the command the plus and minus works oposite to what you would think.

You can use this line in your script (be sure to know what TimeZone your script will be working in, GB, GMT, BST, GMT+1 ????????? ).

Good Luck,
Laurie.
 
#!/usr/bin/ksh

function convert_time_to_seconds {
echo $1|tr ':' ' '|read hrs min sec
echo $((($hrs*60*60)+($min*60)+$sec))
}

function convert_seconds_to_time {
hrs=$(($1/(60*60)))
min=$((($1-($hrs*60*60))/60))
sec=$(($1 % 60))
printf "%02d:%02d:%02d\n" $hrs $min $sec
}

a_secs=$(convert_time_to_seconds "12:30:00")
b_secs=$(convert_time_to_seconds "05:30:00")

c_secs=$(($a_secs+$b_secs))
c_time=$(convert_seconds_to_time $c_secs)

echo $a_time + $b_time = $c_time
 
Sorry, I'll try again...

#!/usr/bin/ksh

function convert_time_to_seconds {
echo $1|tr ':' ' '|read hrs min sec
echo $((($hrs*60*60)+($min*60)+$sec))
}

function convert_seconds_to_time {
hrs=$(($1/(60*60)))
min=$((($1-($hrs*60*60))/60))
sec=$(($1 % 60))
printf "%02d:%02d:%02d\n" $hrs $min $sec
}

a_secs=$(convert_time_to_seconds "12:30:00")
b_secs=$(convert_time_to_seconds "05:30:00")

echo $(convert_seconds_to_time $(($a_secs+$b_secs)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top