Search code examples
perl

How do I get the current year with Perl?


in a script, the variables are defined with "my".

I now have the variable

my $timestamp = time();

inserted and would now like to output the year.

With

my $timestamp = time();
my $year=$timestamp;

the timestamp is displayed correctly in the print section with $year.

How do I have to define $year so that the date, e.g. date(Y) is output?


Solution

  • Use localtime instead.

    my @t = localtime;
    print $t[5] + 1900;
    

    Or reach for something more highlevel:

    use Time::Piece;
    my $time = localtime;
    print $time->year;
    

    See Time::Piece for details.