How to get next hour?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get next hour?
# 1  
Old 02-14-2013
How to get next hour?

Hi guys

I want any script to get me next hour

For example

Code:
Nexthour.sh   2013022823

It get me result
Code:
2013030100

Thanks a lot , I'm using Solaris 10
# 2  
Old 02-14-2013
You could do this with perl:

Code:
#!/usr/bin/perl
use Time::Local 'timelocal';
use POSIX;
my $year, $month, $day, $hour;
if($ARGV[0] =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)/) {
    $ARGV[0] =~ s/(\d\d\d\d)(\d\d)(\d\d)(\d\d)/\1 \2 \3 \4/;
    ($year, $month, $day, $hour) = split(/\s/, $ARGV[0]);
    break unless timelocal(0,0,$hour,$day,$month-1,$year);
}

print strftime("%Y%m%d%H\n", localtime(mktime(0,0,$hour+1,$day,$month-1,$year-1900,0,0))) unless ($day == 0)

# 3  
Old 02-14-2013
date +%Y%m%d%H --date="+ 1hour"
# 4  
Old 02-14-2013
an awk approach:


Code:
echo "20130228230000" | awk -v h="3" '{
t_s1=substr($1,1,4)" "substr($1,5,2)" "substr($1,7,2)" "substr($1,9,2)" "substr($1,11,2)" "substr($1,13,2);
$1=strftime("%Y-%m-%d %H:%M:%S",mktime(t_s1)+h*3600);
print }'

# 5  
Old 02-14-2013
We'll have to wait for the OP to get back but I doubt either of the previous two solutions will work on Solaris, as they both require GNU extensions.

Unfortunately on Solaris you are pretty much stuck to using perl, C or a fairly complex shell script to manually calculate dates.
# 6  
Old 02-14-2013
I wrote a general tool tm2tm.c that parses time, delta's it and prints it, posted before: https://www.unix.com/shell-programmin...bsd-linux.html

It inherits a necessity for input element separators from strtoul():
Code:
$ tm2tm 2013-02-28-23 '%Y-%m-%d-%H' +1h '%Y%m%d%H'
2013030100
$


Last edited by DGPickett; 02-14-2013 at 04:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Want to get average value for each hour

I want to get CPU average value only (not required user CPU & memory) with each hours on individual date. The sample output is below | | | User |Memory| User | Date | Time |CPU %|CPU % | % |Mem % | 03/02/2015|00:00:00| 24.56| 20.66| 89.75| 63.48|... (13 Replies)
Discussion started by: Saravanan_0074
13 Replies

2. Shell Programming and Scripting

Hour handling through parameter

hi, I am using parameter to get the new hour as below time=30 time1=1 hours v_StartTime=`date +'%F %T'` v_EndTime=`date +'%F %T' -d ''$time' minutes'` v_EndTime1=`date +'%F %T' -d ''$time1'` echo $v_StartTime echo $v_EndTime echo $v_EndTime1 but i am getting error as test.ksh:... (1 Reply)
Discussion started by: ATWC
1 Replies

3. Shell Programming and Scripting

How to convert 24 hour time to 12 hour timing?

Hi friends, I want to convert 24 hour timing to 12 hour please help me... my data file looks like this.. 13-Nov-2011 13:27:36 15.32044 72.68502 13-Nov-2011 12:08:31 15.31291 72.69807 16-Nov-2011 01:16:54 15.30844 72.74028 15-Nov-2011 20:09:25 15.35096 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

4. Shell Programming and Scripting

Show previous hour

date +%Y%m%d%H output : 2011031415 I want to get previous hour like this 2011031414. Any one can help ? (15 Replies)
Discussion started by: alvin0618
15 Replies

5. Shell Programming and Scripting

Getting the last hour from a log.

I have a log like this: Jan 26 13:59:41 server2 ntpdate: step time server 91.189.94.4 offset 0.065456 sec Jan 26 13:59:41 server2 ntpd: ntpd 4.2.4p8@1.1612-o Fri Aug 6 22:49:54 UTC 2010 (1) Jan 26 13:59:41 server2 ntpd: precision = 1.000 usec Jan 26 13:59:41 server2 ntpd: ntp_io: estimated max... (2 Replies)
Discussion started by: Jotne
2 Replies

6. What is on Your Mind?

Power Hour?

I had some free time at work today so I decided to get a little practice with my shell scripts (I'm pretty new to the whole UNIX thing). I'm sure I'm not the only college student here so maybe this code will come in handy for future weekends. #!/bin/sh if then echo "No playlist... (0 Replies)
Discussion started by: thedoobieman5
0 Replies

7. Shell Programming and Scripting

How can i grep for an hour before data

Hi, My log file is something like this. (08/04/2009 00:27:42.179)(:) aaaaaaaaaaaa (08/04/2009 00:27:42.181)(:) bbbbbbbbbbbbbbbb (08/04/2009 01:00:42.713)(:) cd cdc d ddddsksjdkssksksj (08/04/2009 01:02:42.716)(:) raarrarararararara (08/04/2009 01:07:43.036)(:ERROR) Port... (8 Replies)
Discussion started by: rdhanek
8 Replies

8. Shell Programming and Scripting

help in hour grep

i have this script that checks for yesterday date and also specific hour in that ----------------------------------------------------------------- TZ=`date +%Z`+24 ;a=`date +%Y-%m-%d %k` cd logs count=0 for i in DBMaint.log do cat $i | grep $a >> file12.txt done... (0 Replies)
Discussion started by: ali560045
0 Replies

9. UNIX for Dummies Questions & Answers

an hour less in 24 hour system

My program: __________________________________ #!/bin/ksh DAY=`date +%y%m%d` H=`date +%H` M=`date +%M` day=`date +%m/%d/%y` let h=$H-1 echo DAY $DAY echo H $H echo M $M echo day $day echo h $h _____________________________________ My result: (3 Replies)
Discussion started by: bobo
3 Replies

10. UNIX for Dummies Questions & Answers

1 Hour less

set DAY=`date +%y%m%d` set H=`date +%H` set M=`date +%M` mailx -s "$H-Mydata" myemail@mail.com<mydata I am looking to set the current hour to have 1 hour less in the subject header: For example: let's say the system time is 8 I want to have "7-Mydata" not "8-Mydata" Can some1... (6 Replies)
Discussion started by: bobo
6 Replies
Login or Register to Ask a Question