add 2hours in current time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add 2hours in current time
# 1  
Old 09-10-2008
add 2hours in current time

hi
i want to add 2hr ( or any hours inputed by user) to current time
and print it.
can any one help me with script.


eg suppose current date is "10-sep-2008 23:10:00"
and if i added 4hrs to it output should be "11-sep-2008 03:10:00"

i want to run shell script in "ksh"
# 2  
Old 09-10-2008
One way is to change the TZ variable in the script. example you are in MST (+7), pick a timezone two hours earlier like EST
Code:
export TZ=MST7MDT
date
export TZ=EST5EDT
date

the 7 and the 5 are important you can make up 3 letter names for the timezone, or just reuse the current timezone. You can fudge time by as much as +12 -12 hours.

Otherwise see the datecalc script in Perderabo's thread 'Date Arithmetic' in the forums FAQ.
# 3  
Old 09-10-2008
Quote:
Originally Posted by anup13
hi
i want to add 2hr ( or any hours inputed by user) to current time
and print it.
can any one help me with script.


eg suppose current date is "10-sep-2008 23:10:00"
and if i added 4hrs to it output should be "11-sep-2008 03:10:00"

i want to run shell script in "ksh"
please post the output of your echo $TZ
if you have date -d option you can do it with date also just go through the man page
# 4  
Old 09-10-2008
the TZ solution is probably the simplest. In case you need something more arbitrary, perl comes in really handy, but this can also be done with a combination of date and bc.
Code:
perl -e 'print "" . localtime(time()+$ARGV[0]) . "\n";' 7200

Here, 7200 represents 2 hours of seconds. The code above gets the current time in seconds, adds to it the contents of the first argument, and translates it back into standard "date" format.
# 5  
Old 09-10-2008
If the OP has GNU date. The whole thing sounds like homework to me.
# 6  
Old 09-12-2008
hi
guys
thanks for your reply. !!

cant it done using only shell scripting. without using perl or doing changes to system like changing TZ
# 7  
Old 09-14-2008
Quote:
Originally Posted by anup13
hi
guys
thanks for your reply. !!

cant it done using only shell scripting. without using perl or doing changes to system like changing TZ
Probably. Please read the site rules.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

2. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

3. Shell Programming and Scripting

To get the time exactly 24hrs from the current time

Hi guys, I am having file which contains below data. 2012-04-24 08:40:13 10739022730 1027699274PersonInfoShipTO details missing FirstName,LastName, 2012-04-24 08:40:13 10739022730 1027699274PersonInfoShipTO details missing FirstName,LastName, 2012-04-24 08:40:13 ... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

4. Shell Programming and Scripting

Add current date and time

i have file 1.txt asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney i want to add today's date and time in the end of each row expected output asdas|csada|13|03|10|04|23|A1|canberra|130430|1358 sdasd|sfdsf|13|04|26|23|28|A1|sydney|130430|1358 todays date... (10 Replies)
Discussion started by: radius
10 Replies

5. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

6. Shell Programming and Scripting

How far is given date from current time?

give a date and time: Jun 12 21:05:16 06-12-2012 21:05:16 2012/06/12 21:05:16 How can i subtract these dates and times from the current date and time and get back the difference in seconds? a one liner like: echo "Jun 12 21:05:16" | some perl/awk programming 90900s (2 Replies)
Discussion started by: SkySmart
2 Replies

7. Shell Programming and Scripting

Get Current Time in Seconds Perl

hi guys, i need to know how to get the current date/time in seconds and i want to be able to do this in a one liner. like say for instance, if want to get what the time is right now, i'll issue a command like this: ## perl -e ' print scalar(localtime(time + 0)), "\n"' Tue Jul 13 17:45:50... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. Shell Programming and Scripting

unziping to current time stamp

Basically when ever unzipping the .zip file , what ever the file exist in the .zip file with the time stamp, that is the same time stamp after unzip. But if i need the current time stamp to the unzipped file(time stamp whenever unzip process occurs ) any helpful option in unzip command ??? ... (4 Replies)
Discussion started by: posix
4 Replies

9. Shell Programming and Scripting

getting hour minus the current time

Can some one help me getting last hour of the current time with date command in a script. (7 Replies)
Discussion started by: shehzad_m
7 Replies

10. Shell Programming and Scripting

param as current date+time

Hi All, I need to pass param on aix "errpt -a -s MMDDHHMMYY -e MMDDHHMMYY". How do I read the date+time on the system and pass it as parameter? I need also the -s as previous day and the -e as current day. Thanks, itik (1 Reply)
Discussion started by: itik
1 Replies
Login or Register to Ask a Question