libyang 5.0.8
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
date_and_time.c
Go to the documentation of this file.
1
14
15#define _GNU_SOURCE /* strdup */
16
17#include "plugins_types.h"
18
19#include <assert.h>
20#include <ctype.h>
21#include <errno.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
25#include <time.h>
26
27#include "libyang.h"
28
29#include "compat.h"
30#include "ly_common.h"
31#include "plugins_internal.h" /* LY_TYPE_*_STR */
32
43
44static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
45
49static LY_ERR
50lyplg_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,
52 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
53 struct ly_err_item **err)
54{
55 LY_ERR ret = LY_SUCCESS;
56 struct lyd_value_date_and_time *val;
57 uint32_t i, value_size;
58 char c;
59
60 /* init storage */
61 memset(storage, 0, sizeof *storage);
63 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
64 storage->realtype = type;
65
66 if (format == LY_VALUE_LYB) {
67 /* validation */
68 if (value_size_bits < 64) {
69 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %" PRIu32
70 " b (expected at least 64 b).", value_size_bits);
71 goto cleanup;
72 }
73 value_size = LYPLG_BITS2BYTES(value_size_bits);
74 for (i = 9; i < value_size; ++i) {
75 c = ((char *)value)[i];
76 if (!isdigit(c)) {
77 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time character '%c' "
78 "(expected a digit).", c);
79 goto cleanup;
80 }
81 }
82
83 /* store timestamp */
84 memcpy(&val->time, value, sizeof val->time);
85
86 /* store fractions of second */
87 if (value_size > 9) {
88 val->fractions_s = strndup(((char *)value) + 9, value_size - 9);
89 LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
90 }
91
92 /* store unknown timezone */
93 if (value_size > 8) {
94 val->unknown_tz = *(((uint8_t *)value) + 8) ? 1 : 0;
95 }
96
97 /* success */
98 goto cleanup;
99 }
100
101 /* get value byte length */
102 ret = lyplg_type_check_value_size("date-and-time", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES, 0,
103 &value_size, err);
104 LY_CHECK_GOTO(ret, cleanup);
105
106 /* check hints */
107 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
108 LY_CHECK_GOTO(ret, cleanup);
109
110 if (!(options & LYPLG_TYPE_STORE_ONLY)) {
111 /* validate value */
112 ret = lyplg_type_validate_patterns(ctx, ((struct lysc_type_str *)type)->patterns, value, value_size, err);
113 LY_CHECK_RET(ret);
114 }
115
116 /* convert to UNIX time and fractions of second */
117 ret = ly_time_str2time(value, &val->time, &val->fractions_s);
118 if (ret) {
119 ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
120 goto cleanup;
121 }
122
123 if (!strncmp(((char *)value + value_size) - 6, "-00:00", 6) || (((char *)value)[value_size - 1] == 'Z')) {
124 /* unknown timezone, timezone format supported for backwards compatibility */
125 val->unknown_tz = 1;
126 }
127
128 if (format == LY_VALUE_CANON) {
129 /* store canonical value */
130 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
131 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
132 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
133 LY_CHECK_GOTO(ret, cleanup);
134 } else {
135 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
136 LY_CHECK_GOTO(ret, cleanup);
137 }
138 }
139
140cleanup:
141 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
142 free((void *)value);
143 }
144
145 if (ret) {
146 lyplg_type_free_date_and_time(ctx, storage);
147 }
148 return ret;
149}
150
154static LY_ERR
155lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
156 const struct lyd_value *val2)
157{
158 struct lyd_value_date_and_time *v1, *v2;
159
160 LYD_VALUE_GET(val1, v1);
161 LYD_VALUE_GET(val2, v2);
162
163 /* compare timestamp and unknown tz */
164 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
165 return LY_ENOT;
166 }
167
168 /* compare second fractions */
169 if ((!v1->fractions_s && !v2->fractions_s) ||
170 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
171 return LY_SUCCESS;
172 }
173 return LY_ENOT;
174}
175
183static ly_bool
184lyplg_type_fractions_is_zero(char *frac)
185{
186 char *iter;
187
188 if (!frac) {
189 return 1;
190 }
191
192 for (iter = frac; *iter; iter++) {
193 if (*iter != '0') {
194 return 0;
195 }
196 }
197
198 return 1;
199}
200
210static int
211lyplg_type_sort_by_fractions(char *f1, char *f2)
212{
213 ly_bool f1_is_zero, f2_is_zero;
214 int df;
215
216 f1_is_zero = lyplg_type_fractions_is_zero(f1);
217 f2_is_zero = lyplg_type_fractions_is_zero(f2);
218
219 if (f1_is_zero && !f2_is_zero) {
220 return -1;
221 } else if (!f1_is_zero && f2_is_zero) {
222 return 1;
223 } else if (f1_is_zero && f2_is_zero) {
224 return 0;
225 }
226
227 /* both f1 and f2 have some non-zero number */
228 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
229 df = strcmp(f1, f2);
230 if (df > 0) {
231 return 1;
232 } else if (df < 0) {
233 return -1;
234 } else {
235 return 0;
236 }
237}
238
242static int
243lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
244{
245 struct lyd_value_date_and_time *v1, *v2;
246 double dt;
247
248 LYD_VALUE_GET(val1, v1);
249 LYD_VALUE_GET(val2, v2);
250
251 /* compare timestamps */
252 dt = difftime(v1->time, v2->time);
253 if (dt != 0) {
254 return dt;
255 }
256
257 /* compare second fractions */
258 return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s);
259}
260
264static const void *
265lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
266 void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
267{
268 struct lyd_value_date_and_time *val;
269 struct tm tm;
270 char *ret;
271
272 LYD_VALUE_GET(value, val);
273
274 if (format == LY_VALUE_LYB) {
275 if (val->unknown_tz || val->fractions_s) {
276 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
277 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
278
279 *dynamic = 1;
280 if (value_size_bits) {
281 *value_size_bits = 64 + 8 + (val->fractions_s ? strlen(val->fractions_s) * 8 : 0);
282 }
283 memcpy(ret, &val->time, sizeof val->time);
284 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
285 if (val->fractions_s) {
286 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
287 }
288 } else {
289 *dynamic = 0;
290 if (value_size_bits) {
291 *value_size_bits = 64;
292 }
293 ret = (char *)&val->time;
294 }
295 return ret;
296 }
297
298 /* generate canonical value if not already */
299 if (!value->_canonical) {
300 if (val->unknown_tz) {
301 /* ly_time_time2str but always using GMT */
302 if (!gmtime_r(&val->time, &tm)) {
303 return NULL;
304 }
305
306 if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%sZ",
307 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
308 val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
309 return NULL;
310 }
311 } else {
312 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
313 return NULL;
314 }
315 }
316
317 /* store it */
318 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
319 LOGMEM(ctx);
320 return NULL;
321 }
322 }
323
324 /* use the cached canonical value */
325 if (dynamic) {
326 *dynamic = 0;
327 }
328 if (value_size_bits) {
329 *value_size_bits = strlen(value->_canonical) * 8;
330 }
331 return value->_canonical;
332}
333
337static LY_ERR
338lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
339{
340 LY_ERR ret;
341 struct lyd_value_date_and_time *orig_val, *dup_val;
342
343 memset(dup, 0, sizeof *dup);
344
345 /* optional canonical value */
346 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
347 LY_CHECK_GOTO(ret, error);
348
349 /* allocate value */
350 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
351 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
352
353 LYD_VALUE_GET(original, orig_val);
354
355 /* copy timestamp and unknown tz */
356 dup_val->time = orig_val->time;
357 dup_val->unknown_tz = orig_val->unknown_tz;
358
359 /* duplicate second fractions */
360 if (orig_val->fractions_s) {
361 dup_val->fractions_s = strdup(orig_val->fractions_s);
362 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
363 }
364
365 dup->realtype = original->realtype;
366 return LY_SUCCESS;
367
368error:
369 lyplg_type_free_date_and_time(ctx, dup);
370 return ret;
371}
372
376static void
377lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
378{
379 struct lyd_value_date_and_time *val;
380
381 lydict_remove(ctx, value->_canonical);
382 value->_canonical = NULL;
383 LYD_VALUE_GET(value, val);
384 if (val) {
385 free(val->fractions_s);
387 }
388}
389
398 {
399 .module = "ietf-yang-types",
400 .revision = "2025-12-22",
401 .name = "date-and-time",
402
403 .plugin.id = "ly2 date-and-time",
404 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
405 .plugin.store = lyplg_type_store_date_and_time,
406 .plugin.validate_value = NULL,
407 .plugin.validate_tree = NULL,
408 .plugin.compare = lyplg_type_compare_date_and_time,
409 .plugin.sort = lyplg_type_sort_date_and_time,
410 .plugin.print = lyplg_type_print_date_and_time,
411 .plugin.duplicate = lyplg_type_dup_date_and_time,
412 .plugin.free = lyplg_type_free_date_and_time,
413 },
414 {0}
415};
const struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
libyang context handler.
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.
Definition log.h:252
@ LYVE_DATA
Definition log.h:289
@ LY_EMEM
Definition log.h:254
@ LY_ENOT
Definition log.h:266
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:297
#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, 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.
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, uint32_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
LY_DATA_TYPE basetype
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_VALUE_CANON
Definition tree.h:235
@ LY_VALUE_LYB
Definition tree.h:240
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
API for (user) types plugins.
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.
const struct lysc_type * realtype
Definition tree_data.h:544
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.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:583
const char * _canonical
Definition tree_data.h:541
YANG data representation.
Definition tree_data.h:540
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition tree_data.h:676
#define LOGMEM(CTX)
Definition tree_edit.h:22