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,
size_t value_len,
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),
61 memset(storage, 0,
sizeof *storage);
63 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
70 "(expected at least 8).", value_len);
73 for (i = 9; i < value_len; ++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_len - 9);
93 val->
unknown_tz = *(((int8_t *)value) + 8) ? 1 : 0;
102 LY_CHECK_GOTO(ret, cleanup);
110 if (!strncmp(((
char *)value + value_len) - 6,
"-00:00", 6)) {
119 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
120 LY_CHECK_GOTO(ret, cleanup);
123 LY_CHECK_GOTO(ret, cleanup);
128 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
133 lyplg_type_free_date_and_time(ctx, storage);
142 lyplg_type_compare_date_and_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
171 lyplg_type_fractions_is_zero(
char *frac)
179 for (iter = frac; *iter; iter++) {
200 ly_bool f1_is_zero, f2_is_zero;
203 f1_is_zero = lyplg_type_fractions_is_zero(f1);
204 f2_is_zero = lyplg_type_fractions_is_zero(f2);
206 if (f1_is_zero && !f2_is_zero) {
208 }
else if (!f1_is_zero && f2_is_zero) {
210 }
else if (f1_is_zero && f2_is_zero) {
215 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
230 lyplg_type_sort_date_and_time(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
253 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
264 LY_CHECK_ERR_RET(!ret,
LOGMEM(ctx), NULL);
270 memcpy(ret, &val->
time,
sizeof val->
time);
280 ret = (
char *)&val->
time;
289 if (!gmtime_r(&val->
time, &tm)) {
292 if (asprintf(&ret,
"%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
293 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
329 memset(dup, 0,
sizeof *dup);
333 LY_CHECK_GOTO(ret, error);
337 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
355 lyplg_type_free_date_and_time(ctx, dup);
363 lyplg_type_free_date_and_time(
const struct ly_ctx *ctx,
struct lyd_value *value)
385 .
module =
"ietf-yang-types",
386 .revision =
"2013-07-15",
387 .name =
"date-and-time",
389 .plugin.id =
"libyang 2 - date-and-time, version 1",
390 .plugin.store = lyplg_type_store_date_and_time,
391 .plugin.validate = NULL,
392 .plugin.compare = lyplg_type_compare_date_and_time,
393 .plugin.sort = lyplg_type_sort_date_and_time,
394 .plugin.print = lyplg_type_print_date_and_time,
395 .plugin.duplicate = lyplg_type_dup_date_and_time,
396 .plugin.free = lyplg_type_free_date_and_time,
397 .plugin.lyb_data_len = -1,
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)
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.
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 lyplg_type_check_hints(uint32_t hints, const char *value, size_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 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.
API for (user) types plugins.
LY_ERR
libyang's error codes returned by the libyang functions.
assert(!value->_canonical)