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

RFC: adding POSIX::strptime()

Thread Next
Paul LeoNerd Evans
July 20, 2011 06:44
RFC: adding POSIX::strptime()
Message ID:
I've always considered it an omission, that POSIX.{pm,xs} don't wrap
strptime(3), the way they wrap strftime(3). Would anyone agree it should
be added?

I'm aware many variations on a theme exist, such as a CPAN module called
(confusingly), POSIX::strptime. However, they all make the mistake of
defining the API as

  ($sec, $min,...) = strptime($str, $format);

This is massively different from the C API, which (admittedly out of
necessity) takes a pointer to the  struct tm  structure as a
parameter. I propose any core Perl wrapping of strptime(3) ought to
offer similar, as it allows you to perform partial parsing; such as to
obtain the time 12:34:56 today:

  use POSIX 'strptime';

  my @time = localtime;
  strptime '12:34:56', '%H:%M:%S', \@time;

The strptime(3) API guarantees not to touch fields that weren't
specified in the format string; the Perl binding of it should do

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 C, or abuse
pos() marker on the inbound string..? Or something else entirely..?

Paul "LeoNerd" Evans
ICQ# 4135350       |  Registered Linux# 179460

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About