Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

localtime - make the date format different?

by ultranerds (Hermit)
on Dec 22, 2008 at 09:59 UTC ( [id://732017]=perlquestion: print w/replies, xml ) Need Help??

ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to make this code:

my @stats = stat("$folder/$file"); my $date = localtime $stats[9];


..so that instead of showing something like:

Tue Aug 16 19:23:30 2005

..I want more like:

2008-10-15 12:43:00

Is this even possible?

TIA!

Andy

Replies are listed 'Best First'.
Re: localtime - make the date format different?
by Corion (Patriarch) on Dec 22, 2008 at 10:14 UTC

    Use POSIX::strftime:

    use POSIX qw(strftime); print strftime "%Y-%m-%d %H:%M:%S", localtime;
Re: localtime - make the date format different?
by pKai (Priest) on Dec 22, 2008 at 10:14 UTC

    For this I regularly have a sub which formats the date and time like this:

    Z:\>perl -e "@t=localtime(); printf('%s-%02d-%02d %02d:%02d:%02d', $t[ +5]+1900, $t[4]+1, @t[3,2,1,0])" 2008-12-22 11:13:35
Re: localtime - make the date format different?
by poolpi (Hermit) on Dec 22, 2008 at 10:39 UTC
    #!/usr/bin/perl use strict; use warnings; use File::stat; use DateTime; my $file = '/home/you/test.txt'; my $st = stat($file) or die "No $file: $!"; my $dt = DateTime->from_epoch( epoch => $st->mtime ); print $dt->ymd, ' ', $dt->hms;


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      The OP wanted the local time, so you need to add time_zone => 'local' to the constructor.
Re: localtime - make the date format different?
by andreas1234567 (Vicar) on Dec 23, 2008 at 07:03 UTC
    Date::Format:
    $ perl -wle 'use Date::Format; my @lt = localtime((stat(q{/etc/passwd} +))[9]); print strftime("%c", @lt);' 11/08/08 11:27:57
    Consult the documentation to get the output exactly how you want it.
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: localtime - make the date format different?
by imrags (Monk) on Dec 22, 2008 at 10:05 UTC
    Use a split using \s as the separator or u can also use =~ s/\s/-/, $date
    Njoi
    Raghu
      haha always the simple things :D
      my @stats = stat("$folder/$file"); my @date = split / /, localtime $stats[9]; my $date = qq|$date[4] $date[1] $date[2] $date[3]|;


      Thanks for the tip =)

      Cheers

      Andy

        That won't work correctly at the beginning of the month.

        $ perl -le' my @date = split / /, localtime 1228636800; print qq|$date[4] $date[1] $date[2] $date[3]|; ' 00:00:00 Dec 7

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-25 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found