title: Error in elapsed time since modification of file using "DateTime"
body: <p>I am trying to clean up a rather large file system. I use the <code>stat</code> function to obtain the modification time of each file.</p>
  
  <p>According to <a href="https://perldoc.perl.org/functions/stat.html" rel="nofollow noreferrer" title="perldoc"><code>perldoc -f stat</code></a>, the tenth element of the returned list is the last modified time in seconds since the epoch.</p>
  
  <p>I use <code>DateTime-&gt;from_epoch</code> and subtract <code>DateTime-&gt;now</code> to calculate the age of the fule</p>
  
  <pre><code>    #!/usr/bin/perl
  
      use strict;
      use warnings;
  
      use DateTime;
  
      my $now = DateTime-&gt;now();
      #my $now = DateTime-&gt;now( time_zone =&gt; "America/New_York" );
  
      $self-&gt;{dir} = '/tmp/test';
      opendir(DIR, $self-&gt;{dir}) or die $@;
      my @files = grep(/\.txt$/, readdir(DIR));
      closedir(DIR);
  
      for ( @files ) {
  
              my $file = stat($self-&gt;{dir} . '/' . $_);
              my $mtime = DateTime-&gt;from_epoch(epoch =&gt; $file-&gt;mtime);
              #my $mtime = DateTime-&gt;from_epoch(epoch =&gt; $file-&gt;mtime, time_zone=&gt; "America/New_York");
              my $elapsed = $now - $mtime;
              push(@{$self-&gt;{stale}}, {file =&gt; $self-&gt;{dir} . '/' . $_, mtime =&gt; $elapsed-&gt;in_units('minutes')}) if $elapsed-&gt;in_units('minutes') &gt; 15;
              push(@{$self-&gt;{remove}}, {file =&gt; $self-&gt;{dir} . '/' . $_, mtime =&gt; $elapsed-&gt;in_units('days')}) if $elapsed-&gt;in_units('days') &gt; 10;
      }
  </code></pre>
  
  <p>If I manually create test files and change the modification time, the result is off by 30 days</p>
  
  <pre><code>$ touch /tmp/test/test{100..104}.txt -d '-45 days'
  $ perl MTIME.pm 
  $VAR1 = {
            'mtime' =&gt; 15,
            'file' =&gt; '/tmp/test/test100.txt'
          }; $VAR1 = {
            'mtime' =&gt; 15,
            'file' =&gt; '/tmp/test/test104.txt'
          }; $VAR1 = {
            'mtime' =&gt; 15,
            'file' =&gt; '/tmp/test/test103.txt'
          }; $VAR1 = {
            'mtime' =&gt; 15,
            'file' =&gt; '/tmp/test/test101.txt'
          }; $VAR1 = {
            'mtime' =&gt; 15,
            'file' =&gt; '/tmp/test/test102.txt'
          };
  </code></pre>
  
  <p>I've tried <code>DateTime</code> objects both with and without the time zone set with no difference in results.</p>
  
  <pre><code>$ touch /tmp/test/test{100..104}.txt -d '-45 days'
  $ touch /tmp/test/test{105..110}.txt
  $ ll /tmp/test
  total 11
  -rw-r--r-- 1 root root    0 Apr  3 19:31 test100.txt
  -rw-r--r-- 1 root root    0 Apr  3 19:31 test101.txt
  -rw-r--r-- 1 root root    0 Apr  3 19:31 test102.txt
  -rw-r--r-- 1 root root    0 Apr  3 19:31 test103.txt
  -rw-r--r-- 1 root root    0 Apr  3 19:31 test104.txt
  -rw-r--r-- 1 root root    0 May 18 19:30 test105.txt
  -rw-r--r-- 1 root root    0 May 18 19:30 test106.txt
  -rw-r--r-- 1 root root    0 May 18 19:30 test107.txt
  -rw-r--r-- 1 root root    0 May 18 19:30 test108.txt
  -rw-r--r-- 1 root root    0 May 18 19:30 test109.txt
  -rw-r--r-- 1 root root    0 May 18 19:30 test110.txt
  </code></pre>
  
  <p>Working solution:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings 'all';
  
  use Data::Dumper;
  
  my $self = bless { }, 'My::Class';
  
  my @files = glob '/tmp/test/*.txt';
  
  for (@files) {
          my $days = int(-M $_);
          my $mins = int((time - (stat $_)[9]) / 60);
          my $item = {
                  file  =&gt; $_,
                  days =&gt; $days,
                  minutes =&gt; $mins
          };
          push @{ $self-&gt;{remove} }, $item if $days &gt; 10;
          push @{ $self-&gt;{stale} },  $item if $mins &gt; 15;
  }
  
  print Dumper $self;
  </code></pre>