Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Compare field andsend mail

by Cristoforo (Deacon)
on Apr 13, 2011 at 01:50 UTC ( #899078=note: print w/replies, xml ) Need Help??

in reply to Compare field andsend mail

I would suggest calculating the time to be compared against once at the beginning of the script, and directly compare that with the date/time you extract from the file you are reading. The same type problem was discussed here.

One advantage you have in your file is that the format for the date/time sorts naturally because the most significant parts of the date begin at the left going to the right.

That is 'YYYY/MM/DD HH:MM:SS' is in a good order for sorts or comparisons.

#!/usr/dist/share/perl,v5.003/5bin.sun4/perl use strict; use warnings; use Time::Local qw/ timelocal_nocheck /;; use POSIX qw/ strftime /; my $secs = 3600; # 1 hour my ($s, $m, $h, $d, $mon, $y) = localtime; my $time_ago = strftime "%Y/%m/%d %H:%M:%S", localtime timelocal_nocheck $s - $secs, $m, $h, $d, $mon, $y; print "$time_ago was one hour ago\n"; my $mailprog = "|/usr/sbin/sendmail -t"; my $lst_email = ''; # DATA should be saved as the spaecial filehandle # for self-contained programs (like this one) + #open (my $in, "/mylist.log") || die ("Can't Open data File: $!\n"); # while (<$in>) { while (<DATA>) { chomp; my ($datestamp, $status, $location) = split /\|/; if ($datestamp lt $time_ago && $status eq 'red') { # do email notification print "red for more than an hour\n$_\n"; } } __DATA__ 2011/04/12 12:50:24|red|florida 2011/04/12 15:20:21|green|tampa 2011/04/12 15:30:12|red|miami
Update: I hour ago could have been written:

my $hrs = 1;

and then subtracted from $h in the $time_ago formation.

my $time_ago = strftime "%Y/%m/%d %H:%M:%S", localtime timelocal_nocheck $s, $m, $h - $hrs, $d, $mon, $y;
To avoid possible errors in the time calculation, you could use a module made for dealing with dates, like DateTime.

use DateTime; my $date = DateTime->now->subtract(hours => 1); my $hr_before = $date->set_time_zone('local')->strftime("%Y/%m/%d %H:% +M:%S");

Replies are listed 'Best First'.
Re^2: Compare field andsend mail
by britney (Acolyte) on Apr 13, 2011 at 18:37 UTC
    Thanks for teaching me this, it remind me now. BTW, how do we do this in perl
    $today = timelocal(localtime); my $lastaction = &date_to_unix($datestamp); sub date_to_unix() { my ($year,$mon,$day,$hour,$min,$sec) = $_[0] =~ /(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/; return undef unless ($day and $mon and $year); return timelocal(0,$min,$hour,$day,$mon-1,$year-1900); } $duration = ($today - $lastaction)/3600;
    How do i get $duration in hours only like 6 hrs instead of 6.6565656657 (6.6 is okay, i can live with 6) I know we have something to ignore remainder but i cannot find it in google :-) Thanks again. == It work now, i used this code
    $durationTemp = ($today - $lastaction) / 3600; $duration=sprintf("%d",$durationTemp);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://899078]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-05-28 11:28 GMT
Find Nodes?
    Voting Booth?