Available from BrightTools, Activeworx Security Center includes new features for meeting SOX and HIPAA compliance requirements, improved tools for forensic investigation, and much more.

Recent Articles

Simple Schedule
I often get asked for web-based scheduling programs. I've done quite a few of them over the years, sometimes using scripts available from the web, but more often writing my own simply because I don't like modifying other people's code.

A Ps Problem With BBX
Someone recently asked me about increasing command width for 'ps' on SCO Unix.

Perl 'Eval' for Data Validation
I recently did a little web based Service Schedule. This is something that gets its data from another program: in other words, the details...

Rounding Time

It was a long, hard day. Up early to clean a room of furniture so that new carpet can be put in later this week, off to...

Controlling Concurrent Runs with Perl

Sometimes you have a program that can't be run by more than one person, or one that must run frequently...




WebProWorld Top Threads

KinderStart Sues Google Over PageRank
KinderStart filed suit yesterday in U.S. District Court in San Jose alleging that Google improperly "blacklisted" its website...

POLL: Are we and Google at risk?
Judges have tried to close other websites already, violating our 1st amendment freedoms of press and of speech. We must say no to this. Is there a website we can have a say on and a vote?

Has web design gotten easier?
Over the years the price of web design services on average have reduced. Is this because web design has become easier or because it has become more mainstream?
Perhaps it's because of competition from LEDCs?
What dya think?

Working for friends
Where does everyone stand on the issue of working for friends? Do you find it difficult to keep work and personal life separate? Does your friend try to use the friendship as leverage for getting a lower quote out of you for the services you provide?

03.21.06


File Date Comparison

By A.P. Lawrence

Sometimes you want to use the date of a file somewhere else. For this example, we'll use the case where a file shouldn't be overwritten if it was created or changed today.

If we are not entirely sure of the parameters, we should stop right there: what does "today" mean? Does it mean anytime after midnight of the day before, or does it mean within the past 24 hours? I have seen "today" actually mean the latter more than once, so keep that in mind when people are loosely tossing around "today".

We also should be asking which file time we need to work with. Is the creation time, the access time, or the modified time? When talking to non-programmers, even that isn't enough, because the "creation" time might be misinterpreted; if they opened an existing file and replaced its contents, they may think of that as "creation" rather than modification.

But for today, we'll keeo it simple. We have a file, and it really is the creation date we want to look at, and "today" really is anything after midnight yesterday. We want to know if the file was created today.

By the way, I got yelled at for forgetting to mention that "ctime" is "changed time", not "creation time". More precisely, it's the inode change time. If you chmod a file, ctime changes. If you have not done anything to change the inode, ctime is creation time.

But: as this page noted while casting shame upon me and others who carelessly referred to ctime as creation time, some filesystems track four times, and one actually is the real creation time. FreeBSD's inode includes a "btime" (birth time) holder. It's still somewhat useless, as most utilities are unaware of the "birth time" even if the file system does support it. But we digress:

If we are fortunate enough to be working on a Linux platform, we have the "stat" command available. It's worth reading up on "stat"'s man page, but there are a couple of easy ways to get the creation date. Let's say our file is "z" and try a few things:

$ ls -l z
-rw-r--r-- 1 apl staff 2552 Sep 4 16:08 z
$ stat z
234881029 1802523 -rw-r--r-- 1 apl staff 0 2552 "Sep 5 13:54:27 2005" "Sep 4 16:08:15 2005" "Sep 4 16:08:15 2005" 4096 8 0 z
$ stat -f "%Sc" z
Sep 4 16:08:15 2005


If we don't have stat, the shell becomes trickier because "ls -l" outputs differently depending on the modification time of the file; if it is more than 6 months in the past (or future), then the year of the last modification is displayed in place of the hour and minute fields. That forces us to look at the field to see if it has a ":" in it. Something like this will do it:

set -- `ls -l $1`
year=$8
thisyear=`date "+%Y"`
case $8 in
   *:*) year=$thisyear;;
esac
echo $year


But I will have switched to Perl long before I'd get into that ugly mess. Perl has "stat", and we'd use it like this:

#!/usr/bin/perl
$rfile=shift @ARGV;
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
$mtime,$ctime,$blksize,$blocks)=stat $rfile;
($sec, $min, $hour, $mday, $mon, $year) = localtime();
$today="$mday $mon $year";
($sec, $min, $hour, $mday, $mon, $year) = localtime($ctime);
$filedate="$mday $mon $year";
exit 1 if ($filedate eq $today);
exit 0;


You could use something like that in a shell script like this (assume the Perl script is called "filedate.pl")

filedate.pl filetocheck || exit 1
yourscript


Obviously the "stat" gives us much more than we need here. If all those unused variables annoy you, use an array slice:

($atime,$mtime,$ctime)=(stat($rfile))[8..10];

*Originally published at APLawrence.com

About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com



About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals

PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com


-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article




Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde