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

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.

Replies are listed 'Best First'.
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,

(jeffa) Re: Shorter Formating?
by jeffa (Bishop) 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? ...


    (the triplet paradiddle with high-hat)
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];
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
    use POSIX; my $changedate = strftime "%Y-%m-%d %T", localtime();

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://189968]
Approved by Albannach
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2018-06-25 16:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.