30 #include "ly_common.h"
31 #include "plugins_internal.h"
44 static void lyplg_type_free_date_and_time(
const struct ly_ctx *ctx,
struct lyd_value *value);
50 lyplg_type_store_date_and_time(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
51 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
57 uint32_t i, value_size;
61 memset(storage, 0,
sizeof *storage);
63 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
68 if (value_size_bits < 64) {
70 " b (expected at least 64 b).", value_size_bits);
74 for (i = 9; i < value_size; ++i) {
75 c = ((
char *)value)[i];
78 "(expected a digit).", c);
84 memcpy(&val->
time, value,
sizeof val->
time);
88 val->
fractions_s = strndup(((
char *)value) + 9, value_size - 9);
94 val->
unknown_tz = *(((uint8_t *)value) + 8) ? 1 : 0;
104 LY_CHECK_GOTO(ret, cleanup);
108 LY_CHECK_GOTO(ret, cleanup);
117 if (!strncmp(((
char *)value + value_size) - 6,
"-00:00", 6)) {
126 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
127 LY_CHECK_GOTO(ret, cleanup);
130 LY_CHECK_GOTO(ret, cleanup);
135 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
140 lyplg_type_free_date_and_time(ctx, storage);
149 lyplg_type_compare_date_and_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
178 lyplg_type_fractions_is_zero(
char *frac)
186 for (iter = frac; *iter; iter++) {
207 ly_bool f1_is_zero, f2_is_zero;
210 f1_is_zero = lyplg_type_fractions_is_zero(f1);
211 f2_is_zero = lyplg_type_fractions_is_zero(f2);
213 if (f1_is_zero && !f2_is_zero) {
215 }
else if (!f1_is_zero && f2_is_zero) {
217 }
else if (f1_is_zero && f2_is_zero) {
222 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
237 lyplg_type_sort_date_and_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
260 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_size_bits)
271 LY_CHECK_ERR_RET(!ret,
LOGMEM(ctx), NULL);
274 if (value_size_bits) {
277 memcpy(ret, &val->
time,
sizeof val->
time);
284 if (value_size_bits) {
285 *value_size_bits = 64;
287 ret = (
char *)&val->
time;
296 if (!gmtime_r(&val->
time, &tm)) {
299 if (asprintf(&ret,
"%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
300 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
321 if (value_size_bits) {
322 *value_size_bits = strlen(value->
_canonical) * 8;
336 memset(dup, 0,
sizeof *dup);
340 LY_CHECK_GOTO(ret, error);
344 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
362 lyplg_type_free_date_and_time(ctx, dup);
370 lyplg_type_free_date_and_time(
const struct ly_ctx *ctx,
struct lyd_value *value)
392 .
module =
"ietf-yang-types",
393 .revision =
"2013-07-15",
394 .name =
"date-and-time",
396 .plugin.id =
"ly2 date-and-time",
398 .plugin.store = lyplg_type_store_date_and_time,
399 .plugin.validate_value = NULL,
400 .plugin.validate_tree = NULL,
401 .plugin.compare = lyplg_type_compare_date_and_time,
402 .plugin.sort = lyplg_type_sort_date_and_time,
403 .plugin.print = lyplg_type_print_date_and_time,
404 .plugin.duplicate = lyplg_type_dup_date_and_time,
405 .plugin.free = lyplg_type_free_date_and_time,
struct lysc_type * realtype
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...
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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.
YANG extension compiled instance.
LY_ERR
libyang's error codes returned by the libyang functions.
uint8_t ly_bool
Type to indicate boolean value.
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-yang-types date-and-time values.
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes...
struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
YANG data representation.
Libyang full error structure.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
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.
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(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 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.
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, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
LIBYANG_API_DECL const char * ly_last_logmsg(void)
Get the last (thread-specific) full logged error message.
#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_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.
API for (user) types plugins.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
assert(!value->_canonical)