Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Shorter Formating?

by talwyn (Monk)
on Aug 14, 2002 at 01:54 UTC ( #189968=perlquestion: print w/ replies, xml ) Need Help??
talwyn has asked for the wisdom of the Perl Monks concerning the following question:

Ok, I'm a bit tired.... I would like some help in making
my ($sec, $min, $hour, $monthday, $month, $year, $weekday, $yday, $is +dst) = localtime ( time()); my $changedate = sprintf "%4d-%02d-%02d %02d:%02d:%02d", ($year + 1900),($month +1),$monthday,$hour,$min,$sec;
Just a bit shorter but still fairly obvious as to what I am doing. Any comments appreciated.

Comment on Shorter Formating?
Download Code
Re: Shorter Formating?
by BrowserUk (Pope) on Aug 14, 2002 at 02:07 UTC

    A little shorter and less temp vars?

    my @dt = localtime ( time()); my$changedate = sprintf "%4d-%02d-%02d %02d:%02d:%02d", ($dt[5] + 1900),($dt[4]+1), (@dt)[3,2,1,0];
(jeffa) Re: Shorter Formating?
by jeffa (Chancellor) on Aug 14, 2002 at 02:13 UTC
    my @time = reverse ((localtime)[0..5]); $time[0]+= 1900; $time[1]+= 1; my $changedate = sprintf "%4d-%02d-%02d %02d:%02d:%02d", @time;
    I think you can use mapcar to shorten it some more. Where's tye? ...

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Shorter Formating?
by Zaxo (Archbishop) on Aug 14, 2002 at 02:17 UTC

    &POSIX::strftime has all you want:

    use POSIX qw( strftime ); print strftime( '%Y-%m-%d %T', localtime);

    See 'man 2 strftime' for all the format codes available. POSIX man page tells about portability and perlish concerns.

    Update: Shortened the format with %T; was %H:%M:%S.

    After Compline,
    Zaxo

Re: Shorter Formating?
by screamingeagle (Curate) on Aug 14, 2002 at 05:12 UTC
    well, i'm only being half-serious here :0)
    if you're using Oracle as a back-end :
    select to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual;
    if you're using SQL Server , then it's even shorter :
    select getdate()
    can't get shorter than that, can u ? ;-)
    (I wouldnt recommend firing an sql query just to get the timestamp, though)
Re: Shorter Formating?
by Steve_p (Priest) on Aug 14, 2002 at 11:16 UTC
    Try
    use POSIX; my $changedate = strftime "%Y-%m-%d %T", localtime();

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-12-27 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls