31#include "plugins_internal.h"
44static void lyplg_type_free_time(
const struct ly_ctx *ctx,
struct lyd_value *value);
50lyplg_type_store_time(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
51 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
52 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
59 uint32_t i, value_size;
60 char c, *ptr, *str = NULL;
64 memset(storage, 0,
sizeof *storage);
65 if (!strcmp(type->
name,
"time")) {
70 LY_CHECK_ERR_GOTO(!val && !val_nz, ret =
LY_EMEM, cleanup);
75 if (val && (value_size_bits < 40)) {
77 " b (expected at least 40 b).", type->
name, value_size_bits);
79 }
else if (val_nz && (value_size_bits < 32)) {
81 " b (expected at least 32 b).", type->
name, value_size_bits);
85 for (i = val ? 5 : 4; i < value_size; ++i) {
86 c = ((
char *)value)[i];
89 "(expected a digit).", type->
name, c);
103 val->
unknown_tz = *(((uint8_t *)value) + 4) ? 1 : 0;
107 if (val && (value_size > 5)) {
108 val->
fractions_s = strndup(((
char *)value) + 5, value_size - 5);
110 }
else if (val_nz && (value_size > 4)) {
111 val_nz->
fractions_s = strndup(((
char *)value) + 4, value_size - 4);
127 LY_CHECK_GOTO(ret, cleanup);
131 LY_CHECK_GOTO(ret, cleanup);
141 if (asprintf(&str,
"1970-01-01T%.*s", (
int)value_size, (
char *)value) == -1) {
155 ptr = strptime(value,
"%H:%M:%S", &tm);
156 if (!ptr || (ptr - (
char *)value != 8)) {
158 (
int)value_size, (
char *)value);
174 val_nz->
fractions_s = strndup(ptr, value_size - (ptr - (
char *)value));
182 if (val && (((
char *)value)[value_size - 1] ==
'Z')) {
191 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
192 LY_CHECK_GOTO(ret, cleanup);
195 LY_CHECK_GOTO(ret, cleanup);
206 lyplg_type_free_time(ctx, storage);
215lyplg_type_compare_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
220 const char *fr1, *fr2;
247 if ((!fr1 && !fr2) || (fr1 && fr2 && !strcmp(fr1, fr2))) {
261lyplg_type_fractions_is_zero(
char *frac)
269 for (iter = frac; *iter; iter++) {
288lyplg_type_sort_by_fractions(
char *f1,
char *f2)
290 ly_bool f1_is_zero, f2_is_zero;
293 f1_is_zero = lyplg_type_fractions_is_zero(f1);
294 f2_is_zero = lyplg_type_fractions_is_zero(f2);
296 if (f1_is_zero && !f2_is_zero) {
298 }
else if (!f1_is_zero && f2_is_zero) {
300 }
else if (f1_is_zero && f2_is_zero) {
305 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
331 dt = difftime(v1->
seconds, v2->seconds);
337 dt = difftime(vn1->
seconds, vn2->seconds);
345 return lyplg_type_sort_by_fractions(v1->
fractions_s, v2->fractions_s);
347 return lyplg_type_sort_by_fractions(vn1->
fractions_s, vn2->fractions_s);
356 void *UNUSED(prefix_data),
ly_bool *dynamic, uint64_t *value_size_bits)
364 char *ret, *fractions_s;
376 if (value_size_bits) {
377 *value_size_bits = 32;
379 return (
char *)&val_nz->
seconds;
392 ret = malloc(4 + (val ? 1 : 0) + (fractions_s ? strlen(fractions_s) : 0));
393 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
396 if (value_size_bits) {
397 *value_size_bits = 32 + (val ? 8 : 0) + (fractions_s ? strlen(fractions_s) * 8 : 0);
399 memcpy(ret, &seconds,
sizeof seconds);
401 memcpy(ret + 4, &unknown_tz,
sizeof unknown_tz);
404 memcpy(ret + (val ? 5 : 4), fractions_s, strlen(fractions_s));
421 if (!gmtime_r(&t, &tm)) {
425 if (asprintf(&ret,
"%02d:%02d:%02d%s%s%s", tm.tm_hour, tm.tm_min, tm.tm_sec,
426 fractions_s ?
"." :
"", fractions_s ? fractions_s :
"", val ?
"Z" :
"") == -1) {
441 assert(ret[10] ==
'T');
442 memmove(ret, ret + 11, strlen(ret + 11) + 1);
458 if (value_size_bits) {
459 *value_size_bits = strlen(value->
_canonical) * 8;
474 memset(dup, 0,
sizeof *dup);
478 LY_CHECK_GOTO(ret, error);
483 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
498 LY_CHECK_ERR_GOTO(!dup_val_nz, ret =
LY_EMEM, error);
516 lyplg_type_free_time(ctx, dup);
555 .module =
"ietf-yang-types",
559 .plugin.id =
"ly2 time",
561 .plugin.store = lyplg_type_store_time,
563 .plugin.validate_tree = NULL,
564 .plugin.compare = lyplg_type_compare_time,
565 .plugin.sort = lyplg_type_sort_time,
566 .plugin.print = lyplg_type_print_time,
567 .plugin.duplicate = lyplg_type_dup_time,
568 .plugin.free = lyplg_type_free_time,
571 .module =
"ietf-yang-types",
573 .name =
"time-no-zone",
575 .plugin.id =
"ly2 time",
577 .plugin.store = lyplg_type_store_time,
579 .plugin.validate_tree = NULL,
580 .plugin.compare = lyplg_type_compare_time,
581 .plugin.sort = lyplg_type_sort_time,
582 .plugin.print = lyplg_type_print_time,
583 .plugin.duplicate = lyplg_type_dup_time,
584 .plugin.free = lyplg_type_free_time,
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
LIBYANG_API_DECL const char * ly_last_logmsg(void)
Get the last (thread-specific) full logged error message.
LY_ERR
libyang's error codes returned by the libyang functions.
Libyang full error structure.
const char *const char * revision
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_value_string(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *storage, struct ly_err_item **err)
Implementation of lyplg_type_validate_value_clb for the string type.
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser's hints (if any) in the specified format.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(const struct ly_ctx *ctx, struct lysc_pattern **patterns, const char *str, uint32_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
@ LYPLG_LYB_SIZE_VARIABLE_BITS
@ LYPLG_LYB_SIZE_FIXED_BITS
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_ONLY
uint8_t ly_bool
Type to indicate boolean value.
API for (user) types plugins.
const struct lyplg_type_record plugins_time[]
Plugin information for time and time-no-zone type implementation.
const struct lysc_type * realtype
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
YANG data representation.
Special lyd_value structure for ietf-yang-types time values.
Special lyd_value structure for ietf-yang-types time-no-zone values.
LIBYANG_API_DECL LY_ERR ly_time_str2time(const char *value, time_t *time, char **fractions_s)
Convert date-and-time from string to UNIX timestamp and fractions of a second.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
LIBYANG_API_DECL LY_ERR ly_time_time2str(time_t time, const char *fractions_s, char **str)
Convert UNIX timestamp and fractions of a second into canonical date-and-time string value.