CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Why doesn't strftime() work?

 



bulrush
User

May 26, 2016, 7:26 AM

Post #1 of 4 (1686 views)
Why doesn't strftime() work? Can't Post

The docs were real clear and this should be real simple, but I can't get strftime to work. Why not? Here's my test program. I'm using Strawberry Perl 5.24 in a Windows 7 cmd window.


Code
# test.pl 
use warnings;
use strict;
use Fcntl;
use POSIX qw(strftime);

my(@a,@b,$in,$num,$olds,$s,$t);
my($fmt);

$t=$^V;
print "Strawberry Perl version: $t\n\n";

my $l=localtime();
print "localtime=$l\n";

$fmt="%T";
$t=strftime $fmt,localtime;
print "$fmt= $t\n";

$t=strftime("%T",localtime);
print "$fmt= $t\n";

exit;

-----


bulrush
User

May 26, 2016, 7:46 AM

Post #2 of 4 (1685 views)
Re: [bulrush] Why doesn't strftime() work? [In reply to] Can't Post

Apparently %r and %F are not supported in Strawberry Perl, but they are on my regular Linux perl. This code works:


Code
#!/usr/bin/perl 
# test.pl
#use warnings;
#use strict;
#use Fcntl;
use POSIX qw(strftime);

my(@a,@b,$s,$t);
my($fmt);

print "Strawberry Perl version: $^V\n\n";

my $l=localtime();
print "localtime=$l\n";

$fmt="%H:%M:%S";
$t=strftime ($fmt,localtime(time));
print "$fmt= $t\n";

$t="%Y-%m-%d";
$t=strftime($t,localtime(time));
print "$fmt= $t\n";

exit;

-----


bulrush
User

May 26, 2016, 8:05 AM

Post #3 of 4 (1683 views)
Re: [bulrush] Why doesn't strftime() work? [In reply to] Can't Post

 And this is how I check that a strftime formatting code works with generic codes only, like Strawberry Perl uses.


Code
############################################################# 
# Check formatting codes first then return current date or
# time according to correct format codes.
sub getstrftime
{my($fmt)=@_;
my(@a,@b,$i,$j,$procname,$s,$t);
my(@codes,$c,$errcnt,$out);

$procname="getstrftime";

$errcnt=0;
# These are valid formatting codes. %F not supported in
# Strawberry perl.
my %valid=('%Y', 1,
'%y',1,
'%d',1,
'%m',1,
'%H',1,
'%I',1,
'%M',1,
'%S',1,
'%p',1,
'%j',1,
'%U',1,
'%w',1
);
@codes=($fmt=~m/%[a-zA-Z]/g);
for ($i=0; $i<=$#codes; $i++)
{
$c=$codes[$i];
if (! exists $valid{$c})
{
$s="$procname ERROR: '$c' is not a valid code for generic strftime().";
print "$s\n";
$errcnt++;
}
}
if ($errcnt>0)
{
print "$procname: Fix errors and try again.\n";
exit 1;
}

$out=strftime($fmt,localtime(time));

return $out; # getstrftime
}

-----


Laurent_R
Veteran / Moderator

May 26, 2016, 2:53 PM

Post #4 of 4 (1681 views)
Re: [bulrush] Why doesn't strftime() work? [In reply to] Can't Post

You appear to have solved your problem, but I just want to point out that POSIX is really a Unix-related standard. Although the module has been ported to Windows, you probably can't expect everything to work as in Unix or Linux.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives