Perl print the current year in four-digit format

how can i get the current year in 4 digits, this is what i tried

 #!/usr/local/bin/perl

 @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
 @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
 $year = $year+1900;
 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
 print "DBR_ $year\\$months[$mon]\\Failures_input\\Failures$mday$months[$mon].csv \n";

Will print DBR_ 114\Apr\Failures_input\Failures27Apr.csv

How do I get 2014?

I am using the version 5.8.8 build 820.

+4
source share
4 answers

Move the line:

$year = $year+1900;

After this call localtime()and so that it becomes:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year = $year+1900;
+7
source
use Time::Piece;

my $t = Time::Piece->new();
print $t->year;
+8
source

:

#!/usr/bin/perl

use POSIX qw(strftime);

$year = strftime "%Y", localtime;

printf("year %02d", $year);
+3

- Time::Piece. localtime, Time::Piece, , . (localtime , , .)

strftime /, .

This very concise program creates the path to the file that I think you need (I doubt if there should be a space after DBR_?). Note that there is no need to double the backslash inside a single quote string if it is not the last character of the string.

use strict
use warnings;

use Time::Piece;

my $path = localtime->strftime('DBR_%Y\%b\Failures_input\Failures%m%d.csv');

print $path;

Output

DBR_2014\Apr\Failures_input\Failures27Apr.csv
+3
source

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


All Articles