date,touch: treat 2-digit years better (fit them into +-50 yrs around today)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
a5d3d3436b
commit
92ffe0571a
@ -120,9 +120,10 @@ int touch_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
struct tm tm_time;
|
struct tm tm_time;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
//time(&t);
|
//memset(&tm_time, 0, sizeof(tm_time));
|
||||||
//localtime_r(&t, &tm_time);
|
/* Better than memset: makes "HH:MM" dates meaningful */
|
||||||
memset(&tm_time, 0, sizeof(tm_time));
|
time(&t);
|
||||||
|
localtime_r(&t, &tm_time);
|
||||||
parse_datestr(date_str, &tm_time);
|
parse_datestr(date_str, &tm_time);
|
||||||
|
|
||||||
/* Correct any day of week and day of year etc. fields */
|
/* Correct any day of week and day of year etc. fields */
|
||||||
|
12
libbb/time.c
12
libbb/time.c
@ -93,6 +93,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
|
|||||||
*
|
*
|
||||||
* This coincides with the format of "touch -t TIME"
|
* This coincides with the format of "touch -t TIME"
|
||||||
*/
|
*/
|
||||||
|
unsigned cur_year = ptm->tm_year;
|
||||||
int len = strchrnul(date_str, '.') - date_str;
|
int len = strchrnul(date_str, '.') - date_str;
|
||||||
|
|
||||||
/* MM[.SS] */
|
/* MM[.SS] */
|
||||||
@ -133,6 +134,17 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
|
|||||||
&end) >= 5) {
|
&end) >= 5) {
|
||||||
/* Adjust month from 1-12 to 0-11 */
|
/* Adjust month from 1-12 to 0-11 */
|
||||||
ptm->tm_mon -= 1;
|
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
|
} else
|
||||||
/* ccyymmddHHMM[.SS] */
|
/* ccyymmddHHMM[.SS] */
|
||||||
if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
|
if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
|
||||||
|
Loading…
Reference in New Issue
Block a user