How to change ctime to normal string representation?

Using File::stat, we can get ctimethis file. My question is how to change ctime, which means the inode change time in seconds from the epoch to the normal time representation like "2009-08-26 17:28:28". Is there a built-in module or module to solve this problem?

+3
source share
7 answers

The most standard way is to use the POSIX module and the strftime function.

use POSIX qw( strftime );
use File::stat;

my $stat_epoch = stat( 'some_file.name' )->ctime;
print strftime('%Y-%m-%d %H:%M:%S', localtime( $stat_epoch ) );

All of these markers, such as% Y,% m, etc., are defined in the standard and work identically in the C command, system "date" (at least on Unix), etc.

+9
source

, , ,

print scalar localtime stat($filename)->ctime;

. - "Wed Jun 10 19:25:16 2009". . GMT, < scalar gmtime.

localtime gmtime .

+3
use DateTime;
$dt = DateTime->from_epoch( epoch => $epoch );

datetime , . $year = $dt->year; .. , .

$epoch = 123456789;
$dt = DateTime->from_epoch( epoch => $epoch );
print $dt;

1973-11-29T21:33:09
0

:: localtime perldoc ctime() .

use File::stat;
use Time::localtime;

my $date_string = ctime(stat($file)->ctime);
0
use File::stat;
use Time::CTime;

$file = "BLAH BLAH BLAH";

$st = stat($file) or die "No $file: $!";
print strftime('%b %o', localtime($st->ctime));

#Feb 11th, for example.
0

, . Date::Format:

#!/usr/bin/perl
use strict;
use warnings;

use Date::Format;
use File::Stat;

my $fs = File::Stat->new( '.vimrc' );
my $mtime = $fs->ctime();
print time2str( "changed in %Y on %B, %o at %T\n", $mtime );
0
source

First of all, are you really using File::Stat, not File::Stat? If so, and if you have 5.8 or more, switch to File::Stat.

perldoc localtime

  • localtime EXPR
  • localtime

    Converts the time returned by the time function into a 9-element list with the time parsed for the local time zone.

...

In a scalar context, localtime()returns the value ctime(3):

Once you understand that an localtimeargument can take, then your mind will open to all the other possibilities mentioned in this thread.

#!/usr/bin/perl

use strict;
use warnings;

use File::stat;

my $stat = stat 't.pl';

print "$_\n" for $stat->ctime, scalar localtime($stat->ctime);
0
source

Source: https://habr.com/ru/post/1716080/


All Articles