Technical article 110507

strptime does not handle the %p directive correctly for the 12:00 hour.

OpenServer 5.0.0OpenServer 5.0.2OpenServer 5.0.4OpenServer 5.0.5OpenServer 5.0.6UnixWare 7.0.0UnixWare 7.0.1UnixWare 7.1.0UnixWare 7.1.1

Written · Last revised

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)

From the Xinuos technical article database, record 110507. Reproduced with permission. The original is at www.sco.com/ta/110507, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.