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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add or Subtract the hours,minutes or seconds in the the time variable
# 1  
Old 03-09-2014
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 is in specific format
Code:
Start_Time="May 12, 2012 10:00:00 AM MST"

I have another way which I Incorporated in my script which changes the time into GMT.But the problem is this method cause an issue when daylight saving system starts.MDT is fine during these days but if someone choose MST time zone it creates an issue.I am working on Linux system(x86_64) and bash shell
Code:
date "+%B %e, %Y %X" -u -d 'May 12, 2012 10:00:00 AM MDT'
output :->May 12, 2012 04:00:00 PM

date "+%B %e, %Y %X" -u -d 'May 12, 2012 10:00:00 AM MST'
output:->May 12, 2012 05:00:00 PM

# 2  
Old 03-09-2014
Im not sure I understand your problem. Daylight saving time should be one hour ahead, so your results should be correct, no?
# 3  
Old 03-09-2014
This problem is when the person who is working from India choose the time in MST to create the Instance it doesn't create the Instance with the correct time
If I choose time 10 AM MST then it coverts the time to 5 PM GMT and it means if u add 5:30 hours in that GMT time (5+5:30)then according to India it will schedule the instancne at 10:30 am which is not correct it should schedule the instance at 9:30 am according to India
# 4  
Old 03-09-2014
Then you have to control the users' input data, not the date/time algorithm. You can check it with various methods: in a shell script, or by using awk or similar ...
# 5  
Old 03-09-2014
If the time is sync-ed, it doesn't matter from which part of the world you are issuing the command. You need to get the user-input right for the right output.
# 6  
Old 03-09-2014
Can you please let me know one thing how could I add or subtract hours,minutes or seconds for the time that is store in a variable.I know how to do from the current time but not able to do that from the time that is stored in a variable
# 7  
Old 03-09-2014
Using GNU date
Code:
root@maximus:/tmp# echo $dd
2014-03-09 17:38:55

root@maximus:/tmp# date -d "$dd 1 hour" "+%Y-%m-%d %H:%M:%S"
2014-03-09 18:38:55
root@maximus:/tmp# date -d "$dd 1 min" "+%Y-%m-%d %H:%M:%S"
2014-03-09 17:39:55
root@maximus:/tmp# date -d "$dd 1 sec" "+%Y-%m-%d %H:%M:%S"
2014-03-09 17:38:56

root@maximus:/tmp# date -d "$dd 1 hour ago" "+%Y-%m-%d %H:%M:%S"
2014-03-09 16:38:55
root@maximus:/tmp# date -d "$dd 1 min ago" "+%Y-%m-%d %H:%M:%S"
2014-03-09 17:37:55
root@maximus:/tmp# date -d "$dd 1 sec ago" "+%Y-%m-%d %H:%M:%S"
2014-03-09 17:38:54

Saving it in another variable
Code:
root@maximus:/tmp# new_date=$( date -d "$dd 1 sec ago" "+%Y-%m-%d %H:%M:%S" )
root@maximus:/tmp# echo $new_date
2014-03-09 17:38:54

Change the date format as you want.

HTH
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

2. Shell Programming and Scripting

How to split numeric value into hours:minutes:seconds

I have a problem. I am working on a Call Detail Report system. Come to find out the phone switch does not report in seconds. It is a 5 digit field that reports h:mm:ss The problem is I have 1-5 digit numbers Ie 1 = 1 second and should be reported as 0:00:01 22 should be 0:00:22 321 should be... (5 Replies)
Discussion started by: truecall
5 Replies

3. Shell Programming and Scripting

How to subtract time by 10 minutes in datecalc tool

Hi guys. I am trying to subtract 10 minutes from the current Unix system date and time. I have the datecalc provided here but it is mainly the date and not the time. Please check on how can i subtract 10 minutes from the current time using datecalc or any other shell scripting that will... (2 Replies)
Discussion started by: bantiloe
2 Replies

4. Shell Programming and Scripting

Perl - Extract 12 hour time, convert to 24 and subtract 15 minutes?

OK, I am by no means a programmer... I have been given the task to do some automation scripts. I have got most of it working from snippets I have found on the Web. One requirement has me stumped. The initial timing file created by the user is a comma delimited in the following format.... (4 Replies)
Discussion started by: autotuner
4 Replies

5. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

6. Shell Programming and Scripting

Process Time in hours or minutes only

Hi i want to print the time of a process in hours only..(or) in minutes only.Is there anyway to print the process such like that when i give the commnand like following #ps -eo pid,time PID TIME 412 01:49:32 481 00:03 it shows in HH:MM:SS format: Could anyone... (1 Reply)
Discussion started by: srikanthg
1 Replies

7. Shell Programming and Scripting

Difference in day-hours-minutes-seconds format

Hi experts, I am reading two log files and passing dates as output to a txt file. Code is given below: echo "Start Time:" >> Report.txt cat start.log | while read LINE1 do echo $DATE1 >> Report.txt done echo "End Time:" >> Report.txt cat end.log | while read LINE2 ... (7 Replies)
Discussion started by: Sreejith_VK
7 Replies

8. Shell Programming and Scripting

how to display time in minutes n seconds...

Hi all, may i know how to display time in minutes and seconds(may be milliseconds and even smaller that ) in shell scripts.... (1 Reply)
Discussion started by: santy
1 Replies

9. Shell Programming and Scripting

script for add and subtract two hours

i find a script to calculate hours of job i(nclude extraordinary) i make a script to calculate add and subtract two hours (format hh:mm:ss) (7 Replies)
Discussion started by: ZINGARO
7 Replies

10. Shell Programming and Scripting

Convert minutes to hours, minutes, seconds

How would you convert lets say a 1000 minutes to hours, minutes, seconds (1 Reply)
Discussion started by: Vozx
1 Replies
Login or Register to Ask a Question