Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to convert mtime using strftime() in perl

by Anonymous Monk
on Jun 28, 2006 at 18:38 UTC ( #558113=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am having a code to convert mtime to yyyymmdd hhmm using strftime function.But i am getting error as follows,see the code
use POSIX qw(strftime); $mtime = 1151492749; $systime = localtime($mtime); print"Systime=> $systime\n"; $now_string = strftime "%a %b %e %H:%M:%S %Y", $systime; print"New stirng=> $now_string\n";
output:-
Systime=> Wed Jun 28 16:35:49 2006 Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1 +, yday = -1, isdst = -1) at one.pl line 46.
When I run this code i am getting this issue,Now I want the output as New stirng=> 20060628 1635.How I can get this.Please help me.
Thanks.

Replies are listed 'Best First'.
Re: How to convert mtime using strftime() in perl
by Zaxo (Archbishop) on Jun 28, 2006 at 18:44 UTC

    You're calling localtime in scalar context; strftime wants a list. You can just dispose of the $systime variable and say,

    $now_string = strftime '%Y%m%d %H%M', localtime $mtime;
    I threw in the strftime format string for what you want.

    After Compline,
    Zaxo

Re: How to convert mtime using strftime() in perl
by Fletch (Chancellor) on Jun 28, 2006 at 18:46 UTC

    strftime needs a list of values. You call localtime in scalar context, making it return just a pre-formatted string version of the time. Either use an array to hold the return value

    my @systime = localtime( $mtime ); my $str = strftime( "%a %b %e %H:%M:%S %Y", @systime );

    or call localtime when you call strftime

    my $str = strftime( "%a %b %e %H:%M:%S %Y", localtime( $mtime ) );
Re: How to convert mtime using strftime() in perl
by madtoperl (Monk) on Jun 28, 2006 at 19:00 UTC
    Hi,

    see the following code will give your required output,
    my $new_string = strftime( "%Y%m%d %H%M", localtime( $mtime ) ); print"New string=> $new_string\n";
    output:-
    New String=> 20060628 1635
    Thanks and Regards,
    madtoperl.
      I've tried everything and I get the same error as the original post.
      my $mtime = stat( $map_fpath )->mtime; my $map_time = strftime( "%Y%m%d %H%M", localtime( $mtime ) );
      Output:
      Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1 +, yday = -1, isdst = -1)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://558113]
Approved by Hue-Bond
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2016-07-26 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (236 votes). Check out past polls.