#include <common/utils.h>
#include <pthread.h>
-#define USEC_PER_SEC 1000000
-
/*
* Quit pipe for all threads. This permits a single cancellation point
* for all threads when receiving an event on the pipe.
#include <time.h>
#include <stdbool.h>
-#define MSEC_PER_SEC 1000ULL
-#define NSEC_PER_SEC 1000000000ULL
-#define NSEC_PER_MSEC 1000000ULL
-#define NSEC_PER_USEC 1000ULL
+#define MSEC_PER_SEC 1000ULL
+#define NSEC_PER_SEC 1000000000ULL
+#define NSEC_PER_MSEC 1000000ULL
+#define NSEC_PER_USEC 1000ULL
+#define USEC_PER_SEC 1000000ULL
+#define USEC_PER_MSEC 1000ULL
+
+#define SEC_PER_MINUTE 60ULL
+#define MINUTE_PER_HOUR 60ULL
+
+#define USEC_PER_MINUTE (USEC_PER_SEC * SEC_PER_MINUTE)
+#define USEC_PER_HOURS (USEC_PER_MINUTE * MINUTE_PER_HOUR)
bool locale_supports_utf8(void);
/**
* Parse a string that represents a time in human readable format. It
- * supports decimal integers suffixed by 's', 'u', 'm', 'us', and 'ms'.
+ * supports decimal integers suffixed by:
+ * "us" for microsecond,
+ * "ms" for millisecond,
+ * "s" for second,
+ * "m" for minute,
+ * "h" for hour
*
* The suffix multiply the integer by:
- * 'u'/'us': 1
- * 'm'/'ms': 1000
- * 's': 1000000
+ * "us" : 1
+ * "ms" : 1000
+ * "s" : 1000000
+ * "m" : 60000000
+ * "h" : 3600000000
*
* Note that unit-less numbers are assumed to be microseconds.
*
{
int ret;
uint64_t base_time;
- long multiplier = 1;
+ uint64_t multiplier = 1;
const char *str_end;
char *num_end;
/* Check if a prefix is present. */
switch (*num_end) {
case 'u':
- multiplier = 1;
- /* Skip another letter in the 'us' case. */
- num_end += (*(num_end + 1) == 's') ? 2 : 1;
+ /*
+ * Microsecond (us)
+ *
+ * Skip the "us" if the string matches the "us" suffix,
+ * otherwise let the check for the end of the string handle
+ * the error reporting.
+ */
+ if (*(num_end + 1) == 's') {
+ num_end += 2;
+ }
break;
case 'm':
- multiplier = 1000;
- /* Skip another letter in the 'ms' case. */
- num_end += (*(num_end + 1) == 's') ? 2 : 1;
+ if (*(num_end + 1) == 's') {
+ /* Millisecond (ms) */
+ multiplier = USEC_PER_MSEC;
+ /* Skip the 's' */
+ num_end++;
+ } else {
+ /* Minute (m) */
+ multiplier = USEC_PER_MINUTE;
+ }
+ num_end++;
break;
case 's':
- multiplier = 1000000;
+ /* Second */
+ multiplier = USEC_PER_SEC;
+ num_end++;
+ break;
+ case 'h':
+ /* Hour */
+ multiplier = USEC_PER_HOURS;
num_end++;
break;
case '\0':
#include <assert.h>
#include <string.h>
#include <stdio.h>
+#include <inttypes.h>
#include <tap/tap.h>
static struct valid_test_input valid_tests_inputs[] = {
{ "0", 0 },
{ "1234", 1234 },
- { "0u", 0 },
- { "1234u", 1234 },
- { "16m", 16000 },
- { "128m", 128000 },
+ { "1234us", 1234 },
+ { "16ms", 16000 },
+ { "128ms", 128000 },
{ "32s", 32000000 },
+ { "1m", 60000000 },
+ { "20m", 1200000000 },
+ { "1h", 3600000000 },
+ { "5h", 18000000000 },
{ "00", 0 },
- { "0m", 0 },
+ { "0us", 0 },
+ { "0ms", 0 },
{ "0s", 0 },
- { "00m", 0 },
+ { "0m", 0 },
+ { "0h", 0 },
+ { "00us", 0 },
+ { "00ms", 0 },
{ "00s", 0 },
+ { "00m", 0 },
+ { "00h", 0 },
{ "12ms", 12000 },
{ "3597us", 3597 },
{ "+5", 5 },
"14ns",
"14ms garbage after value",
"0x14s",
+ "0u",
+ "5mS",
+ "5Ms",
+ "12ussr",
+ "67msrp",
+ "14si",
+ "12mo",
+ "53hi",
};
static const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
/* Test valid cases */
for (i = 0; i < num_valid_tests; i++) {
- char name[100];
-
- sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
+ char name[256];
ret = utils_parse_time_suffix(valid_tests_inputs[i].input, &result);
+ sprintf(name, "valid test case: %s expected %" PRIu64, valid_tests_inputs[i].input, result);
ok(ret == 0 && result == valid_tests_inputs[i].expected_result, name);
}