rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit bda69cbe49f86f808de0767ac0644a12718a98bf
parent b9a7fc39ba92798d1a5a08f02742cc080bdf2df7
Author: vaplv <vaplv@free.fr>
Date:   Mon,  8 Dec 2014 11:53:31 +0100

Add the TIME_DUMP_AUTO constant

Diffstat:
Msrc/clock_time.c | 24++++++++++++++++--------
Msrc/clock_time.h | 17++++++++++-------
Msrc/test_time.c | 4+++-
3 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/src/clock_time.c b/src/clock_time.c @@ -153,36 +153,44 @@ time_dump time_nsec = TIME_TO_NSEC(time); if(flag & TIME_DAY) { const int64_t nb_days = time_nsec / NSEC_PER_DAY; - DUMP(nb_days, "day"); + if(flag != TIME_DUMP_AUTO || nb_days) + DUMP(nb_days, "day"); time_nsec -= nb_days * NSEC_PER_DAY; } if(flag & TIME_HOUR) { const int64_t nb_hours = time_nsec / NSEC_PER_HOUR; - DUMP(nb_hours, "hour"); + if(flag != TIME_DUMP_AUTO || nb_hours) + DUMP(nb_hours, "hour"); time_nsec -= nb_hours * NSEC_PER_HOUR; } if(flag & TIME_MIN) { const int64_t nb_mins = time_nsec / NSEC_PER_MIN; - DUMP(nb_mins, "min"); + if(flag != TIME_DUMP_AUTO || nb_mins) + DUMP(nb_mins, "min"); time_nsec -= nb_mins * NSEC_PER_MIN; } if(flag & TIME_SEC) { const int64_t nb_secs = time_nsec / NSEC_PER_SEC; - DUMP(nb_secs, "sec"); + if(flag != TIME_DUMP_AUTO || nb_secs) + DUMP(nb_secs, "sec"); time_nsec -= nb_secs * NSEC_PER_SEC; } if(flag & TIME_MSEC) { const int64_t nb_msecs = time_nsec / NSEC_PER_MSEC; - DUMP(nb_msecs, "msec"); + if(flag != TIME_DUMP_AUTO || nb_msecs) + DUMP(nb_msecs, "msec"); time_nsec -= nb_msecs * NSEC_PER_MSEC; } if(flag & TIME_USEC) { const int64_t nb_usecs = time_nsec / NSEC_PER_USEC; - DUMP(nb_usecs, "usec"); + if(flag != TIME_DUMP_AUTO || nb_usecs) + DUMP(nb_usecs, "usec"); time_nsec -= nb_usecs * NSEC_PER_USEC; } - if(flag & TIME_NSEC) - DUMP(time_nsec, "nsec"); + if(flag & TIME_NSEC) { + if(flag != TIME_DUMP_AUTO || time_nsec) + DUMP(time_nsec, "nsec"); + } #undef DUMP diff --git a/src/clock_time.h b/src/clock_time.h @@ -24,6 +24,9 @@ struct time { int64_t nsec; }; +/* time_dump flag constant used to auto-format the time_dump message */ +#define TIME_DUMP_AUTO (-1) + enum time_unit { TIME_NSEC = BIT(0), TIME_USEC = BIT(1), @@ -42,14 +45,14 @@ time_current RSYS_API void time_sub - (struct time* res, - const struct time* a, + (struct time* res, + const struct time* a, const struct time* b); RSYS_API void time_add - (struct time* res, - const struct time* a, + (struct time* res, + const struct time* a, const struct time* b); RSYS_API int64_t @@ -60,9 +63,9 @@ time_val RSYS_API void time_dump (const struct time* time, - int flag, - size_t* real_dump_len, /* May be NULL. */ - char* dump, /* May be NULL. */ + int flag, /* Combination of time_unit or TIME_DUMP_AUTO */ + size_t* real_dump_len, /* May be NULL */ + char* dump, /* May be NULL */ size_t max_dump_len); END_DECLS diff --git a/src/test_time.c b/src/test_time.c @@ -37,7 +37,9 @@ main(int argc, char** argv) CHECK(time_val(&res, TIME_SEC), time / 1000000000); time_dump - (&res, TIME_SEC|TIME_MSEC|TIME_USEC|TIME_NSEC, NULL, dump, sizeof(dump)); + (&res, TIME_SEC|TIME_MSEC|TIME_USEC|TIME_NSEC, NULL, dump, sizeof(dump)); + printf("%s\n", dump); + time_dump(&res, TIME_DUMP_AUTO, NULL, dump, sizeof(dump)); printf("%s\n", dump); return 0; }