diff options
author | Bartosz Golaszewski <bartekgola@gmail.com> | 2013-07-25 04:59:46 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-07-25 05:10:01 +0200 |
commit | 688a7e3f0454363e1dfed481e95afcdb818b1c91 (patch) | |
tree | 2dc27f105deca84073361e397ddcf171b2c64595 | |
parent | c19be75d57ff42dee54b53e21b3eb4723b8cf243 (diff) | |
download | busybox-w32-688a7e3f0454363e1dfed481e95afcdb818b1c91.tar.gz busybox-w32-688a7e3f0454363e1dfed481e95afcdb818b1c91.tar.bz2 busybox-w32-688a7e3f0454363e1dfed481e95afcdb818b1c91.zip |
date: accept 'yyyy-mm-dd HH' and 'yyyy-mm-dd' date formats
function old new delta
parse_datestr 794 885 +91
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/time.c | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/libbb/time.c b/libbb/time.c index 57e14b66c..ea2f72e3b 100644 --- a/libbb/time.c +++ b/libbb/time.c | |||
@@ -23,14 +23,16 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
23 | if (sscanf(date_str, "%u:%u%c", | 23 | if (sscanf(date_str, "%u:%u%c", |
24 | &ptm->tm_hour, | 24 | &ptm->tm_hour, |
25 | &ptm->tm_min, | 25 | &ptm->tm_min, |
26 | &end) >= 2) { | 26 | &end) >= 2 |
27 | ) { | ||
27 | /* no adjustments needed */ | 28 | /* no adjustments needed */ |
28 | } else | 29 | } else |
29 | /* mm.dd-HH:MM */ | 30 | /* mm.dd-HH:MM */ |
30 | if (sscanf(date_str, "%u.%u-%u:%u%c", | 31 | if (sscanf(date_str, "%u.%u-%u:%u%c", |
31 | &ptm->tm_mon, &ptm->tm_mday, | 32 | &ptm->tm_mon, &ptm->tm_mday, |
32 | &ptm->tm_hour, &ptm->tm_min, | 33 | &ptm->tm_hour, &ptm->tm_min, |
33 | &end) >= 4) { | 34 | &end) >= 4 |
35 | ) { | ||
34 | /* Adjust month from 1-12 to 0-11 */ | 36 | /* Adjust month from 1-12 to 0-11 */ |
35 | ptm->tm_mon -= 1; | 37 | ptm->tm_mon -= 1; |
36 | } else | 38 | } else |
@@ -38,15 +40,13 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
38 | if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year, | 40 | if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year, |
39 | &ptm->tm_mon, &ptm->tm_mday, | 41 | &ptm->tm_mon, &ptm->tm_mday, |
40 | &ptm->tm_hour, &ptm->tm_min, | 42 | &ptm->tm_hour, &ptm->tm_min, |
41 | &end) >= 5) { | 43 | &end) >= 5 |
42 | ptm->tm_year -= 1900; /* Adjust years */ | ||
43 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ | ||
44 | } else | ||
45 | /* yyyy-mm-dd HH:MM */ | 44 | /* yyyy-mm-dd HH:MM */ |
46 | if (sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year, | 45 | || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year, |
47 | &ptm->tm_mon, &ptm->tm_mday, | 46 | &ptm->tm_mon, &ptm->tm_mday, |
48 | &ptm->tm_hour, &ptm->tm_min, | 47 | &ptm->tm_hour, &ptm->tm_min, |
49 | &end) >= 5) { | 48 | &end) >= 5 |
49 | ) { | ||
50 | ptm->tm_year -= 1900; /* Adjust years */ | 50 | ptm->tm_year -= 1900; /* Adjust years */ |
51 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ | 51 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
52 | } else | 52 | } else |
@@ -58,7 +58,6 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
58 | return; /* don't fall through to end == ":" check */ | 58 | return; /* don't fall through to end == ":" check */ |
59 | } else | 59 | } else |
60 | #endif | 60 | #endif |
61 | //TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes) | ||
62 | { | 61 | { |
63 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 62 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
64 | } | 63 | } |
@@ -68,7 +67,21 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
68 | end = '\0'; | 67 | end = '\0'; |
69 | /* else end != NUL and we error out */ | 68 | /* else end != NUL and we error out */ |
70 | } | 69 | } |
71 | } else if (date_str[0] == '@') { | 70 | } else |
71 | /* yyyy-mm-dd HH */ | ||
72 | if (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year, | ||
73 | &ptm->tm_mon, &ptm->tm_mday, | ||
74 | &ptm->tm_hour, | ||
75 | &end) >= 4 | ||
76 | /* yyyy-mm-dd */ | ||
77 | || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year, | ||
78 | &ptm->tm_mon, &ptm->tm_mday, | ||
79 | &end) >= 3 | ||
80 | ) { | ||
81 | ptm->tm_year -= 1900; /* Adjust years */ | ||
82 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ | ||
83 | } else | ||
84 | if (date_str[0] == '@') { | ||
72 | time_t t = bb_strtol(date_str + 1, NULL, 10); | 85 | time_t t = bb_strtol(date_str + 1, NULL, 10); |
73 | if (!errno) { | 86 | if (!errno) { |
74 | struct tm *lt = localtime(&t); | 87 | struct tm *lt = localtime(&t); |