Problem
strptime incorrectly handles the %p directive for the 12:00 hour. strptime updates the tm_hour field with 0 when 12:00 PM is passed in as an argument and 12 when 12:00 AM is passed in as an argument. This is the case for all 12:XX values and is the opposite of what it should be according to convention.
The code stub below demonstrates the problem:
#include <sys/types.h>
#include <time.h>
#include <stdio.h>main()
{
struct tm when;
char s[30];memset(&when,0,sizeof(when)); strptime("08/25/99 12:00 am","%D %I:%M %p",&when); printf("\n strptime input 08/25/99 12:00 am:\n"); printf("tm_hour= %d \ntm_min= %d \n", when.tm_hour, when.tm_min); strftime(s,sizeof(s),"%a %D %I:%M %p",&when); printf("Output should be 12:00 am today, output is: %s.\n",s);
memset(&when,0,sizeof(when)); strptime("08/25/99 12:00 pm","%D %I:%M %p",&when); printf("\n strptime input 08/25/99 12:00 pm:\n"); printf("tm_hour= %d \ntm_min= %d \n", when.tm_hour, when.tm_min); strftime(s,sizeof(s),"%a %D %I:%M %p",&when); printf("Output should be 12:00 pm today, output is: %s.\n",s); }
This produces the following output:strptime input 08/25/99 12:00 am: tm_hour= 12 tm_min= 0 Output should be 12:00 am today, output is: Wed 08/25/99 12:00 PM.
strptime input 08/25/99 12:00 pm: tm_hour= 0 tm_min= 0 Output should be 12:00 pm today, output is: Wed 08/25/99 12:00 AM.
Solution
To work around this problem, do not use the %p directive and use standard military time as input to strptime.
NOTE: This problem occurs on all development system releases supplied by
SCO. See also
Manual pages: strptime(S), strptime(3C)