Solved

Easy Date Question(100 pts)

Posted on 1998-12-10
8
134 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now