Solved

Easy Date Question(100 pts)

Posted on 1998-12-10
8
136 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hard perl script 16 156
Perl script to parse xml file 2 111
Exchange 2010 Transport Rule Regex 28 107
Log File Creation with Header and Footer 17 93
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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