script for add and subtract two hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script for add and subtract two hours
# 8  
Old 08-17-2006
Adapt the following script :

Code:
#!/ust/bin/ksh

TimeDiff()
{
   integer seconds
   integer h1 m1 s1 time1
   integer h2 m2 s2 time2

   # Convert times to seconds

   echo "$1" | IFS=':' read h1 m1 s1
   echo "$2" | IFS=':' read h2 m2 s2
   (( time1 = h1*3600  + m1*60 +s1 ))
   (( time2 = h2*3600  + m2*60 +s2 ))

   # Compute diff times in seconds

   (( seconds = time1 - time2 ))
   (( seconds < 0 )) && (( seconds = -seconds ))

   # Output result as seconds

   echo $seconds

   # Output result as hh:mm:ss

   (( h1 = seconds / 3600 ))
   (( m1 = seconds / 60 % 60 ))
   (( s1 = seconds % 60 ))
   printf "%02d:%02d:%02d\n" $h1 $m1 $s1
}

TimeDiff $1 $2

Output:
Code:
$ sh difftime.ksh 12:12:12 11:10:09  
3723
01:02:03


Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add time hours

Gents, Is there the way to increase hours (+3) to the data with the format following. example JDhhmmss 335193508 input 335193508 335203508 335213508 335223508 335233508 output 335223508 335233508 336003508 336013508 (14 Replies)
Discussion started by: jiam912
14 Replies

2. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

3. Shell Programming and Scripting

Add new column which is subtract of 2 columns.

Hi below is a file Date Category Time Attempts Success 2/17/2014 PayFlow ATB 0.999988 4039104 4039057 2/18/2014 PayFlow ATB 0.999912 4620964 4620558 2/19/2014 PayFlow ATB 0.999991 4380836 4380796 2/20/2014 PayFlow ATB 0.999988 5031047 5030985 2/21/2014 ... (5 Replies)
Discussion started by: villain41
5 Replies

4. UNIX for Dummies Questions & Answers

awk to add/subtract an integer to/from each entry in columns?

---------- Post updated at 01:58 PM ---------- Previous update was at 01:48 PM ---------- For some reason my question is not getting printed. Here are the details: Greetings. I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

5. Shell Programming and Scripting

Add values in 2 columns and subtract from third

Hi All, I have a file with thousands of lines in the following format, where Field1=First 8 characters Field2-9-16 characters Field3=17-26 characters I need to add Field 1 and Field2 and subtract the result from Field 3. Field3=Field3 - (Field1 + Field2) 0012.00 0010.00 0001576.53... (4 Replies)
Discussion started by: nua7
4 Replies

6. Shell Programming and Scripting

add and subtract specific row using awk

Hi Folks I have tried awk command to conditionally add or subtract value from specific row in a file. The test file looks like: # cat test.txt cont x y Max 0.3 0.9 Min 0.2 0.9 diff 0.1 0 # awk '{for (i=2; i <=NF; i++) if ($i== "0") $i=$i+0.2; print}' test.txt Output: cont... (1 Reply)
Discussion started by: dixits
1 Replies

7. Shell Programming and Scripting

Script to subtract rows HELP

Dear All, Please help with a script which can accomplish the following: Input table: $1 $2 $3 Student1 1 50 Student2 56 75 Student3 77 100 Desired Output: $1 $2 $3 $4 Student1 1 50 Student2 56 75 6 Student3 77 ... (4 Replies)
Discussion started by: saint2006
4 Replies

8. Shell Programming and Scripting

Add/Subtract Time

need some help on the below requirement: File1: SV,22,20100501140000,JFK,RUH SV,29,20100501073000,BOM,RUH SV,29,20100501073000,SIN,RUH third filed is datetime which is of the format (yyyymmddhh24miss) File2 JFK,+,0500 BLR,-,0530 SIN,-,0800 for every line of file 1, take 4... (9 Replies)
Discussion started by: ssantoshss
9 Replies

9. UNIX for Dummies Questions & Answers

Subtract -1 from a date in Unix script

Hi I have a date in a variable in Unix script and have to subtract -1 from the date. fromdate='07/13/2009' Now we want to subract one day from it i.e the end result should be '07/12/2009' Any suggestions? Thanks (2 Replies)
Discussion started by: akashtcs
2 Replies

10. UNIX for Dummies Questions & Answers

How to subtract 2 hours from 'date' in shell ( /bin/sh ) script ?

I write a sh script that zip and copy to tape all files that older then 2 hours. 1. The way I choose is - touch a file with "now - 2 hours", then use fine with '! -newer' 2. Do you have any other idea to do it ? tnx. (1 Reply)
Discussion started by: yairon
1 Replies
Login or Register to Ask a Question
AT(1)							      General Commands Manual							     AT(1)

NAME
at - execute commands at a later time SYNOPSIS
at [ -c ] [ -s ] [ -m ] time [ day ] [ file ] DESCRIPTION
At spools away a copy of the named file to be used as input to sh(1) or csh(1). If the -c flag (for (csh(1))) or the -s flag (for (sh(1))) is specified, then that shell will be used to execute the job; if no shell is specified, the current environment shell is used. If no file name is specified, at prompts for commands from standard input until a ^D is typed. If the -m flag is specified, mail will be sent to the user after the job has been run. If errors occur during execution of the job, then a copy of the error diagnostics will be sent to the user. If no errors occur, then a short message is sent informing the user that no errors occurred. The format of the spool file is as follows: A four line header that includes the owner of the job, the name of the job, the shell used to run the job, and whether mail will be set after the job is executed. The header is followed by a cd command to the current directory and a umask command to set the modes on any files created by the job. Then at copies all relevant environment variables to the spool file. When the script is run, it uses the user and group ID of the creator of the spool file. The time is 1 to 4 digits, with an optional following `A', `P', `N' or `M' for AM, PM, noon or midnight. One and two digit numbers are taken to be hours, three and four digits to be hours and minutes. If no letters follow the digits, a 24 hour clock time is understood. The optional day is either (1) a month name followed by a day number, or (2) a day of the week; if the word `week' follows, invocation is moved seven days further off. Names of months and days may be recognizably truncated. Examples of legitimate commands are at 8am jan 24 at -c -m 1530 fr week at -s -m 1200n week At programs are executed by periodic execution of the command /usr/libexec/atrun from cron(8). The granularity of at depends upon the how often atrun is executed. Error output is lost unless redirected or the -m flag is requested, in which case a copy of the errors is sent to the user via mail(1). FILES
/usr/spool/at spooling area /usr/spool/at/yy.ddd.hhhh.* job file /usr/spool/at/past directory where jobs are executed from /usr/spool/at/lasttimedone last time atrun was run /usr/libexec/atrun executor (run by cron(8)) SEE ALSO
atq(1), atrm(1), calendar(1), sleep(1), cron(8) DIAGNOSTICS
Complains about various syntax errors and times out of range. BUGS
Due to the granularity of the execution of /usr/libexec/atrun, there may be bugs in scheduling things almost exactly 24 hours into the future. If the system crashes, mail is not sent to the user informing them that the job was not completed. Sometimes old spool files are not removed from the directory /usr/spool/at/past. This is usually due to a system crash, and requires that they be removed by hand. 4th Berkeley Distribution October 21, 1996 AT(1)