develooper Front page | perl.perl5.porters | Postings from July 2011

Re: RFC: adding POSIX::strptime()

Thread Previous
From:
Eric Brine
Date:
July 22, 2011 12:17
Subject:
Re: RFC: adding POSIX::strptime()
Message ID:
CALJW-qGp_zDuofnhjPQ97-VKHehzM15WcpGnq2Wt9Vydtz=Rxg@mail.gmail.com
On Fri, Jul 22, 2011 at 12:06 PM, Paul LeoNerd Evans <leonerd@leonerd.org.uk
> wrote:

>
> I wanted to preserve the original values at positions 3,4,5 from my
> localtime call, and have strptime mutate only the values at 0,1,2; the
> intention being to have strptime alter the time-of-the-day but leave the
> date alone.
>

I know!!! The problem is that you keep saying that

my @time = strptime( $str, $fmt, localtime )

wouldn't do that.

> The same choice applies if you pass a list. Why do you think it can't?
> ...
> > sub sometime
> >  {
> >     my ( $str, $fmt ) = @_;
> >     return mktime( strptime( $str, $fmt, localtime ) );
> >  }
>
> OK, so it could take and return a 9-element list with some of the
> elements different. I prefer the neater-in-my-opinion API of mutating an
> array, a reference to which is passed. Swings, roundabouts,...
>

strptime( $str, $fmt, \( my @time = localtime ));

  -vs-

my @time = strptime( $str, $fmt, localtime );



> > Then presumably I've already answered it?
>
> Not at all.
>
> Your strptime is returning a 9-element list, correct?
> That makes @time an 18 element list, half of which is complete junk.
>


Sorry, misapplication of parens. Should have been

my @time;
strptime $timestr, "%H:%M:%S", \@time;
strptime $datestr, "%Y/%m/%d", \@time;

   -vs--

my @time =
   strptime $datestr, "%Y/%m/%d",
      strptime $timestr, "%H:%M:%S";

Yours further has the subtle question of; if it's going to take a
>
9-element list, and return a 9-element list with some different values,
> what happens if the format doesn't define all 6 base fields, and no
> original list was passed in? I.e. what does the innermost strptime() in
> the last example here, actually pass into the outermost one?
>

The implication that yours doesn't is false. I made my code equivalent to
yours. You know how to provide defaults:

my @time = localtime;
strptime $timestr, "%H:%M:%S", \@time;
strptime $datestr, "%Y/%m/%d", \@time;

   -vs--

my @time =
   strptime $datestr, "%Y/%m/%d",
      strptime $timestr, "%H:%M:%S",
         localtime;

- Eric


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About