[systemd-devel] [PATCH v5 2/4] util: introduce new sec_to_stringa()

Lennart Poettering lennart at poettering.net
Mon Dec 8 06:41:54 PST 2014


On Mon, 08.12.14 21:18, WaLyong Cho (walyong.cho at samsung.com) wrote:

> +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;
> +
> +        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;
> +}

Why this call? format_timespan() already does this, no?


Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list