Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Ctime/Mtime compare with string date

by bimleshsharma (Beadle)
on Jan 18, 2012 at 13:26 UTC ( #948545=perlquestion: print w/replies, xml ) 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

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948545]
Approved by marto
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
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.