--- src/shared/time-util.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/time-util.h | 1 + src/test/test-time.c | 20 ++++++++++++++++ 3 files changed, 84 insertions(+)
diff --git a/src/shared/time-util.c b/src/shared/time-util.c index d3404af..a45341d 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -861,6 +861,69 @@ int parse_nsec(const char *t, nsec_t *nsec) { return 0; } +int sec_to_stringa(const char *t, usec_t usec, const char **time) { + static const struct { + const char *suffix; + usec_t usec; + } table[] = { + { "seconds", USEC_PER_SEC }, + { "second", USEC_PER_SEC }, + { "sec", USEC_PER_SEC }, + { "s", USEC_PER_SEC }, + { "minutes", USEC_PER_MINUTE }, + { "minute", USEC_PER_MINUTE }, + { "min", USEC_PER_MINUTE }, + { "months", USEC_PER_MONTH }, + { "month", USEC_PER_MONTH }, + { "msec", USEC_PER_MSEC }, + { "ms", USEC_PER_MSEC }, + { "m", USEC_PER_MINUTE }, + { "hours", USEC_PER_HOUR }, + { "hour", USEC_PER_HOUR }, + { "hr", USEC_PER_HOUR }, + { "h", USEC_PER_HOUR }, + { "days", USEC_PER_DAY }, + { "day", USEC_PER_DAY }, + { "d", USEC_PER_DAY }, + { "weeks", USEC_PER_WEEK }, + { "week", USEC_PER_WEEK }, + { "w", USEC_PER_WEEK }, + { "years", USEC_PER_YEAR }, + { "year", USEC_PER_YEAR }, + { "y", USEC_PER_YEAR }, + { "usec", 1ULL }, + { "us", 1ULL }, + { "", USEC_PER_SEC }, /* default is sec */ + }; + + char *s = NULL; + unsigned i; + + assert(time); + + if (!t) { + if (asprintf(&s, "%g", ((double) usec)/USEC_PER_SEC) < 0) + return -ENOMEM; + + *time = s; + + return 0; + } + + for (i = 0; i < ELEMENTSOF(table); i++) { + if (streq(t, table[i].suffix)) { + if (asprintf(&s, "%g%s", ((double) usec)/table[i].usec, t) < 0) + return -ENOMEM; + + *time = s; + + return 0; + } + } + + return -EINVAL; +} + bool ntp_synced(void) { struct timex txc = {}; diff --git a/src/shared/time-util.h b/src/shared/time-util.h index b55a660..7d0eff9 100644 --- a/src/shared/time-util.h +++ b/src/shared/time-util.h @@ -100,6 +100,7 @@ int parse_timestamp(const char *t, usec_t *usec); int parse_sec(const char *t, usec_t *usec); int parse_nsec(const char *t, nsec_t *nsec); +int sec_to_stringa(const char *t, usec_t usec, const char **time); bool ntp_synced(void); diff --git a/src/test/test-time.c b/src/test/test-time.c index 8cfc4cc..09de8f0 100644 --- a/src/test/test-time.c +++ b/src/test/test-time.c @@ -86,6 +86,25 @@ static void test_parse_nsec(void) { assert_se(parse_nsec(".s ", &u) < 0); } +static void test_sec_to_stringa(void) { + _cleanup_free_ const char *time1 = NULL, *time2 = NULL, *time3 = NULL, *time4 = NULL, *time5 = NULL; + + assert_se(sec_to_stringa(NULL, 27314123, &time1) >= 0); + puts(time1); + + assert_se(sec_to_stringa("sec", 27314123, &time2) >= 0); + puts(time2); + + assert_se(sec_to_stringa("ms", 27314123, &time3) >= 0); + puts(time3); + + assert_se(sec_to_stringa("days", 27314123, &time4) >= 0); + puts(time4); + + assert_se(sec_to_stringa("years", 27314123, &time5) >= 0); + puts(time5); +} + static void test_format_timespan_one(usec_t x, usec_t accuracy) { char *r; char l[FORMAT_TIMESPAN_MAX]; @@ -156,6 +175,7 @@ static void test_get_timezones(void) { int main(int argc, char *argv[]) { test_parse_sec(); test_parse_nsec(); + test_sec_to_stringa(); test_format_timespan(1); test_format_timespan(USEC_PER_MSEC); test_format_timespan(USEC_PER_SEC); -- 1.9.3 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel