libyang 6.4.3
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
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 "compat.h"
28#include "dict.h"
29#include "ly_common.h"
30#include "plugins_internal.h"
31
43static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
44
48static LY_ERR
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),
52 ly_bool old_rev, struct ly_err_item **err)
53{
54 LY_ERR ret = LY_SUCCESS;
55 struct lyd_value_date_and_time *val;
56 uint32_t i, value_size;
57 char c;
58
59 /* init storage */
60 memset(storage, 0, sizeof *storage);
62 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
63 storage->realtype = type;
64
65 if (format == LY_VALUE_LYB) {
66 /* validation */
67 if (value_size_bits < 64) {
68 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %" PRIu64
69 " b (expected at least 64 b).", value_size_bits);
70 goto cleanup;
71 }
72 value_size = LYPLG_BITS2BYTES(value_size_bits);
73 for (i = 9; i < value_size; ++i) {
74 c = ((char *)value)[i];
75 if (!isdigit(c)) {
76 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time character '%c' "
77 "(expected a digit).", c);
78 goto cleanup;
79 }
80 }
81
82 /* store timestamp */
83 memcpy(&val->time, value, sizeof val->time);
84
85 /* store fractions of second */
86 if (value_size > 9) {
87 val->fractions_s = strndup(((char *)value) + 9, value_size - 9);
88 LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
89 }
90
91 /* store unknown timezone */
92 if (value_size > 8) {
93 val->unknown_tz = *(((uint8_t *)value) + 8) ? 1 : 0;
94 }
95
96 /* success */
97 goto cleanup;
98 }
99
100 /* get value byte length */
101 ret = lyplg_type_check_value_size("date-and-time", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES, 0,
102 &value_size, err);
103 LY_CHECK_GOTO(ret, cleanup);
104
105 /* check hints */
106 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
107 LY_CHECK_GOTO(ret, cleanup);
108
109 if (!(options & LYPLG_TYPE_STORE_ONLY)) {
110 /* validate value */
111 ret = lyplg_type_validate_patterns(ctx, ((struct lysc_type_str *)type)->patterns, value, value_size, err);
112 LY_CHECK_RET(ret);
113 }
114
115 /* convert to UNIX time and fractions of second */
116 ret = ly_time_str2time(value, &val->time, &val->fractions_s);
117 if (ret) {
118 ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
119 goto cleanup;
120 }
121
122 if (!strncmp(((char *)value + value_size) - 6, "-00:00", 6) || (!old_rev && (((char *)value)[value_size - 1] == 'Z'))) {
123 /* unknown timezone, timezone format supported for backwards compatibility */
124 val->unknown_tz = 1;
125 }
126
127 if (format == LY_VALUE_CANON) {
128 /* store canonical value */
129 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
130 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
131 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
132 LY_CHECK_GOTO(ret, cleanup);
133 } else {
134 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
135 LY_CHECK_GOTO(ret, cleanup);
136 }
137 }
138
139cleanup:
140 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
141 free((void *)value);
142 }
143
144 if (ret) {
145 lyplg_type_free_date_and_time(ctx, storage);
146 }
147 return ret;
148}
149
150static LY_ERR
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,
153 const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
154{
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);
157}
158
159static LY_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,
162 const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
163{
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);
166}
167
171static LY_ERR
172lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
173 const struct lyd_value *val2)
174{
175 struct lyd_value_date_and_time *v1, *v2;
176
177 LYD_VALUE_GET(val1, v1);
178 LYD_VALUE_GET(val2, v2);
179
180 /* compare timestamp and unknown tz */
181 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
182 return LY_ENOT;
183 }
184
185 /* compare second fractions */
186 if ((!v1->fractions_s && !v2->fractions_s) ||
187 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
188 return LY_SUCCESS;
189 }
190 return LY_ENOT;
191}
192
200static ly_bool
201lyplg_type_fractions_is_zero(char *frac)
202{
203 char *iter;
204
205 if (!frac) {
206 return 1;
207 }
208
209 for (iter = frac; *iter; iter++) {
210 if (*iter != '0') {
211 return 0;
212 }
213 }
214
215 return 1;
216}
217
227static int
228lyplg_type_sort_by_fractions(char *f1, char *f2)
229{
230 ly_bool f1_is_zero, f2_is_zero;
231 int df;
232
233 f1_is_zero = lyplg_type_fractions_is_zero(f1);
234 f2_is_zero = lyplg_type_fractions_is_zero(f2);
235
236 if (f1_is_zero && !f2_is_zero) {
237 return -1;
238 } else if (!f1_is_zero && f2_is_zero) {
239 return 1;
240 } else if (f1_is_zero && f2_is_zero) {
241 return 0;
242 }
243
244 /* both f1 and f2 have some non-zero number */
245 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
246 df = strcmp(f1, f2);
247 if (df > 0) {
248 return 1;
249 } else if (df < 0) {
250 return -1;
251 } else {
252 return 0;
253 }
254}
255
259static int
260lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
261{
262 struct lyd_value_date_and_time *v1, *v2;
263 double dt;
264
265 LYD_VALUE_GET(val1, v1);
266 LYD_VALUE_GET(val2, v2);
267
268 /* compare timestamps */
269 dt = difftime(v1->time, v2->time);
270 if (dt != 0) {
271 return dt;
272 }
273
274 /* compare second fractions */
275 return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s);
276}
277
281static const void *
282lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
283 void *UNUSED(prefix_data), ly_bool old_rev, ly_bool *dynamic, uint64_t *value_size_bits)
284{
285 struct lyd_value_date_and_time *val;
286 struct tm tm;
287 char *ret;
288
289 LYD_VALUE_GET(value, val);
290
291 if (format == LY_VALUE_LYB) {
292 if (val->unknown_tz || val->fractions_s) {
293 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
294 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
295
296 *dynamic = 1;
297 if (value_size_bits) {
298 *value_size_bits = 64 + 8 + (val->fractions_s ? strlen(val->fractions_s) * 8 : 0);
299 }
300 memcpy(ret, &val->time, sizeof val->time);
301 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
302 if (val->fractions_s) {
303 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
304 }
305 } else {
306 *dynamic = 0;
307 if (value_size_bits) {
308 *value_size_bits = 64;
309 }
310 ret = (char *)&val->time;
311 }
312 return ret;
313 }
314
315 /* generate canonical value if not already */
316 if (!value->_canonical) {
317 if (val->unknown_tz) {
318 /* ly_time_time2str but always using GMT */
319 if (!gmtime_r(&val->time, &tm)) {
320 return NULL;
321 }
322
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) {
326 return NULL;
327 }
328 } else {
329 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
330 return NULL;
331 }
332 }
333
334 /* store it */
335 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
336 LOGMEM(ctx);
337 return NULL;
338 }
339 }
340
341 /* use the cached canonical value */
342 if (dynamic) {
343 *dynamic = 0;
344 }
345 if (value_size_bits) {
346 *value_size_bits = strlen(value->_canonical) * 8;
347 }
348 return value->_canonical;
349}
350
351static const void *
352lyplg_type_print_date_and_time_old(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
353 void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
354{
355 return lyplg_type_print_date_and_time(ctx, value, format, prefix_data, 1, dynamic, value_size_bits);
356}
357
358static const void *
359lyplg_type_print_date_and_time_new(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
360 void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
361{
362 return lyplg_type_print_date_and_time(ctx, value, format, prefix_data, 0, dynamic, value_size_bits);
363}
364
368static LY_ERR
369lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
370{
371 LY_ERR ret;
372 struct lyd_value_date_and_time *orig_val, *dup_val;
373
374 memset(dup, 0, sizeof *dup);
375
376 /* optional canonical value */
377 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
378 LY_CHECK_GOTO(ret, error);
379
380 /* allocate value */
381 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
382 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
383
384 LYD_VALUE_GET(original, orig_val);
385
386 /* copy timestamp and unknown tz */
387 dup_val->time = orig_val->time;
388 dup_val->unknown_tz = orig_val->unknown_tz;
389
390 /* duplicate second fractions */
391 if (orig_val->fractions_s) {
392 dup_val->fractions_s = strdup(orig_val->fractions_s);
393 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
394 }
395
396 dup->realtype = original->realtype;
397 return LY_SUCCESS;
398
399error:
400 lyplg_type_free_date_and_time(ctx, dup);
401 return ret;
402}
403
407static void
408lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
409{
410 struct lyd_value_date_and_time *val;
411
412 lydict_remove(ctx, value->_canonical);
413 value->_canonical = NULL;
414 LYD_VALUE_GET(value, val);
415 if (val) {
416 free(val->fractions_s);
418 }
419}
420
429 {
430 .module = "ietf-yang-types",
431 .revision = "2013-07-15",
432 .name = "date-and-time",
433
434 .plugin.id = "ly2 date-and-time",
435 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
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,
444 },
445 {
446 .module = "ietf-yang-types",
447 .revision = "2025-12-22",
448 .name = "date-and-time",
449
450 .plugin.id = "ly2 date-and-time",
451 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
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,
460 },
461 {0}
462};
const struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
libyang dictionary
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:290
@ 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:298
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
LY_DATA_TYPE basetype
Compiled YANG data node.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:614
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:653
const char * _canonical
Definition tree_data.h:611
YANG data representation.
Definition tree_data.h:610
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition tree_data.h:746
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.
Definition utils.h:93
@ LY_VALUE_CANON
Definition utils.h:94
@ LY_VALUE_LYB
Definition utils.h:100
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.