FreeBSD Bugzilla – Attachment 145482 Details for
Bug 137307
[libc] [patch] Enhance strptime(3) to support %U and %W
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Updated patch
libc-strptime.diff (text/plain), 4.47 KB, created by
Pedro F. Giffuni
on 2014-08-07 19:56:00 UTC
(
hide
)
Description:
Updated patch
Filename:
MIME Type:
Creator:
Pedro F. Giffuni
Created:
2014-08-07 19:56:00 UTC
Size:
4.47 KB
patch
obsolete
>Index: lib/libc/stdtime/strptime.3 >=================================================================== >--- lib/libc/stdtime/strptime.3 (revision 269651) >+++ lib/libc/stdtime/strptime.3 (working copy) >@@ -164,9 +164,7 @@ > .Fa \&%U > and > .Fa %W >-format specifiers accept any value within the range 00 to 53 >-without validating against other values supplied (like month >-or day of the year, for example). >+format specifiers accept a two-digit decimal within the range 00 to 53. > .Pp > The > .Fa %Z >Index: lib/libc/stdtime/strptime.c >=================================================================== >--- lib/libc/stdtime/strptime.c (revision 269651) >+++ lib/libc/stdtime/strptime.c (working copy) >@@ -56,10 +56,27 @@ > #include "libc_private.h" > #include "timelocal.h" > >+#include "tzfile.h" >+ > static char * _strptime(const char *, const char *, struct tm *, int *, locale_t); > >-#define asizeof(a) (sizeof (a) / sizeof ((a)[0])) >+#define asizeof(a) (sizeof(a) / sizeof((a)[0])) > >+/* >+ * Calculate the week day of the first day of a year. Valid for >+ * the Gregorian calendar, which began Sept 14, 1752 in the UK >+ * and its colonies. Ref: >+ * http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week/ >+ */ >+ >+static int >+first_wday_of(int year) >+{ >+ return (((2 * (3 - (year / 100) % 4)) + (year % 100) + >+ ((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7); >+} >+ >+ > static char * > _strptime(const char *buf, const char *fmt, struct tm *tm, int *GMTp, > locale_t locale) >@@ -66,9 +83,13 @@ > { > char c; > const char *ptr; >+ int day_offset = -1; > int i, len; > int Ealternative, Oalternative; >- struct lc_time_T *tptr = __get_current_time_locale(locale); >+ const struct lc_time_T *tptr = __get_current_time_locale(locale); >+ static int start_of_month[2][13] = { >+ {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, >+ {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}}; > > ptr = fmt; > while (*ptr != 0) { >@@ -119,7 +140,7 @@ > if (i < 19) > return (NULL); > >- tm->tm_year = i * 100 - 1900; >+ tm->tm_year = i * 100 - TM_YEAR_BASE; > break; > > case 'c': >@@ -303,6 +324,27 @@ > return (NULL); > > tm->tm_wday = i; >+ if (day_offset >= 0 && (i - day_offset) != 0) { >+ tm->tm_yday += i - day_offset; >+ i = 0; >+ while (tm->tm_yday >= >+ start_of_month[isleap(tm->tm_year + >+ TM_YEAR_BASE)][i]) >+ i++; >+ if (i > 12) >+ { >+ i = 1; >+ tm->tm_yday -= >+ start_of_month[ >+ isleap(tm->tm_year + TM_YEAR_BASE)] >+ [12]; >+ tm->tm_year++; >+ } >+ tm->tm_mon = i - 1; >+ tm->tm_mday = tm->tm_yday - >+ start_of_month[isleap(tm->tm_year + >+ TM_YEAR_BASE)][i - 1] + 1; >+ } > buf += len; > break; > >@@ -309,10 +351,8 @@ > case 'U': > case 'W': > /* >- * XXX This is bogus, as we can not assume any valid >- * information present in the tm structure at this >- * point to calculate a real value, so just check the >- * range for now. >+ * We expect that the year has already been >+ * parsed. > */ > if (!isdigit_l((unsigned char)*buf, locale)) > return (NULL); >@@ -327,6 +367,43 @@ > if (i > 53) > return (NULL); > >+ /* Week numbers are 1-origin. So that we can always >+ * return the date of a Sunday (or Monday), treat week >+ * 0 as week 1. >+ */ >+ >+ if (i == 0) >+ i = 1; >+ >+ if (c == 'U') >+ day_offset = TM_SUNDAY; >+ else >+ day_offset = TM_MONDAY; >+ >+ /* >+ * Set the date to the first Sunday (or Monday) >+ * of the specified week of the year. >+ */ >+ >+ tm->tm_yday = (7 - first_wday_of(tm->tm_year + >+ TM_YEAR_BASE) + day_offset) % 7 + (i - 1) * 7; >+ i = 0; >+ while (tm->tm_yday >= >+ start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i]) >+ i++; >+ if (i > 12) >+ { >+ i = 1; >+ tm->tm_yday -= >+ start_of_month[ >+ isleap(tm->tm_year + TM_YEAR_BASE)][12]; >+ tm->tm_year++; >+ } >+ tm->tm_mon = i - 1; >+ tm->tm_mday = tm->tm_yday - >+ start_of_month[isleap(tm->tm_year + TM_YEAR_BASE)][i - 1] + 1; >+ tm->tm_wday = day_offset; >+ > break; > > case 'w': >@@ -471,7 +548,7 @@ > len--; > } > if (c == 'Y') >- i -= 1900; >+ i -= TM_YEAR_BASE; > if (c == 'y' && i < 69) > i += 100; > if (i < 0) >@@ -546,7 +623,6 @@ > return ((char *)buf); > } > >- > char * > strptime_l(const char * __restrict buf, const char * __restrict fmt, > struct tm * __restrict tm, locale_t loc) >@@ -564,6 +640,7 @@ > > return (ret); > } >+ > char * > strptime(const char * __restrict buf, const char * __restrict fmt, > struct tm * __restrict tm)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 137307
:
98401
|
98402
|
144894
|
145108
|
145481
|
145482
|
147573
|
147680
|
147740