date,touch: treat 2-digit years better (fit them into +-50 yrs around today)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/libbb/time.c b/libbb/time.c
index 2a74d34..1eb2d75 100644
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -93,6 +93,7 @@
*
* This coincides with the format of "touch -t TIME"
*/
+ unsigned cur_year = ptm->tm_year;
int len = strchrnul(date_str, '.') - date_str;
/* MM[.SS] */
@@ -133,6 +134,17 @@
&end) >= 5) {
/* Adjust month from 1-12 to 0-11 */
ptm->tm_mon -= 1;
+ if ((int)cur_year >= 50) { /* >= 1950 */
+ /* Adjust year: */
+ /* 1. Put it in the current century */
+ ptm->tm_year += (cur_year / 100) * 100;
+ /* 2. If too far in the past, +100 years */
+ if (ptm->tm_year < cur_year - 50)
+ ptm->tm_year += 100;
+ /* 3. If too far in the future, -100 years */
+ if (ptm->tm_year > cur_year + 50)
+ ptm->tm_year -= 100;
+ }
} else
/* ccyymmddHHMM[.SS] */
if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",