/** \brief Convert timestamp in uint64_t to string. * * Timestamp is composed from Unix time (number of seconds that have elapsed * since 1.1.1970 UTC) and milliseconds. Seconds are multiplied by 1000, * afterward milliseconds are added. * * \param[in] ts Seconds and milliseconds in one uint64_t variable. * \return String timestamp representation. Static memory. */ static const char * timestamp_to_str(const uint64_t *ts) { time_t sec; uint64_t msec; size_t off; struct tm *(*timeconv)(const time_t *); timeconv = output_params.ts_localtime ? localtime : gmtime; switch (output_params.ts_conv) { case OUTPUT_TS_CONV_NONE: snprintf(global_str, sizeof (global_str), "%" PRIu64, *ts); break; case OUTPUT_TS_CONV_STR: assert(output_params.ts_conv_str != NULL); sec = *ts / 1000; msec = *ts % 1000; off = strftime(global_str, sizeof (global_str), output_params.ts_conv_str, timeconv(&sec)); snprintf(global_str + off, sizeof (global_str) - off, ".%.3" PRIu64, msec); break; default: assert(!"unknown timestamp conversion"); } return global_str; }
/* Format leaky bucket as a string. Caller must free string */ char *bucket_output(const struct bucket_conf *c, struct leaky_bucket *b) { char *buf; if (c->capacity == 0) { asprintf(&buf, "not enabled"); } else { int unit = 0; //bucket_age(c, b, bucket_time()); timeconv(c->tunit, &unit); asprintf(&buf, "%u in %u%c", b->count + b->excess, c->agetime/unit, c->tunit); } return buf; }
/* capacity / time time: number [hmds] capacity: number [kmg] */ static int parse_rate(const char *rate, struct bucket_conf *c) { char cunit[2], tunit[2]; unsigned cap, t; int n; int unit; cunit[0] = 0; tunit[0] = 0; n = sscanf(rate, "%u %1s / %u %1s", &cap, cunit, &t, tunit); if (n != 4) { cunit[0] = 0; tunit[0] = 0; if (n <= 2) { n = sscanf(rate, "%u / %u %1s", &cap, &t, tunit); if (n < 2) return -1; } else return -1; } if (t == 0 || cap == 0) return -1; switch (tolower(cunit[0])) { case 'g': cap *= 1000; case 'm': cap *= 1000; case 'k': cap *= 1000; case 0: break; default: return -1; } c->tunit = tolower(tunit[0]); if (timeconv(c->tunit, &unit) < 0) return -1; c->agetime = unit * t; c->capacity = cap; return 0; }