Front page | perl.perl5.porters |
Postings from August 2011
Re: RFC: adding POSIX::strptime()
Thread Previous
|
Thread Next
From:
Paul LeoNerd Evans
Date:
August 4, 2011 03:27
Subject:
Re: RFC: adding POSIX::strptime()
Message ID:
20110804102656.GH18032@cel.leo
On Wed, Aug 03, 2011 at 01:17:19PM +0200, Aristotle Pagaltzis wrote:
> You can leave those fields with a default value of undef when
> returning a list. And you can take a list as input to define
> defaults other than undef, as Eric showed.
>
> Then you don’t have to have a bunch of statements as in
>
> my @time = localtime;
> strptime( $str, '%H:%M:%S', \@time );
> mktime( @time );
>
> you can just write
>
> mktime strptime $str, '%H:%M:%S', localtime;
>
> I know which one is messier to me.
Yes; on consideration, your and Eric's list-mutating function idea seems
neater than my array-mutating procedure.
> > A related question is how to handle the case of partial
> > parsing? Again, most of the existing CPAN wrappers want to
> > consume all of the string input, and either complain if given
> > trailing content (Time::Piece::strptime), or just plain ignore
> > it (POSIX::strptime::strptime). I propose that, even if not the
> > default, an option should be to yield the length of
> > successfully-parsed input, to allow for partial parsers. I'm
> > not quite sure what the API should look like in this case
> > though - return the parsed length a.la. C, or abuse pos()
> > marker on the inbound string..? Or something else entirely..?
>
> It seems to me the option should be the default. And I think it
> should work by setting `pos()` on the passed string.
>
> So after
>
> mktime strptime $str, '%H:%M:%S', localtime;
>
> `pos $str` would tell you were any partial parse left off if you
> really need the offset, but you can also implicitly pick up the
> parse right there yourself using `scalar //g`.
>
> That gives you a nice, neat, Perlish way to do everything you can
> do with the C API, without having to write C in Perl.
Two questions remains though:
1. Does the caller have to ask to use the pos() marker to start from?
It could be confusing if a pos() marker is accidentally left on the
string and the function starts from there, instead of at 0.
2. Does the caller have to say "don't worry about trailing content, I
will handle it"? If the caller had no intention to handle this,
would the following be an error?
my $str = "12:34:56 DIE DIE DIE";
my @time = strptime $str, '%H:%M:%S', localtime;
> Heck, the Time::Piece `strptime` can be extended to cover all of
> the use cases you showed. Beyond setting `pos()` it would just
> need to allow calling `strptime` as an instance method, and use
> the values from the constructing object for the default values
> of unspecified fields.
>
> I.e. where you propose
>
> my @time = localtime;
> strptime( $str, '%H:%M:%S', \@time );
>
> it would instead be
>
> Time::Piece->now->strptime( $str, '%H:%M:%S' );
>
> Now isn’t *that* pretty.
LGTM
> The one option this doesn’t give you, compared to the above API,
> is handling elliptical datetimes.
How do you mean elliptical? If by that you mean the two split partial
cases, that's still not hard:
Time::Piece->now
->strptime( "2011/Aug/4", '%Y/%b/%d' )
->strptime( "12:34:56", '%H:%M:%S' )
--
Paul "LeoNerd" Evans
leonerd@leonerd.org.uk
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
Thread Previous
|
Thread Next