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

RFC: adding POSIX::strptime()

Thread Next
From:
Paul LeoNerd Evans
Date:
July 20, 2011 06:44
Subject:
RFC: adding POSIX::strptime()
Message ID:
20110720134417.GA18966@cel.leo
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
similar.

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..?

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Thread Next


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