This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Questions about strptime()/strftime()


I looked further and I was getting the Seg Faults when printf() was trying to print the timezone from within the tm struct. It seems that strptime() doesn't do any intialization. After adding some memset(&tm, 0, sizeof(tm)) calls, the seg faults went away.

R.

Arkadiusz Miskiewicz wrote:
On Saturday 21 June 2008, Carlos O'Donell wrote:
On Sat, Jun 21, 2008 at 1:57 PM, Richard Harvey Chapman

<hchapman@3gfp.com> wrote:
result = strptime(str, format, &tt2_r);
This returns "Illegal seek" because "%F", "%z", and "%Z" are not valid
formats which strptime can convert.

Why it doesn't return NULL on a 2, 3 and 4th test then? Failure in first test is another magic.


$ ./a.out
strptime() == NULL
DEBUG: Parsed as 00-00-00
DEBUG: Parsed as 23-00-108
DEBUG: Parsed as 23-00-108
DEBUG: Parsed as 23-00-108

$ more test.c
#define _GNU_SOURCE 1
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <locale.h>

int main()
{
  char *date = "Mon Jan 23 14:01:57 +0000 2008";
  struct tm tmp;
  if (setlocale(LC_TIME, "pl_PL") == NULL)
          printf("setlocale() == NULL\n");
  memset (&tmp, 0, sizeof (tmp));
  if (strptime (date, "%a %b %d %T %z %Y", &tmp) == NULL)
          printf("strptime() == NULL\n");
  printf ("DEBUG: Parsed as %02d-%02d-%02d\n", tmp.tm_mday, tmp.tm_mon, tmp.tm_year);
  memset (&tmp, 0, sizeof (tmp));
  if (strptime (date, "Mon %b %d %T %z %Y", &tmp) == NULL)
          printf("strptime() == NULL\n");
  printf ("DEBUG: Parsed as %02d-%02d-%02d\n", tmp.tm_mday, tmp.tm_mon, tmp.tm_year);
  setlocale(LC_TIME, "C");
  memset (&tmp, 0, sizeof (tmp));
  if (strptime (date, "%a %b %d %T %z %Y", &tmp) == NULL)
          printf("strptime() == NULL\n");
  printf ("DEBUG: Parsed as %02d-%02d-%02d\n", tmp.tm_mday, tmp.tm_mon, tmp.tm_year);
  memset (&tmp, 0, sizeof (tmp));
  if (strptime (date, "Mon %b %d %T %z %Y", &tmp) == NULL)
          printf("strptime() == NULL\n");
  printf ("DEBUG: Parsed as %02d-%02d-%02d\n", tmp.tm_mday, tmp.tm_mon, tmp.tm_year);
}


1. Please see
http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html for
the full list.
2. Remember to check return values.

Cheers,
Carlos.





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]