Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to convert mtime using strftime() in perl

by Anonymous Monk
on Jun 28, 2006 at 18:38 UTC ( [id://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 (Bishop) 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 ) );
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found