diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-05-17 17:01:25 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-05-17 17:01:25 +0200 |
commit | b1401f93da11d38d49a5117d4f717fec384e7176 (patch) | |
tree | 39d197e4ce5b92ce843dec69caacb8ab8aaea5e6 /src | |
parent | 3817f83dd5b65fb7d475560b2637276a89f69087 (diff) | |
download | exchange-b1401f93da11d38d49a5117d4f717fec384e7176.tar.xz |
test json-time conversion, add support for forever/never time
Diffstat (limited to 'src')
-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; } |