diff options
author | Elliott Hughes <enh@google.com> | 2014-04-07 15:11:57 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2014-04-07 15:24:47 -0700 |
commit | e25a6413d381fc1f35def9ddfa2fbdcf199c09ff (patch) | |
tree | 2b5664d6df5fb12337032278a440f17c3256afa6 /toolbox/date.c | |
parent | 5f130216762278331b5d97eaf1fb549680143c85 (diff) | |
download | system_core-e25a6413d381fc1f35def9ddfa2fbdcf199c09ff.zip system_core-e25a6413d381fc1f35def9ddfa2fbdcf199c09ff.tar.gz system_core-e25a6413d381fc1f35def9ddfa2fbdcf199c09ff.tar.bz2 |
Move date off the non-portable bionic32 'strtotimeval'.
Change-Id: Ib706d3a536c409810cfbc47270448c6375314506
Diffstat (limited to 'toolbox/date.c')
-rw-r--r-- | toolbox/date.c | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/toolbox/date.c b/toolbox/date.c index d6c9052..aa3b72e 100644 --- a/toolbox/date.c +++ b/toolbox/date.c @@ -1,10 +1,13 @@ +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> #include <string.h> -#include <errno.h> #include <time.h> +#include <unistd.h> + #include <linux/android_alarm.h> #include <linux/rtc.h> #include <sys/ioctl.h> @@ -108,6 +111,33 @@ static void settime(char *s) { settime_rtc_tm(&tm); } +static char *parse_time(const char *str, struct timeval *ts) { + char *s; + long fs = 0; /* fractional seconds */ + + ts->tv_sec = strtoumax(str, &s, 10); + + if (*s == '.') { + s++; + int count = 0; + + /* read up to 6 digits (microseconds) */ + while (*s && isdigit(*s)) { + if (++count < 7) { + fs = fs*10 + (*s - '0'); + } + s++; + } + + for (; count < 6; count++) { + fs *= 10; + } + } + + ts->tv_usec = fs; + return s; +} + int date_main(int argc, char *argv[]) { int c; @@ -181,7 +211,7 @@ int date_main(int argc, char *argv[]) //strptime(argv[optind], NULL, &tm); //tv.tv_sec = mktime(&tm); //tv.tv_usec = 0; - strtotimeval(argv[optind], &tv); + parse_time(argv[optind], &tv); printf("time %s -> %lu.%lu\n", argv[optind], tv.tv_sec, tv.tv_usec); res = settime_alarm_timeval(&tv); if (res < 0) |