Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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";
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 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.

Comment on How to convert mtime using strftime() in perl
Select or Download Code
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,

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

    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";
    New String=> 20060628 1635
    Thanks and Regards,

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2015-12-01 01:13 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (790 votes), past polls