You're trying to use POSIX::strftime()
incorrectly. It's always a good idea to check the documentation, which says this:
strftime
Convert date and time information to string. Returns the string.
Synopsis:
strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
The month (mon
), weekday (wday
), and yearday (yday
) begin at zero, i.e., January is 0, not 1; Sunday is 0, not 1; January 1st is 0, not 1. The year (year ) is given in years since 1900, i.e., the year 1995 is 95; the year 2001 is 101. Consult your system's strftime()
manpage for details about these and the other arguments.
So passing it a string containing a random(ish) representation of a datetime was slightly optimistic.
You need to parse your string and extract the bits that you need to pass to strftime()
. As you've already be shown, the Modern Perl way to do that is to use a module like Time::Piece. But it's perfectly possible to do it with standard Perl too.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use POSIX 'strftime';
my $time = 'Fri Jan 8 14:24:27 2016';
my ($day, $mon, $date, $hr, $min, $sec, $year) = split /[\s:]+/, $time;
my %months = (
Jan => 0, Feb => 1, Mar => 2, Apr => 3,
May => 4, Jun => 5, Jul => 6, Aug => 7,
Sep => 8, Oct => 9, Nov => 10, Dec => 11,
);
$time = strftime('%Y%m%d',
$sec, $min, $hr, $date, $months{$mon}, $year - 1900);
say $time;
But don't do that. Use a module.