Operating System - OpenVMS
1753481 Members
3757 Online
108794 Solutions
New Discussion юеВ

Re: Bug in strptime() function

 
WW304289
Frequent Advisor

Bug in strptime() function

I've run into what appears to be a bug in strptime() function on VMS. Attached reproducer.

Thanks,
-Boris

t.c
---
#include
#include

int main() {
struct tm tms;
if ( !strptime("20080125", "%Y%m%d", &tms) )
puts("OOPS!");
}

$ cc/ver
HP C V7.3-018 on OpenVMS IA64 V8.3
$ pipe cc t.c ; link t.obj ; run t.exe
OOPS!
$

$ uname -a
Linux ... x86_64 GNU/Linux
$ cc t.c && ./a.out
$
19 REPLIES 19
Clarete Riana
Valued Contributor

Re: Bug in strptime() function

Hi,

The input string and the format string both need spaces after year, month and date values. Find below the modified program that works.

#include
#include

int main() {
struct tm tms;
if ( !strptime("2008 01 25", "%Y %m %d", &tms) )
puts("OOPS!");
}

Joseph Huber_1
Honored Contributor

Re: Bug in strptime() function

Riana,
sorry, but this just confirms the CRTL implementation on VMS is flawed:
nothing in the description requires white-space between the conversion items.

On linux (and other systems ?) it simply works.
If the Xopen/Posix standards tell it different, please cite; otherwise CRTL should be corrected, such a form of date is really not unusual, e.g. in tables sorted by date/time.
http://www.mpp.mpg.de/~huber
Hoff
Honored Contributor

Re: Bug in strptime() function

The language lawyers may (will?) have a split decision here, but it's looking like this behavior diverges from that of gcc. (There are other areas of that divergence, though.)

Here's gcc 4.0.1.


$ cc x.c
f$ ./a.out
20080125
200801
200825
$ cat x.c
#include
#include
#include

int main() {
struct tm tms;
if ( !strptime("20080125", "%Y%m%d", &tms) )
puts("20080125");
if ( !strptime("200801", "%Y%m", &tms) )
puts("200801");
if ( !strptime("200825", "%Y%d", &tms) )
puts("200825");
if ( !strptime("2008", "%Y", &tms) )
puts("2008");
if ( !strptime("20080", "%Y", &tms) )
puts("20080");
if ( !strptime("0125", "%m%d", &tms) )
puts("0125");
exit( EXIT_SUCCESS );
}


Here is gcc 4.2.1...

$ cc x.c
$ ./a.out
$
Craig A Berry
Honored Contributor

Re: Bug in strptime() function

Riana is half right. The standard at:

http://www.opengroup.org/onlinepubs/7990989775/xsh/strptime.html

clearly states, regarding the format string, "There must be white-space or other non-alphanumeric characters between any two conversion specifications." Other C libraries clearly do not comply with this requirement.

However, there is no reason that the string being scanned should also need whitespace between fields. The CRTL does not comply with that aspect of the standard.

There is unfortunately no good alternative to writing code that works everywhere regardless of what the standard says, testing that it does so, and being prepared to rewrite the code when the standard and/or the implementations catch up with each other.
Joseph Huber_1
Honored Contributor

Re: Bug in strptime() function

Thanks for clarification Craig.
The CRTL HELP then correctly represents the standard.

IMHO the standard should more clearly tell that conversion items must be separated by either a white-space or other characters, one of the two must be present; so it is clear that %Y%m%d is not a legal format.
The present wording rather suggests delimiters between conversion items are optional.
http://www.mpp.mpg.de/~huber
Hoff
Honored Contributor

Re: Bug in strptime() function

Another case of being technically correct and operationally wrong? Ah, well.

That's never a fun tradeoff; you're wrong either way you go.

The ftp server has fallen into a similar hole. TCP/IP Services is technically correct, and operationally incompatible.

I'd hope for a gcc or clang compilation mode for newer OpenVMS compilers (to better work with gnv and such), but then I've encountered piles of (inherently buggy) VAX C code recently; old stuff.
Dennis Handly
Acclaimed Contributor

Re: Bug in strptime() function

>Craig: there is no reason that the string being scanned should also need whitespace between fields.

Huh? If it exists in the format, it must be there in the string.

I suppose one way to allow no delimiters is to fully specify (or assume) the widths.
Joseph Huber_1
Honored Contributor

Re: Bug in strptime() function

>>I suppose one way to allow no delimiters is to fully specify (or assume) the widths.

One might think so, but apparently neither VMS nor GNU has implemented it this way.

http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: Bug in strptime() function

i.e.

strptime("20080125", "%4Y%2m%2d", &tms)

gives error in both VMS and GNU systems.
http://www.mpp.mpg.de/~huber