Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

Easy Date Question(100 pts)

   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
petermaag
Asked:
petermaag
  • 4
  • 2
  • 2
1 Solution
 
flivaudaCommented:
Are you saving the day as well?  or just the hour minute?  Can you give me a couple line sin your data file?
0
 
flivaudaCommented:
Are you saving the day as well?  or just the hour minute?  Can you give me a couple line sin your data file?
0
 
petermaagAuthor Commented:
  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!

 
ozoCommented:
Should we then assume that it will never be there longer than 24 hours from the current time?
0
 
petermaagAuthor Commented:
Yeah, these posts will never be more than 24 hours old
0
 
flivaudaCommented:
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
 
flivaudaCommented:
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
 
ozoCommented:
$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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now