30#include "plugins_internal.h"
43static void lyplg_type_free_date_and_time(
const struct ly_ctx *ctx,
struct lyd_value *value);
49lyplg_type_store_date_and_time(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
50 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
51 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
56 uint32_t i, value_size;
60 memset(storage, 0,
sizeof *storage);
62 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
67 if (value_size_bits < 64) {
69 " b (expected at least 64 b).", value_size_bits);
73 for (i = 9; i < value_size; ++i) {
74 c = ((
char *)value)[i];
77 "(expected a digit).", c);
83 memcpy(&val->
time, value,
sizeof val->
time);
87 val->
fractions_s = strndup(((
char *)value) + 9, value_size - 9);
93 val->
unknown_tz = *(((uint8_t *)value) + 8) ? 1 : 0;
103 LY_CHECK_GOTO(ret, cleanup);
107 LY_CHECK_GOTO(ret, cleanup);
122 if (!strncmp(((
char *)value + value_size) - 6,
"-00:00", 6) || (!old_rev && (((
char *)value)[value_size - 1] ==
'Z'))) {
131 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
132 LY_CHECK_GOTO(ret, cleanup);
135 LY_CHECK_GOTO(ret, cleanup);
145 lyplg_type_free_date_and_time(ctx, storage);
151lyplg_type_store_date_and_time_old(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
152 uint64_t value_size_bits, uint32_t options,
LY_VALUE_FORMAT format,
void *prefix_data, uint32_t hints,
155 return lyplg_type_store_date_and_time(ctx, type, value, value_size_bits, options, format, prefix_data, hints,
156 ctx_node, storage, unres, 1, err);
160lyplg_type_store_date_and_time_new(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
161 uint64_t value_size_bits, uint32_t options,
LY_VALUE_FORMAT format,
void *prefix_data, uint32_t hints,
164 return lyplg_type_store_date_and_time(ctx, type, value, value_size_bits, options, format, prefix_data, hints,
165 ctx_node, storage, unres, 0, err);
172lyplg_type_compare_date_and_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
201lyplg_type_fractions_is_zero(
char *frac)
209 for (iter = frac; *iter; iter++) {
228lyplg_type_sort_by_fractions(
char *f1,
char *f2)
230 ly_bool f1_is_zero, f2_is_zero;
233 f1_is_zero = lyplg_type_fractions_is_zero(f1);
234 f2_is_zero = lyplg_type_fractions_is_zero(f2);
236 if (f1_is_zero && !f2_is_zero) {
238 }
else if (!f1_is_zero && f2_is_zero) {
240 }
else if (f1_is_zero && f2_is_zero) {
245 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
260lyplg_type_sort_date_and_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
283 void *UNUSED(prefix_data),
ly_bool old_rev,
ly_bool *dynamic, uint64_t *value_size_bits)
294 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
297 if (value_size_bits) {
300 memcpy(ret, &val->
time,
sizeof val->
time);
307 if (value_size_bits) {
308 *value_size_bits = 64;
310 ret = (
char *)&val->
time;
319 if (!gmtime_r(&val->
time, &tm)) {
323 if (asprintf(&ret,
"%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
324 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
325 val->
fractions_s ?
"." :
"", val->fractions_s ? val->fractions_s :
"", old_rev ?
"-00:00" :
"Z") == -1) {
345 if (value_size_bits) {
346 *value_size_bits = strlen(value->
_canonical) * 8;
353 void *prefix_data,
ly_bool *dynamic, uint64_t *value_size_bits)
355 return lyplg_type_print_date_and_time(ctx, value, format, prefix_data, 1, dynamic, value_size_bits);
360 void *prefix_data,
ly_bool *dynamic, uint64_t *value_size_bits)
362 return lyplg_type_print_date_and_time(ctx, value, format, prefix_data, 0, dynamic, value_size_bits);
374 memset(dup, 0,
sizeof *dup);
378 LY_CHECK_GOTO(ret, error);
382 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
400 lyplg_type_free_date_and_time(ctx, dup);
408lyplg_type_free_date_and_time(
const struct ly_ctx *ctx,
struct lyd_value *value)
430 .module =
"ietf-yang-types",
432 .name =
"date-and-time",
434 .plugin.id =
"ly2 date-and-time",
436 .plugin.store = lyplg_type_store_date_and_time_old,
437 .plugin.validate_value = NULL,
438 .plugin.validate_tree = NULL,
439 .plugin.compare = lyplg_type_compare_date_and_time,
440 .plugin.sort = lyplg_type_sort_date_and_time,
441 .plugin.print = lyplg_type_print_date_and_time_old,
442 .plugin.duplicate = lyplg_type_dup_date_and_time,
443 .plugin.free = lyplg_type_free_date_and_time,
446 .module =
"ietf-yang-types",
447 .revision =
"2025-12-22",
448 .name =
"date-and-time",
450 .plugin.id =
"ly2 date-and-time",
452 .plugin.store = lyplg_type_store_date_and_time_new,
453 .plugin.validate_value = NULL,
454 .plugin.validate_tree = NULL,
455 .plugin.compare = lyplg_type_compare_date_and_time,
456 .plugin.sort = lyplg_type_sort_date_and_time,
457 .plugin.print = lyplg_type_print_date_and_time_new,
458 .plugin.duplicate = lyplg_type_dup_date_and_time,
459 .plugin.free = lyplg_type_free_date_and_time,
const struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
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_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_BYTES
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 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 date-and-time 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.