diff options
-rw-r--r-- | src/util/json.c | 9 | ||||
-rw-r--r-- | src/util/test_json.c | 43 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/util/json.c b/src/util/json.c index 90031faae..8abcef1c9 100644 --- a/src/util/json.c +++ b/src/util/json.c @@ -83,6 +83,9 @@ TALER_json_from_abs (struct GNUNET_TIME_Absolute stamp) json_t *j; char *mystr; int ret; + + if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) + return json_string ("never"); ret = GNUNET_asprintf (&mystr, "%llu", (long long) (stamp.abs_value_us / (1000 * 1000))); @@ -259,6 +262,12 @@ TALER_json_to_abs (json_t *json, GNUNET_assert (NULL != abs); EXITIF (NULL == (str = json_string_value (json))); + if (0 == strcasecmp (str, + "never")) + { + *abs = GNUNET_TIME_UNIT_FOREVER_ABS; + return GNUNET_OK; + } EXITIF (1 > sscanf (str, "%llu", &abs_value_s)); abs->abs_value_us = abs_value_s * 1000 * 1000; return GNUNET_OK; diff --git a/src/util/test_json.c b/src/util/test_json.c index dd494c679..a1a5cd54f 100644 --- a/src/util/test_json.c +++ b/src/util/test_json.c @@ -24,6 +24,11 @@ #include "taler_json_lib.h" +/** + * Test amount conversion from/to JSON. + * + * @return 0 on success + */ static int test_amount () { @@ -47,6 +52,42 @@ test_amount () } +/** + * Test time conversion from/to JSON. + * + * @return 0 on success + */ +static int +test_time () +{ + json_t *j; + struct GNUNET_TIME_Absolute a1; + struct GNUNET_TIME_Absolute a2; + + a1 = GNUNET_TIME_absolute_get (); + a1.abs_value_us -= a1.abs_value_us % 1000000; /* round! */ + j = TALER_json_from_abs (a1); + GNUNET_assert (NULL != j); + GNUNET_assert (GNUNET_OK == + TALER_json_to_abs (j, + &a2)); + GNUNET_assert (a1.abs_value_us == + a2.abs_value_us); + json_decref (j); + + a1 = GNUNET_TIME_UNIT_FOREVER_ABS; + j = TALER_json_from_abs (a1); + GNUNET_assert (NULL != j); + GNUNET_assert (GNUNET_OK == + TALER_json_to_abs (j, + &a2)); + GNUNET_assert (a1.abs_value_us == + a2.abs_value_us); + json_decref (j); + return 0; +} + + int main(int argc, const char *const argv[]) @@ -56,6 +97,8 @@ main(int argc, NULL); if (0 != test_amount ()) return 1; + if (0 != test_time ()) + return 1; /* FIXME: implement test... */ return 0; } |