#!/usr/local/bin/perl -Tw # # $Id: yesterday,v 1.2 2000/09/26 18:59:29 dgregor Exp $ # # yesterday -- figure out yesterday's date # - Useful for scripts that rotate logs out of cron at midnight. # # By Daniel J. Gregor, Jr., # Placed in the Public Domain on Wed Dec 16 01:40:27 EST 1998 # # The goal is to accurately find out yesterday's date, so what we do is # find the first second of today, sutract one, and we have yesterday. :) # Thanks to localtime() and timelocal(), this is quite easy: # - Get current time from time(), convert to an array with localtime(). # - Zero out the seconds, minutes, and hours values in the array. # - Convert the array back into seconds since epoch. # - Subtract one from the previous value, and we are now in yesterday # (I hope there aren't any exceptions to this). # - Convert back to an array, zero out seconds, minutes, and hours, # and we are now at the beginning of yesterday. # - Print out the fruits of our labor in a useful format. use Time::Local; use Getopt::Std; use POSIX; Getopt::Std::getopts('f:', \%opts); # get current time @timearray = localtime(time()); # set the seconds, minutes, and hours to zero $timearray[0] = 0; # seconds $timearray[1] = 0; # minutes $timearray[2] = 0; # hours # we now have the first second of today $time = Time::Local::timelocal(@timearray); # now the last second of yesterday @timearray = localtime($time - 1); # set the seconds, minutes, and hours to zero $timearray[0] = 0; # seconds $timearray[1] = 0; # minutes $timearray[2] = 0; # hours # first second of yesterday $time = Time::Local::timelocal(@timearray); # do something useful.... print out the time. if (defined($opts{'f'})) { print POSIX::strftime($opts{'f'}, localtime($time)), "\n"; } else { print scalar(localtime($time)), "\n"; }