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

Dear Monks,

In the other day I was trying to modify a date string that was provided as input to a subroutine.
The input date is like "YYYY-MM-DD HH:MM:SS", and the output should be like "DD.MM.YYYY"

so i came up with this:
sub formatDate{ my $date = (split /\s+/, shift)[0]; $date =~ s/(\d{4})\-(\d{2})\-(\d{2})/$3\.$2\.$1/; return $date; }
I wonder if anyone knows a "one line" way to do this transformation.

Nelio

Replies are listed 'Best First'.
Re: Custom date string formatting
by davorg (Chancellor) on Jan 31, 2007 at 16:53 UTC
      you could be even more liberal by using [^\d]+:
      sub format_date { return join '.', (split /[^\d]+/, $_[0])[2,1,0]; }
Re: Custom date string formatting
by Limbic~Region (Chancellor) on Jan 31, 2007 at 17:19 UTC
    nelio,
    If you aren't familiar with POSIX 'strftime', you might want to look into it.

    Cheers - L~R

Re: Custom date string formatting
by johngg (Canon) on Jan 31, 2007 at 16:56 UTC
    You could do

    sub formatDate { return join q{.}, reverse split m{-}, (split m{\s+}, $_[0])[0]; }

    but, for maintainability, you are probably better off breaking it down over two or three lines.

    Cheers,

    JohnGG

Re: Custom date string formatting
by andyford (Curate) on Jan 31, 2007 at 17:16 UTC

    You were very close to an answer. You just needed to capture the time part in fourth set of parenthesis and then toss it by not referring to it.
    Assuming that there's always just one space between the date and the time:

    $date =~ s/(\d{4})\-(\d{2})\-(\d{2})(\s.+)/$3\.$2\.$1/;

    non-Perl: Andy Ford

      Yep, it really did'nt occur to me at the time. That's a clean regex solution.
      Thanks
Re: Custom date string formatting
by izut (Chaplain) on Jan 31, 2007 at 21:14 UTC

    Hi nelio.

    You really should follow Limbic~Region's tip, by using POSIX' strftime, which is bundled with Perl or DateTime.

    I would also to add another alternative (which you should not use in production environment):

    sub format_date { return join ".", (reverse unpack "A4xA2xA2", shift); }

    Update: I think it is ok use this if you know what are you doing and you will not perform any date calculation with it.

    Igor 'izut' Sutton
    your code, your rules.

Re: Custom date string formatting
by f00li5h (Chaplain) on Feb 01, 2007 at 06:45 UTC

    I'm sure that you don't want to be doing date manipulation as strings ... that'll only lead to pain and suffering... promose

    DateTime may help out some ...

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      Since I'm getting the input as a string and not as a date, and since I really don't want to do any datetime calculations (just present the date in a different way) I figured out that using POSIX would be like trying to kill a fly with a 9 inch cannon.
      Also the environment I'm working on is kinda restrictive, so any module that's not installed by default should be considered "exotic".

      Nelio

        Aah, yes, but if later you find yourself wanting to do more date manipulations, and finding that the fly spray you have won't work against the larger flying animals.

        Also, you're going to have to install your script on anyway, worst case, you have a use lib q[./lib]; and a lib directory with your script... best case, you just drop it in the same directory as your script (since . is generally in @INC anyway) Cpan as non root also offers helpful advice

        90% of your project is only done if you reuse code.

        ... having said that, i'm sure you have your reasons

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: Custom date string formatting
by nelio (Novice) on Feb 02, 2007 at 02:56 UTC
    Just out of curiosity, i've just been measuring some benchmarks by running the code 1000000 times and the results are as follows:

    the code by nelio took: 7 wallclock secs ( 6.73 usr + 0.02 sys = 6.75 CPU)
    the code by davorg took: 5 wallclock secs ( 4.90 usr + 0.01 sys = 4.91 CPU)
    the code by altblue took: 5 wallclock secs ( 5.05 usr + 0.01 sys = 5.06 CPU)
    the code by johngg took: 4 wallclock secs ( 2.99 usr + 0.01 sys = 3.00 CPU)
    the code by andyford took: 6 wallclock secs ( 5.79 usr + 0.01 sys = 5.80 CPU)
    the code by izut took: 3 wallclock secs ( 2.20 usr + 0.01 sys = 2.21 CPU)

    And the winner is... izut! :P