diff options
| author | Ron Yorston <rmy@pobox.com> | 2021-09-18 08:40:44 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-18 22:55:46 +0200 |
| commit | f27a6a94a7fb172a6768bc450dbdec68f15bc78f (patch) | |
| tree | 3c96ddf79ecf2658d43a839f193878e52d2ec846 /libbb | |
| parent | 6d2463ac01dd88cc4359ebbeae9cd757ce037c2b (diff) | |
| download | busybox-w32-f27a6a94a7fb172a6768bc450dbdec68f15bc78f.tar.gz busybox-w32-f27a6a94a7fb172a6768bc450dbdec68f15bc78f.tar.bz2 busybox-w32-f27a6a94a7fb172a6768bc450dbdec68f15bc78f.zip | |
libbb: code shrink parse_datestr (again)
Commit 9fe1548bb (date,touch: allow timezone offsets in dates)
mentioned the similarity between '@' format dates and those with
timezone offsets. It didn't notice that as a result there's
common code which can be shared.
function old new delta
parse_datestr 730 687 -43
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-43) Total: -43 bytes
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/time.c | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/libbb/time.c b/libbb/time.c index 41a69c754..f09ef5d52 100644 --- a/libbb/time.c +++ b/libbb/time.c | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | 13 | int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) |
| 14 | { | 14 | { |
| 15 | char end = '\0'; | 15 | char end = '\0'; |
| 16 | time_t t; | ||
| 16 | #if ENABLE_DESKTOP | 17 | #if ENABLE_DESKTOP |
| 17 | /* | 18 | /* |
| 18 | * strptime is BIG: ~1k in uclibc, ~10k in glibc | 19 | * strptime is BIG: ~1k in uclibc, ~10k in glibc |
| @@ -29,10 +30,10 @@ int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
| 29 | "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */ | 30 | "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */ |
| 30 | "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */ | 31 | "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */ |
| 31 | "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */ | 32 | "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */ |
| 32 | #if ENABLE_FEATURE_TIMEZONE | 33 | # if ENABLE_FEATURE_TIMEZONE |
| 33 | "%Y-%m-%d %R %z" "\0" /* yyyy-mm-dd HH:MM TZ */ | 34 | "%Y-%m-%d %R %z" "\0" /* yyyy-mm-dd HH:MM TZ */ |
| 34 | "%Y-%m-%d %T %z" "\0" /* yyyy-mm-dd HH:MM:SS TZ */ | 35 | "%Y-%m-%d %T %z" "\0" /* yyyy-mm-dd HH:MM:SS TZ */ |
| 35 | #endif | 36 | # endif |
| 36 | "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */ | 37 | "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */ |
| 37 | "%Y-%m-%d" "\0" /* yyyy-mm-dd */ | 38 | "%Y-%m-%d" "\0" /* yyyy-mm-dd */ |
| 38 | /* extra NUL */; | 39 | /* extra NUL */; |
| @@ -45,11 +46,8 @@ int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
| 45 | while (*fmt) { | 46 | while (*fmt) { |
| 46 | endp = strptime(date_str, fmt, ptm); | 47 | endp = strptime(date_str, fmt, ptm); |
| 47 | if (endp && *endp == '\0') { | 48 | if (endp && *endp == '\0') { |
| 48 | #if ENABLE_FEATURE_TIMEZONE | 49 | # if ENABLE_FEATURE_TIMEZONE |
| 49 | if (strchr(fmt, 'z')) { | 50 | if (strchr(fmt, 'z')) { |
| 50 | time_t t; | ||
| 51 | struct tm *utm; | ||
| 52 | |||
| 53 | /* we have timezone offset: obtain Unix time_t */ | 51 | /* we have timezone offset: obtain Unix time_t */ |
| 54 | ptm->tm_sec -= ptm->tm_gmtoff; | 52 | ptm->tm_sec -= ptm->tm_gmtoff; |
| 55 | ptm->tm_isdst = 0; | 53 | ptm->tm_isdst = 0; |
| @@ -57,13 +55,9 @@ int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
| 57 | if (t == (time_t)-1) | 55 | if (t == (time_t)-1) |
| 58 | break; | 56 | break; |
| 59 | /* convert Unix time_t to struct tm in user's locale */ | 57 | /* convert Unix time_t to struct tm in user's locale */ |
| 60 | utm = localtime(&t); | 58 | goto localise; |
| 61 | if (!utm) | ||
| 62 | break; | ||
| 63 | *ptm = *utm; | ||
| 64 | return 0; | ||
| 65 | } | 59 | } |
| 66 | #endif | 60 | # endif |
| 67 | return 1; | 61 | return 1; |
| 68 | } | 62 | } |
| 69 | *ptm = save; | 63 | *ptm = save; |
| @@ -141,13 +135,14 @@ int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
| 141 | } else | 135 | } else |
| 142 | #endif /* ENABLE_DESKTOP */ | 136 | #endif /* ENABLE_DESKTOP */ |
| 143 | if (date_str[0] == '@') { | 137 | if (date_str[0] == '@') { |
| 144 | time_t t; | ||
| 145 | if (sizeof(t) <= sizeof(long)) | 138 | if (sizeof(t) <= sizeof(long)) |
| 146 | t = bb_strtol(date_str + 1, NULL, 10); | 139 | t = bb_strtol(date_str + 1, NULL, 10); |
| 147 | else /* time_t is 64 bits but longs are smaller */ | 140 | else /* time_t is 64 bits but longs are smaller */ |
| 148 | t = bb_strtoll(date_str + 1, NULL, 10); | 141 | t = bb_strtoll(date_str + 1, NULL, 10); |
| 149 | if (!errno) { | 142 | if (!errno) { |
| 150 | struct tm *lt = localtime(&t); | 143 | struct tm *lt; |
| 144 | IF_FEATURE_TIMEZONE(localise:) | ||
| 145 | lt = localtime(&t); | ||
| 151 | if (lt) { | 146 | if (lt) { |
| 152 | *ptm = *lt; | 147 | *ptm = *lt; |
| 153 | return 0; | 148 | return 0; |
