NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/50009: strptime small enhancement



The following reply was made to PR lib/50009; it has been noted by GNATS.

From: David CARLIER <devnexen%gmail.com@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: 
Subject: Re: lib/50009: strptime small enhancement
Date: Mon, 29 Jun 2015 22:41:14 +0100

 --Apple-Mail=_65474D83-214C-489E-822A-D2420A0A27D0
 Content-Transfer-Encoding: quoted-printable
 Content-Type: text/plain;
 	charset=utf-8
 
 
 Here another version based on FreeBSD=E2=80=99s implementation pretty =
 much, works on more use cases than the first version and =E2=80=98U=E2=80=99=
  and =E2=80=98W=E2=80=99 flags work better.
 
 Index: time/strptime.c
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
 RCS file: /cvsroot/src/lib/libc/time/strptime.c,v
 retrieving revision 1.39
 diff -u -r1.39 strptime.c
 --- time/strptime.c	6 Apr 2015 14:38:22 -0000	1.39
 +++ time/strptime.c	29 Jun 2015 21:33:46 -0000
 @@ -36,6 +36,8 @@
 
  #include "namespace.h"
  #include <sys/localedef.h>
 +#include <sys/types.h>
 +#include <sys/clock.h>
  #include <ctype.h>
  #include <locale.h>
  #include <string.h>
 @@ -60,6 +62,12 @@
  #define ALT_O			0x02
  #define	LEGAL_ALT(x)		{ if (alt_format & ~(x)) return =
 NULL; }
 
 +#define	FLAG_YEAR	(1 << 0)
 +#define	FLAG_MTH	(1 << 1)
 +#define	FLAG_YDAY	(1 << 2)
 +#define	FLAG_MDAY	(1 << 3)
 +#define	FLAG_WDAY	(1 << 4)
 +
  static char gmt[] =3D { "GMT" };
  static char utc[] =3D { "UTC" };
  /* RFC-822/RFC-2822 */
 @@ -74,6 +82,18 @@
  static const u_char *find_string(const u_char *, int *, const char * =
 const *,
  	const char * const *, int);
 
 +static const int mths_per_yr_kind[2][13] =3D {
 +	{ 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 }
 +};
 +
 +static int
 +first_wday_of(int yr)
 +{
 +	return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / =
 4) +
 +		(is_leap_year(yr) ? 6 : 0) + 1) % 7;
 +}
 +
  char *
  strptime(const char *buf, const char *fmt, struct tm *tm)
  {
 @@ -85,7 +105,7 @@
  {
  	unsigned char c;
  	const unsigned char *bp, *ep;
 -	int alt_format, i, split_year =3D 0, neg =3D 0, offs;
 +	int alt_format, i, split_year =3D 0, neg =3D 0, flags =3D 0, =
 doff =3D -1, woff =3D 0, offs;
  	const char *new_fmt;
 
  	bp =3D (const u_char *)buf;
 @@ -133,16 +153,19 @@
  		 */
  		case 'c':	/* Date and time, using the locale's =
 format. */
  			new_fmt =3D _TIME_LOCALE(loc)->d_t_fmt;
 +			flags |=3D FLAG_WDAY | FLAG_MTH | FLAG_MDAY | =
 FLAG_YEAR;
  			goto recurse;
 
  		case 'D':	/* The date as "%m/%d/%y". */
  			new_fmt =3D "%m/%d/%y";
  			LEGAL_ALT(0);
 +			flags |=3D FLAG_MTH | FLAG_MDAY | FLAG_YEAR;
  			goto recurse;
 
  		case 'F':	/* The date as "%Y-%m-%d". */
  			new_fmt =3D "%Y-%m-%d";
  			LEGAL_ALT(0);
 +			flags |=3D FLAG_MTH | FLAG_MDAY | FLAG_YEAR;
  			goto recurse;
 
  		case 'R':	/* The time as "%H:%M". */
 @@ -166,6 +189,7 @@
 
  		case 'x':	/* The date, using the locale's format. =
 */
  			new_fmt =3D _TIME_LOCALE(loc)->d_fmt;
 +			flags |=3D FLAG_MTH | FLAG_MDAY | FLAG_YEAR;
  		    recurse:
  			bp =3D (const u_char *)strptime((const char =
 *)bp,
  							    new_fmt, =
 tm);
 @@ -180,6 +204,7 @@
  			bp =3D find_string(bp, &tm->tm_wday,
  			    _TIME_LOCALE(loc)->day, =
 _TIME_LOCALE(loc)->abday, 7);
  			LEGAL_ALT(0);
 +			flags |=3D FLAG_WDAY;
  			continue;
 
  		case 'B':	/* The month, using the locale's form. =
 */
 @@ -189,6 +214,7 @@
  			    _TIME_LOCALE(loc)->mon, =
 _TIME_LOCALE(loc)->abmon,
  			    12);
  			LEGAL_ALT(0);
 +			flags |=3D FLAG_MTH;
  			continue;
 
  		case 'C':	/* The century number. */
 @@ -201,12 +227,14 @@
  			split_year =3D 1;
  			tm->tm_year =3D i;
  			LEGAL_ALT(ALT_E);
 +			flags |=3D FLAG_YEAR;
  			continue;
 
  		case 'd':	/* The day of month. */
  		case 'e':
  			bp =3D conv_num(bp, &tm->tm_mday, 1, 31);
  			LEGAL_ALT(ALT_O);
 +			flags |=3D FLAG_MDAY;
  			continue;
 
  		case 'k':	/* The hour (24-hour clock =
 representation). */
 @@ -232,6 +260,7 @@
  			bp =3D conv_num(bp, &i, 1, 366);
  			tm->tm_yday =3D i - 1;
  			LEGAL_ALT(0);
 +			flags |=3D FLAG_YDAY;
  			continue;
 
  		case 'M':	/* The minute. */
 @@ -244,6 +273,7 @@
  			bp =3D conv_num(bp, &i, 1, 12);
  			tm->tm_mon =3D i - 1;
  			LEGAL_ALT(ALT_O);
 +			flags |=3D FLAG_MTH;
  			continue;
 
  		case 'p':	/* The locale's equivalent of AM/PM. */
 @@ -287,6 +317,8 @@
 
  				if (localtime_r(&sse, tm) =3D=3D NULL)
  					bp =3D NULL;
 +				else
 +					flags |=3D FLAG_YDAY | FLAG_WDAY =
 | FLAG_MTH | FLAG_MDAY | FLAG_YEAR;
  			}
  			continue;
 
 @@ -300,11 +332,17 @@
  			 */
  			 bp =3D conv_num(bp, &i, 0, 53);
  			 LEGAL_ALT(ALT_O);
 +			 if (c =3D=3D 'U')
 +				doff =3D TM_SUNDAY;
 +			 else
 +				doff =3D TM_MONDAY;
 +			 woff =3D i;
  			 continue;
 
  		case 'w':	/* The day of week, beginning on sunday. =
 */
  			bp =3D conv_num(bp, &tm->tm_wday, 0, 6);
  			LEGAL_ALT(ALT_O);
 +			flags |=3D FLAG_WDAY;
  			continue;
 
  		case 'u':	/* The day of week, monday =3D 1. */
 @@ -336,6 +374,7 @@
  			bp =3D conv_num(bp, &i, 0, 9999);
  			tm->tm_year =3D i - TM_YEAR_BASE;
  			LEGAL_ALT(ALT_E);
 +			flags |=3D FLAG_YEAR;
  			continue;
 
  		case 'y':	/* The year within 100 years of the =
 epoch. */
 @@ -353,6 +392,7 @@
  					i =3D i + 1900 - TM_YEAR_BASE;
  			}
  			tm->tm_year =3D i;
 +			flags |=3D FLAG_YEAR;
  			continue;
 
  		case 'Z':
 @@ -425,6 +465,7 @@
  				continue;
  			case '+':
  				neg =3D 0;
 +				flags |=3D FLAG_WDAY | FLAG_MTH | =
 FLAG_MDAY | FLAG_YEAR;
  				break;
  			case '-':
  				neg =3D 1;
 @@ -529,6 +570,52 @@
  		}
  	}
 
 +	if (!(flags & FLAG_YDAY) && (flags & FLAG_YEAR)) {
 +		int isleap =3D is_leap_year(tm->tm_year + TM_YEAR_BASE);
 +		if ((flags & (FLAG_MTH | FLAG_MDAY)) =3D=3D (FLAG_MTH | =
 FLAG_MDAY)) {
 +			tm->tm_yday =3D =
 mths_per_yr_kind[isleap][tm->tm_mon] + (tm->tm_mday - 1);
 +			flags |=3D FLAG_YDAY;
 +		} else if (doff !=3D -1) {
 +			if (!(flags & FLAG_WDAY)) {
 +				tm->tm_wday =3D doff;
 +				flags |=3D FLAG_WDAY;
 +			}
 +			tm->tm_yday =3D (7 - first_wday_of(tm->tm_year + =
 TM_YEAR_BASE) +
 +				doff) % 7 + (woff - 1) * 7 + tm->tm_wday =
 - doff;
 +			flags |=3D FLAG_YDAY;
 +		}
 +	}
 +
 +	if ((flags & (FLAG_YEAR | FLAG_YDAY)) =3D=3D (FLAG_YEAR | =
 FLAG_YDAY)) {
 +		int isleap =3D is_leap_year(tm->tm_year + TM_YEAR_BASE);
 +		if (!(flags & FLAG_MTH)) {
 +			i =3D 0;
 +			while (tm->tm_yday >=3D =
 mths_per_yr_kind[isleap][i])
 +				i ++;
 +			if (i > 12) {
 +				i =3D 1;
 +				tm->tm_yday -=3D =
 mths_per_yr_kind[isleap][12];
 +				tm->tm_year ++;
 +			}
 +			tm->tm_mon =3D i - 1;
 +			flags |=3D FLAG_MTH;
 +		}
 +		if (!(flags & FLAG_MDAY)) {
 +			tm->tm_mday =3D tm->tm_yday - =
 mths_per_yr_kind[isleap][tm->tm_mon] + 1;
 +			flags |=3D FLAG_MDAY;
 +		}
 +		if (!(flags & FLAG_WDAY)) {
 +			i =3D 0;
 +			woff =3D first_wday_of(tm->tm_year);
 +			while (i ++ <=3D tm->tm_yday) {
 +				if (woff ++ >=3D 6)
 +					woff =3D 0;
 +			}
 +			tm->tm_wday =3D woff;
 +			flags |=3D FLAG_WDAY;
 +		}
 +	}
 +
  	return __UNCONST(bp);
  }
 
 
 
 > On 29 Jun 2015, at 19:55, Christos Zoulas <christos%zoulas.com@localhost> wrote:
 >=20
 > The following reply was made to PR lib/50009; it has been noted by =
 GNATS.
 >=20
 > From: christos%zoulas.com@localhost (Christos Zoulas)
 > To: gnats-bugs%NetBSD.org@localhost, lib-bug-people%netbsd.org@localhost,
 > 	gnats-admin%netbsd.org@localhost, netbsd-bugs%netbsd.org@localhost, =
 devnexen%gmail.com@localhost
 > Cc:
 > Subject: Re: lib/50009: strptime small enhancement
 > Date: Mon, 29 Jun 2015 14:53:59 -0400
 >=20
 > On Jun 29,  6:50pm, devnexen%gmail.com@localhost (David CARLIER) wrote:
 > -- Subject: Re: lib/50009: strptime small enhancement
 >=20
 > |  Well, it's closer to OpenBSD version but I m not against following
 > |  FreeBSD's path personally. If it s better I can provide a newer =
 version?
 >=20
 > Sure, why don't you look at them both and make a recommendation?
 >=20
 > Thanks,
 >=20
 > christos
 >=20
 
 
 --Apple-Mail=_65474D83-214C-489E-822A-D2420A0A27D0
 Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment;
 	filename=signature.asc
 Content-Type: application/pgp-signature;
 	name=signature.asc
 Content-Description: Message signed with OpenPGP using GPGMail
 
 -----BEGIN PGP SIGNATURE-----
 Comment: GPGTools - https://gpgtools.org
 
 iQEcBAEBCgAGBQJVkbt/AAoJECNxxoUnxrf44tUH/1jH39r64I3xXXlZhLA9HHLx
 LIN+YXIxB3lVHNYjnM63W6YnEzUrlG4o3OTbtHn6PDk1D7zyg/SI0cVo95Gk9C+r
 ryKAfnyhmnCi34jdU4FQ79u4yqDxJawGZBeiiIYANg1od+cNfC2t8j4U4qDm53nO
 BoVhjTE1c978h1TShdaB66+LDJiLwIpL8X7JUU4UmskgJ65Knysa/91ZD36iUUxM
 O7iZAdOiQMENVKMC/LPL8FF6I/BaRYpcfNhuqGh6kmYuF3AA1wZnwWjQNpv70f5U
 Zmz34/4pjFuHDgBC3Gx6slukYl9IWY+yFXEIOdfDiK53Ejrtw9SzhoIrIxdkVCs=
 =jl1Z
 -----END PGP SIGNATURE-----
 
 --Apple-Mail=_65474D83-214C-489E-822A-D2420A0A27D0--
 



Home | Main Index | Thread Index | Old Index