### Ctime/Mtime compare with string date

 on Jan 18, 2012 at 13:26 UTC Need Help??
bimleshsharma has asked for the wisdom of the Perl Monks concerning the following question:

How do we compare ctime/mtime to string date? for example: i want to read those files from folder having creation date is greater than the string date that is stored in a txt file say "date.txt".

Replies are listed 'Best First'.
Re: Ctime/Mtime compare with string date
by moritz (Cardinal) on Jan 18, 2012 at 13:33 UTC

You'll probably have to parse the string date, and compare that to what localtime or gmtime make of the timestamp.

Thanks, I created subroutine to do this that will return -1 if lesser, 0 for equal and 1 for greater.

```sub Date_Comp
{
my %months= {Jan => 1,Feb => 2,Mar => 3,Apr => 4,May => 5,Jun =>
+6,Jul => 7,Aug => 8,Sep => 9,Oct => 10,Nov => 11,Dec => 12};
my(\$date1, \$date2)= @_;
my @dates1= split (" ",\$date1);
my @dates2= split (" ",\$date2);
my @times1= split (":",\$dates1[3]);
my @times2= split (":",\$dates2[3]);

my \$days1= \$dates1[4]*365 + \$months{\$dates1[1]}*30 + \$dates1[2];p
+rint "\ndays1= \$days1";
my \$days2= \$dates2[4]*365 + \$months{\$dates2[1]}*30 + \$dates2[2];p
+rint "\ndays2= \$days2";
my \$time1= \$times1[0]*60*60 + \$times1[1]*60 + \$times1[2]; print "
+\ntimes1= \$time1";
my \$time2= \$times2[0]*60*60 + \$times2[1]*60 + \$times2[2]; print "
+\ntimes2= \$time2";

if (\$days1 > \$days2)
{ print "\n \$date1 is latest \n";    return 1    }
elsif (\$days1 < \$days2)
{ print "\n \$date2 is latest \n";    return -1    }
elsif (\$days1 == \$days2)
{
if (\$time1>\$time2)
{print "\n \$date1 is latest \n"; return 1    }
elsif(\$time1<\$time2)
{print "\n \$date2 is older \n";    return -1}
elsif(\$time1 == \$time2)
{print "\n Both are same \n"; return 0    }
}
}
Some years (leap years) have an extra day and not all months have 30 days. I think this will affect your calculations. The time functions that go back and forth to epoch time know about these details.

Another technique is that if you can get the date/time string to look like this: "2011-10-01 1601" or similar, you can just use: \$datetime1 cmp \$datetime2. The key is that you need leading zeroes for the dates and times or the ascii sort order won't work out (07:02 is not the same as 7:02).

You will be ok on mtime, but see AnonMonk's post re:ctime.

Re: Ctime/Mtime compare with string date
by mr.nick (Chaplain) on Jan 18, 2012 at 14:04 UTC
It's probably best to convert the string-based date into a usable format first, like seconds since the epoch. I normally use Date::Parse for that.

mr.nick ...

Re: Ctime/Mtime compare with string date
by Anonymous Monk on Jan 18, 2012 at 13:39 UTC
```\$ perldoc -f stat |grep -i time
\$atime,\$mtime,\$ctime,\$blksize,\$blocks)
8 atime    last access time in seconds since the epoch
9 mtime    last modify time in seconds since the epoch
10 ctime    inode change time in seconds since the epoch (*)
Notably, the ctime field is non-portable. In particular, you
cannot expect it to be a "creation time"; see "Files and
printf "File is %s, size is %s, perm %04o, mtime %s\n",
scalar localtime \$sb->mtime;

Create A New User
Node Status?
node history
Node Type: perlquestion [id://948545]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-06-24 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?