diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-04-17 14:25:55 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-04-17 14:26:13 +0200 |
commit | 50d0ee6161da036cf7e8761f945793f09227fa10 (patch) | |
tree | 013104e94562079d2c790d6253faa99726c40c45 /src/json/json_helper.c | |
parent | a58d36b8da7afa42410bac54f57d5f3b6b6c4391 (diff) | |
download | gnunet-50d0ee6161da036cf7e8761f945793f09227fa10.tar.gz gnunet-50d0ee6161da036cf7e8761f945793f09227fa10.zip |
add GNUNET_JSON_spec_absolute_time_nbo
Diffstat (limited to 'src/json/json_helper.c')
-rw-r--r-- | src/json/json_helper.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c index dca2106aa..194ec5c76 100644 --- a/src/json/json_helper.c +++ b/src/json/json_helper.c | |||
@@ -610,6 +610,83 @@ GNUNET_JSON_spec_absolute_time (const char *name, | |||
610 | 610 | ||
611 | 611 | ||
612 | /** | 612 | /** |
613 | * Parse given JSON object to absolute time. | ||
614 | * | ||
615 | * @param cls closure, NULL | ||
616 | * @param root the json object representing data | ||
617 | * @param[out] spec where to write the data | ||
618 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
619 | */ | ||
620 | static int | ||
621 | parse_abs_time_nbo (void *cls, | ||
622 | json_t *root, | ||
623 | struct GNUNET_JSON_Specification *spec) | ||
624 | { | ||
625 | struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr; | ||
626 | const char *val; | ||
627 | unsigned long long int tval; | ||
628 | struct GNUNET_TIME_Absolute a; | ||
629 | |||
630 | val = json_string_value (root); | ||
631 | if (NULL == val) | ||
632 | { | ||
633 | GNUNET_break_op (0); | ||
634 | return GNUNET_SYSERR; | ||
635 | } | ||
636 | if ( (0 == strcasecmp (val, | ||
637 | "/forever/")) || | ||
638 | (0 == strcasecmp (val, | ||
639 | "/end of time/")) || | ||
640 | (0 == strcasecmp (val, | ||
641 | "/never/")) ) | ||
642 | { | ||
643 | *abs = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS); | ||
644 | return GNUNET_OK; | ||
645 | } | ||
646 | if (1 != sscanf (val, | ||
647 | "/Date(%llu)/", | ||
648 | &tval)) | ||
649 | { | ||
650 | GNUNET_break_op (0); | ||
651 | return GNUNET_SYSERR; | ||
652 | } | ||
653 | /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ | ||
654 | a.abs_value_us = tval * 1000LL * 1000LL; | ||
655 | if ( (a.abs_value_us) / 1000LL / 1000LL != tval) | ||
656 | { | ||
657 | /* Integer overflow */ | ||
658 | GNUNET_break_op (0); | ||
659 | return GNUNET_SYSERR; | ||
660 | } | ||
661 | *abs = GNUNET_TIME_absolute_hton (a); | ||
662 | return GNUNET_OK; | ||
663 | } | ||
664 | |||
665 | |||
666 | /** | ||
667 | * Absolute time in network byte order. | ||
668 | * | ||
669 | * @param name name of the JSON field | ||
670 | * @param[out] at where to store the absolute time found under @a name | ||
671 | */ | ||
672 | struct GNUNET_JSON_Specification | ||
673 | GNUNET_JSON_spec_absolute_time_nbo (const char *name, | ||
674 | struct GNUNET_TIME_AbsoluteNBO *at) | ||
675 | { | ||
676 | struct GNUNET_JSON_Specification ret = { | ||
677 | .parser = &parse_abs_time_nbo, | ||
678 | .cleaner = NULL, | ||
679 | .cls = NULL, | ||
680 | .field = name, | ||
681 | .ptr = at, | ||
682 | .ptr_size = sizeof (uint64_t), | ||
683 | .size_ptr = NULL | ||
684 | }; | ||
685 | return ret; | ||
686 | } | ||
687 | |||
688 | |||
689 | /** | ||
613 | * Parse given JSON object to relative time. | 690 | * Parse given JSON object to relative time. |
614 | * | 691 | * |
615 | * @param cls closure, NULL | 692 | * @param cls closure, NULL |