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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Extract 12 hour time, convert to 24 and subtract 15 minutes?
# 1  
Old 06-15-2010
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.
Task,Room,Start,End,Date
What I need to do is open the file, pull the Start which is in 12Hour time, convert to 24Hour time and create a cron entry based on this.
Also subtract 15 minutes from the above time and create another cron entry (to kick off a warning that something is about to happen).
Then do the same for the End.

Is there a simple way to do this other than a big lookup table?
# 2  
Old 06-15-2010
could you share sample data from your text file?
# 3  
Old 06-15-2010
Here is one line of the file..
Code:
CycleFans,Storage1,22/05/2010,06:00 PM,22/05/2010,11:00 PM

I can safely ignore the date, as the file is created each day anyway, but the 6:00 PM I need to be in 24 Hour format for Cron, and also 5:45 PM (minus 15 mins for whatever the start time is) for the pre event warning.

The 15 minute pre event at the end is not so important...

The times will not always be on the hour...

Last edited by Yogesh Sawant; 06-21-2010 at 03:24 AM.. Reason: added code tags
# 4  
Old 06-15-2010
Quote:
Originally Posted by autotuner
... but the 6:00 PM I need to be in 24 Hour format for Cron...
Here's an idea:

Code:
$
$
$ cat times.txt
12:00 AM
12:15 AM
12:59 AM
01:00 AM
01:56 AM
09:30 AM
11:59 AM
12:00 PM
12:48 PM
12:59 PM
01:00 PM
01:33 PM
02:15 PM
05:55 PM
06:00 PM
10:24 PM
11:00 PM
11:59 PM
$
$
$ perl -ne 'chomp($x=$_); @t=split(/[: ]/,$x);
          $t[0]=$t[0]+12 if $t[2] eq "PM" and $t[0]<12;
          $t[0]=0 if $t[0]==12 and $t[2] eq "AM";
          printf("%s\t=>\t%02d:%02d\n",$x,$t[0],$t[1])' times.txt
12:00 AM        =>      00:00
12:15 AM        =>      00:15
12:59 AM        =>      00:59
01:00 AM        =>      01:00
01:56 AM        =>      01:56
09:30 AM        =>      09:30
11:59 AM        =>      11:59
12:00 PM        =>      12:00
12:48 PM        =>      12:48
12:59 PM        =>      12:59
01:00 PM        =>      13:00
01:33 PM        =>      13:33
02:15 PM        =>      14:15
05:55 PM        =>      17:55
06:00 PM        =>      18:00
10:24 PM        =>      22:24
11:00 PM        =>      23:00
11:59 PM        =>      23:59
$
$

tyler_durden
# 5  
Old 06-16-2010
Thanks, that get 2 parts of the cron out the way.. Now to figure out how to reliably subtract 15 minutes..
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 extract logs between the current time and the last 15 minutes ?

I want to extract the logs between the current time stamp and 15 minutes before and sent an email to the people configured. I developed the below script but it's not working properly; can someone help me?? I have a log file containing this pattern: Constructor QuartzJob ... (3 Replies)
Discussion started by: puneetkhullar
3 Replies

3. UNIX for Dummies Questions & Answers

Subtract minutes from date

Hi, I am reading a particular date from a file using below command WFLWDATE=$(sed '2q;d' FileA.prm) The echo command outputs the correct date in variable WFLWDATE Now I want to subtract 5 minutes from this variable. I am on AIX and unable to get anything working as expected. Can you... (1 Reply)
Discussion started by: vrupatel
1 Replies

4. 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

5. Shell Programming and Scripting

How to convert 24 Hr Time Format into Minutes?

Hello All I know the general Logic behind it but do not know the shell programming so much. For Example, The Time is stored in a given Variable if the Time is 0800 then i need to extract the last digits of the number and Add it to the Remaining Digit of the Number which is multiplied by... (7 Replies)
Discussion started by: Ajesh
7 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

Perl, time stamp issue. 60th minutes

Hi Guys, Could you tell me how you can get the time stamp for 60th minutes? Currently, we name our file using time stamp upto minutes and then add seconds at the end starting from 01. And when the seconds reaches 60 we simply add 1 to the time stamp and reset the seconds to 00. But the... (3 Replies)
Discussion started by: supaLucas
3 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