Bug 15100 - strptime conversion error for %W
Summary: strptime conversion error for %W
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.15
: P2 normal
Target Milestone: 2.18
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-02-05 11:38 UTC by seanp_oriordain
Modified: 2014-06-13 18:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description seanp_oriordain 2013-02-05 11:38:26 UTC
%W returns incorrect answers

The code here:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
  int
main (void)
{
  struct tm tm;
  char buf[255];

  memset (&tm, 0, sizeof (struct tm));
  // ok what week number is the 1st of January 2012?
  // according to this it is week 01 in %U format 
  // and 00 in %W format...
  strptime ("2012-01-01 01:00", "%Y-%m-$d %H:%M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %U %W %a %b %H:%M", &tm);
  puts("To demonstrate the different week numbers in %U %W");
  puts("Using format %Y-%m-%d %U %W %a %b %H:%M");
  puts (buf);
  // to demonstrate it works
  strptime ("2012 02 Sun 01 00", "%Y %W %a %H %M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %a %b %H %M", &tm);
  puts("\nUsing format %Y-%m-%d %a %b %H%M for 2012 02 Sun 01 00");
  puts (buf);
  // but then the potential bug...
  strptime ("2012 01 Sun 01 00", "%Y %W %a %H %M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %a %b %H %M", &tm);
  puts("\nUsing format %Y-%m-%d %a %b %H%M for 2012 01 Sun 01 00");
  puts("and this is wrong...");
  puts (buf);
  strptime ("2012 00 Sun 01 00", "%Y %W %a %H %M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %a %b %H %M", &tm);
  puts("\nUsing format %Y-%m-%d %a %b %H %M for 2012 00 Sun 01 00");
  puts("and this is VERY wrong...");
  puts (buf);
  exit (EXIT_SUCCESS);
}


generates the following:

To demonstrate the different week numbers in %U %W
Using format %Y-%m-%d %U %W %a %b %H:%M
2012-01-00 01 00 Sun Jan 00:00

Using format %Y-%m-%d %a %b %H%M for 2012 02 Sun 01 00
2012-01-08 Sun Jan 01 00

Using format %Y-%m-%d %a %b %H%M for 2012 01 Sun 01 00
and this is wrong...
2012-01-01 Sun Jan 01 00

Using format %Y-%m-%d %a %b %H %M for 2012 00 Sun 01 00
and this is VERY wrong...
2012-00--371 Sun Saturday 01 00
Comment 1 Andreas Schwab 2013-06-05 08:34:40 UTC
Fixed by 840e294.