in reply to questions about strftime

You needn't subtract anything. If you called strftime without doing so, it would work just fine. However, 86400 is the number of seconds in a day, so subtracting that amount from a time will give you 24 hours before that time. I don't know enough about what you're trying to do to know if that makes sense or not, though.

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

Replies are listed 'Best First'.
Re^2: questions about strftime
by tcf03 (Deacon) on Jan 10, 2005 at 22:05 UTC
    Here is the whole of it. Basically what it does is parses a file made by a login script each time a user logs in and matches it up with data from a a patchlink server (patchlink.com) running SQL server. It parses the data from these two sources and nags the users about PCs which have not logged onto our domain in 60 days and have also made no contact with the patchlink server. Its Kludgey - but its working, now Im working on cleaning up the code...
    # Script: OldAgents.pl # Find all old Patchlink Agents # TFiedler # We are dependant on these... use Win32::ODBC; use Win32::NetResource; use Win32::WinError; use POSIX qw(strftime); use Net::SMTP; # Connect to the udrive my %NetResource = ( LocalName => "U:", RemoteName => "\\\\SERVER\\share" ); my $User = "DOMAIN\\USER"; my $Password = "PASSWORD"; Win32::NetResource::AddConnection( \%NetResource, $Password, $User, 1 +); # If you are having problems connecting uncomment the following few li +nes and comment # out the line above... #if( Win32::NetResource::AddConnection( \%NetResource, $Password, $Use +r, 1 ) ) #{ # print "Successful\n"; #} #else #{ # print NetError(); #} # End connection to udrive routine... if ( -f "oldagents.txt" ) { print "Deleting old data file\n"; unlink("oldagents.txt"); print "Continuing...\n"; } else { print "Data file does not exist.\nContinuing...\n"; } open (OLDAGENTS,">oldagents.txt") || die "unable to open file c:\worki +ng\oldagents.txt\n"; # Lets calculate 60 days ago from NOW! # We are doing this in seconds. # The next 3 lines of code were gotten # from Perlmonks.org... my $thisday = time; my $SixtyDaysPrevious = $thisday - 60*24*60*60; $SixtyDaysPrevious = strftime "%m/%d/%Y", (localtime($SixtyDaysPreviou +s)); # End of perlmonks code. my $db = new Win32::ODBC( "DSN=patchlink_PLUS" ); # does this DB actually exist? if ( ! $db ) { die "Error connecting: " . Win32::ODBC::Error() . "\n"; } # YES! get the data... if ( $db->Sql ( "SELECT AgentName, LastContactDate, AgentVersion from +PLUS.dbo.UP_Agents where LastContactDate < '$SixtyDaysPrevious' order by LastContactDate" )) { # Ah you screwed UP! print "Error submitting SQL statement: " . $db->Error() . "\n"; } else { # Show me the money use Time::localtime; while( $db->FetchRow() ) { my @DATA=$db->Data ("AgentName", "LastContactDate", "AgentVers +ion"); my (undef, undef, $agent) = split(/\\/, $DATA[0]); # if you think you are getting bad data uncomment the # line below and watch the output. You should see # pcnames one per line w/o the \\ ie FIEDLER not \\FIEDLER # print "$agent\n"; my $file="u:\\usr\\NAV\\OS\\$agent\.txt"; # Added 12/07/04 (TF) if ( -f $file ) { open (PCFILE, "$file") || warn "Unable to open $agent.txt\ +n"; while (my @line = <PCFILE>) { foreach my $info(@line) { my (undef, $login, $os, $sp, $lc, $ie, $ip, undef) + = split(",",$info); my $modtime = (stat($file))[9]; $modtime = $modtime - 86400; if ( $modtime == "" ) { $modtime = "null"; } else { $modtime = POSIX::strftime ("%B %d",$modtime,0 +,0,0,0,0,0,0,0); #$modtime = POSIX::strftime ("%D",$modtime,0,0 +,0,0,0,0,0,0); } printf OLDAGENTS "Agent: %s\nAgent Version: %s\nLa +st Contact Date: %s\nUser: %s\nLast login: %s\nLast IP: %s\n\n", $DATA[0], $DATA[2], $DATA[1], $login, $modtime, $i +p; # NOW Lets NAG the user... open (LUSER, ">tmp/$agent.txt") || die "unable to +create $agent.txt\n"; printf LUSER "Agent: %s\nAgent Version: %s\nLast C +ontact Date: %s\nUser: %s\nLast in: %s\nLast IP: %s\n\n", $DATA[0], $DATA[2], $DATA[1], $login, $modtime, $i +p; close (LUSER); open (LUSER, "tmp/$agent.txt") || die "unable to o +pen $agent.txt\n"; $/=undef; # Sluuuuurp... while (my @EMAIL=<LUSER>) { $smtp=Net::SMTP->new('smtpserver.domain.com'); $smtp->mail( $ENV{USER} ); #$smtp->to('USER\@domain.com');# For testing $smtp->to("$login\@domain.com"); #$smtp->cc('USER2\@domain.com'); #$smtp->bcc('USER3\@domain.com'); $smtp->data(); $smtp->datasend("FROM: help\@domain.com\n"); $smtp->datasend("SUBJECT: PC name $agent\n"); $smtp->datasend("Below is a record of a PC tha +t was last logged into by you. This PC has not been\n"); $smtp->datasend("logged into the domain since +$modtime. If you use this PC occasionally,\n"); $smtp->datasend("please make sure that it is t +urned on for at least four hours per month. If you no\n"); $smtp->datasend("longer are in posession of th +is PC, please contact the help desk with the information\n"); $smtp->datasend("below. If you use this PC on +a regular basis please contact the help desk with the\n"); $smtp->datasend("information below.\n\nThank Y +ou\n\n"); foreach my $line(@EMAIL) { $smtp->datasend($line); } $smtp->dataend(); $smtp->quit; } #unlink ("tmp/$agent.txt"); } } } else { my $modtime = (stat($file))[9]; $modtime = $modtime - 86400; if ( $modtime == "" ) { $modtime = "null"; } else { $modtime = POSIX::strftime ("%B %d",$modtime,0,0,0,0,0 +,0,0,0); #$modtime = POSIX::strftime ("%F",$modtime,0,0,0,0,0,0 +,0,0); } printf OLDAGENTS "PC: %s\nAgent Version: %s\nLast Patchlin +k contact: %s\nLast domain login: %s\n\n", $DATA[0], $DATA[2], $DATA[1], $modtime; } } } close(OLDAGENTS); open (OLDAGENTS, "oldagents.txt") || die "unable to open oldagents.txt +\n"; $/=undef; # Sluuuuurp... while (@OLDA=<OLDAGENTS>) { $smtp=Net::SMTP->new('smtp.domain.com'); $smtp->mail( $ENV{USER} ); $smtp->to('USER1\@domain.com'); $smtp->cc('USER2\@domain.com'); $smtp->bcc('USER3\@domain.com'); $smtp->data(); $smtp->datasend("FROM: Patchlink\@Patchlink.domain.com\n"); $smtp->datasend("SUBJECT: Expired patchlink agents\n"); $smtp->datasend("Patchlink agents 60 days old or older:\n\n\n"); foreach $line(@OLDA) { $smtp->datasend($line); } $smtp->dataend(); $smtp->quit; } # Clean up... close(OLDAGENTS); # close open files $db->Close(); # close the DB Win32::NetResource::CancelConnection( "U:", 1, 1); # close the udrive! +!! # Subs sub NetError { my( $Error, $Text, $Provider ); $Error = Win32::GetLastError(); if( ERROR_EXTENDED_ERROR == $Error ) { Win32::NetResource::WNetGetLastError( $Error, $Text, $Provider + ); $Result = "Error: $Error: $Text (Genereated by $Provider)"; } else { $Text=Win32::FormatMessage( $Error ); $Result="Error: $Error: $Text"; } return $Result }
      This code is *ahem* interesting. Does it report the correct date/time in the e-mail? I don't see any reason to subtract a day's worth of seconds...seems arbitrary.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        yes - it does report the correct date in the email. I know the code isn't the greatest - Im working on improving it - is it poor style, or just bad code? Im not a programmer, just a hack. I am very interested in the things im doing wrong if anyone cares to chime in...