Solved

Easy Date Question(100 pts)

Posted on 1998-12-10
8
138 Views
Last Modified: 2010-03-05
   Hey All,
I have a Perl script that writes to a text file the following string, ONLY on one line:

"Server Name","Server IP","Server Port","Server Version","Server Mods","Hour:Minute Posted","Author"

What I would like this script to do is to check the Hour:Minute of the server posting and if this value is greater than 8 hours(to the current time), then that line should be deleated.  Any ideas?

Peter Maag
Webmaster
Hilligoss.com
0
Comment
Question by:petermaag
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Expert Comment

by:flivauda
ID: 1206880
Are you saving the day as well?  or just the hour minute?  Can you give me a couple line sin your data file?
0
 
LVL 1

Expert Comment

by:flivauda
ID: 1206881
Are you saving the day as well?  or just the hour minute?  Can you give me a couple line sin your data file?
0
 

Author Comment

by:petermaag
ID: 1206882
  Flivauda,
Nope, I am just saving the hour and the minute.  Here is an example from the file:

"Peter's Server","220.90.145.113","2346","1.04","NATO","15:47","admin"

In this case the time would be 15:47 .  What I need to do is extract this time, and then see if it has been there longer than 8 hours.(From the current time).

HTH

Peter
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 1206883
Should we then assume that it will never be there longer than 24 hours from the current time?
0
 

Author Comment

by:petermaag
ID: 1206884
Yeah, these posts will never be more than 24 hours old
0
 
LVL 1

Accepted Solution

by:
flivauda earned 100 total points
ID: 1206885
I predict you should also save the day if you want to do it correctly just because how do I know if 10:15 is 8 hours older than 13:15.  it would be if 10:15 was from the day before but not if it was from today.  (Assuming our current time is 13:15)
You could try something like this:
-------
$oldHour = 10;
$oldMin = 34;

$currentHour = 18;
$currentMin = 34;

$basetime = ($currentHour * 60) + $currentMin;
$oldTime = ($oldHour * 60) + $oldMin;


print "Basetime: $basetime\nOldtime: $oldTime\n";
if (($basetime - 480) >= $oldTime)
{
  print "erase\n";
  # erase the line here
}

where the old hour and minute are set from reading in your file and the current hour and minute are gotten from the system clock.  The scenario above will work.  But it will fail if I enter something on 17:10 one day and your program checks it at 18:10 the next day which is 25 hours passing.

Basically without saving the day you cant do it.  The above code will work for some of the times.  But if I were you I would modify the program that saves the file to also save the date and then this is a trival problem.  If you do that I can help you with the code
0
 
LVL 1

Expert Comment

by:flivauda
ID: 1206886
didnt see the gaurantee of only 24 hours old you could add one line to it and use this code and it should work for you:



$oldHour = 10;
$oldMin = 34;

$currentHour = 9;
$currentMin = 34;

if ($currentHour < $oldHour) { $currentHour += 24; }

$basetime = ($currentHour * 60) + $currentMin;
$oldTime = ($oldHour * 60) + $oldMin;


print "Basetime: $basetime\nOldtime: $oldTime\n";
if (($basetime - 480) >= $oldTime)
{
  print "erase\n";
  # erase the line here
}





0
 
LVL 84

Expert Comment

by:ozo
ID: 1206887
$filename = "text.file";
$tempname = "temp.$$";
open(FILE, "<$filename") or die "Unable to open $filename $!";
open(TEMP,">$tempname") or die "Unable to open $tempname $!";
use Fcntl ':flock';
flock(FILE, LOCK_EX);
($h,$m)=(localtime)[2,1];
$t = ($h*60 + $m);
while( <FILE> ){
      print STDERR "can't find date: $_" unless ($h,$m) = ((split/,/)[5] =~ /(\d+):(\d+)/);
      print TEMP if( ($t - ($h*60+$m))%(24*60) < 8*60 );
}
close TEMP;
rename $tempname,$filename or die "can't rename $tempname,$filename $!";
close FILE;
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question