libyang 5.0.2
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)
124#ifndef ENABLE_DATE_AND_TIME_TYPE_COMPAT
125 || (((char *)value)[value_size - 1] == 'Z')
126#endif
127 ) {
128 /* unknown timezone, timezone format supported for backwards compatibility */
129 val->unknown_tz = 1;
130 }
131
132 if (format == LY_VALUE_CANON) {
133 /* store canonical value */
134 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
135 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
136 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
137 LY_CHECK_GOTO(ret, cleanup);
138 } else {
139 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
140 LY_CHECK_GOTO(ret, cleanup);
141 }
142 }
143
144cleanup:
145 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
146 free((void *)value);
147 }
148
149 if (ret) {
150 lyplg_type_free_date_and_time(ctx, storage);
151 }
152 return ret;
153}
154
158static LY_ERR
159lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
160 const struct lyd_value *val2)
161{
162 struct lyd_value_date_and_time *v1, *v2;
163
164 LYD_VALUE_GET(val1, v1);
165 LYD_VALUE_GET(val2, v2);
166
167 /* compare timestamp and unknown tz */
168 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
169 return LY_ENOT;
170 }
171
172 /* compare second fractions */
173 if ((!v1->fractions_s && !v2->fractions_s) ||
174 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
175 return LY_SUCCESS;
176 }
177 return LY_ENOT;
178}
179
187static ly_bool
188lyplg_type_fractions_is_zero(char *frac)
189{
190 char *iter;
191
192 if (!frac) {
193 return 1;
194 }
195
196 for (iter = frac; *iter; iter++) {
197 if (*iter != '0') {
198 return 0;
199 }
200 }
201
202 return 1;
203}
204
214static int
215lyplg_type_sort_by_fractions(char *f1, char *f2)
216{
217 ly_bool f1_is_zero, f2_is_zero;
218 int df;
219
220 f1_is_zero = lyplg_type_fractions_is_zero(f1);
221 f2_is_zero = lyplg_type_fractions_is_zero(f2);
222
223 if (f1_is_zero && !f2_is_zero) {
224 return -1;
225 } else if (!f1_is_zero && f2_is_zero) {
226 return 1;
227 } else if (f1_is_zero && f2_is_zero) {
228 return 0;
229 }
230
231 /* both f1 and f2 have some non-zero number */
232 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
233 df = strcmp(f1, f2);
234 if (df > 0) {
235 return 1;
236 } else if (df < 0) {
237 return -1;
238 } else {
239 return 0;
240 }
241}
242
246static int
247lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
248{
249 struct lyd_value_date_and_time *v1, *v2;
250 double dt;
251
252 LYD_VALUE_GET(val1, v1);
253 LYD_VALUE_GET(val2, v2);
254
255 /* compare timestamps */
256 dt = difftime(v1->time, v2->time);
257 if (dt != 0) {
258 return dt;
259 }
260
261 /* compare second fractions */
262 return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s);
263}
264
268static const void *
269lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
270 void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
271{
272 struct lyd_value_date_and_time *val;
273 struct tm tm;
274 char *ret;
275 const char *unknown_tz;
276
277 LYD_VALUE_GET(value, val);
278
279 if (format == LY_VALUE_LYB) {
280 if (val->unknown_tz || val->fractions_s) {
281 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
282 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
283
284 *dynamic = 1;
285 if (value_size_bits) {
286 *value_size_bits = 64 + 8 + (val->fractions_s ? strlen(val->fractions_s) * 8 : 0);
287 }
288 memcpy(ret, &val->time, sizeof val->time);
289 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
290 if (val->fractions_s) {
291 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
292 }
293 } else {
294 *dynamic = 0;
295 if (value_size_bits) {
296 *value_size_bits = 64;
297 }
298 ret = (char *)&val->time;
299 }
300 return ret;
301 }
302
303 /* generate canonical value if not already */
304 if (!value->_canonical) {
305 if (val->unknown_tz) {
306 /* ly_time_time2str but always using GMT */
307 if (!gmtime_r(&val->time, &tm)) {
308 return NULL;
309 }
310
311#ifdef ENABLE_DATE_AND_TIME_TYPE_COMPAT
312 unknown_tz = "-00:00";
313#else
314 unknown_tz = "Z";
315#endif
316 if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
317 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
318 val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "", unknown_tz) == -1) {
319 return NULL;
320 }
321 } else {
322 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
323 return NULL;
324 }
325 }
326
327 /* store it */
328 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
329 LOGMEM(ctx);
330 return NULL;
331 }
332 }
333
334 /* use the cached canonical value */
335 if (dynamic) {
336 *dynamic = 0;
337 }
338 if (value_size_bits) {
339 *value_size_bits = strlen(value->_canonical) * 8;
340 }
341 return value->_canonical;
342}
343
347static LY_ERR
348lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
349{
350 LY_ERR ret;
351 struct lyd_value_date_and_time *orig_val, *dup_val;
352
353 memset(dup, 0, sizeof *dup);
354
355 /* optional canonical value */
356 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
357 LY_CHECK_GOTO(ret, error);
358
359 /* allocate value */
360 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
361 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
362
363 LYD_VALUE_GET(original, orig_val);
364
365 /* copy timestamp and unknown tz */
366 dup_val->time = orig_val->time;
367 dup_val->unknown_tz = orig_val->unknown_tz;
368
369 /* duplicate second fractions */
370 if (orig_val->fractions_s) {
371 dup_val->fractions_s = strdup(orig_val->fractions_s);
372 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
373 }
374
375 dup->realtype = original->realtype;
376 return LY_SUCCESS;
377
378error:
379 lyplg_type_free_date_and_time(ctx, dup);
380 return ret;
381}
382
386static void
387lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
388{
389 struct lyd_value_date_and_time *val;
390
391 lydict_remove(ctx, value->_canonical);
392 value->_canonical = NULL;
393 LYD_VALUE_GET(value, val);
394 if (val) {
395 free(val->fractions_s);
397 }
398}
399
408 {
409 .module = "ietf-yang-types",
410 .revision = "2025-12-22",
411 .name = "date-and-time",
412
413 .plugin.id = "ly2 date-and-time",
414 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
415 .plugin.store = lyplg_type_store_date_and_time,
416 .plugin.validate_value = NULL,
417 .plugin.validate_tree = NULL,
418 .plugin.compare = lyplg_type_compare_date_and_time,
419 .plugin.sort = lyplg_type_sort_date_and_time,
420 .plugin.print = lyplg_type_print_date_and_time,
421 .plugin.duplicate = lyplg_type_dup_date_and_time,
422 .plugin.free = lyplg_type_free_date_and_time,
423 },
424 {0}
425};
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:541
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:580
const char * _canonical
Definition tree_data.h:538
YANG data representation.
Definition tree_data.h:537
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition tree_data.h:673
#define LOGMEM(CTX)
Definition tree_edit.h:22