libyang  5.8.5
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libyang.h"
28 
29 #include "compat.h"
30 #include "ly_common.h"
31 #include "plugins_internal.h" /* LY_TYPE_*_STR */
32 
44 static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
45 
49 static LY_ERR
50 lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_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  ly_bool old_rev, 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);
62  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
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 %" PRIu64
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) || (!old_rev && (((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 
140 cleanup:
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 
151 static LY_ERR
152 lyplg_type_store_date_and_time_old(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
153  uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
154  const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
155 {
156  return lyplg_type_store_date_and_time(ctx, type, value, value_size_bits, options, format, prefix_data, hints,
157  ctx_node, storage, unres, 1, err);
158 }
159 
160 static LY_ERR
161 lyplg_type_store_date_and_time_new(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
162  uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
163  const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
164 {
165  return lyplg_type_store_date_and_time(ctx, type, value, value_size_bits, options, format, prefix_data, hints,
166  ctx_node, storage, unres, 0, err);
167 }
168 
172 static LY_ERR
173 lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
174  const struct lyd_value *val2)
175 {
176  struct lyd_value_date_and_time *v1, *v2;
177 
178  LYD_VALUE_GET(val1, v1);
179  LYD_VALUE_GET(val2, v2);
180 
181  /* compare timestamp and unknown tz */
182  if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
183  return LY_ENOT;
184  }
185 
186  /* compare second fractions */
187  if ((!v1->fractions_s && !v2->fractions_s) ||
188  (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
189  return LY_SUCCESS;
190  }
191  return LY_ENOT;
192 }
193 
201 static ly_bool
202 lyplg_type_fractions_is_zero(char *frac)
203 {
204  char *iter;
205 
206  if (!frac) {
207  return 1;
208  }
209 
210  for (iter = frac; *iter; iter++) {
211  if (*iter != '0') {
212  return 0;
213  }
214  }
215 
216  return 1;
217 }
218 
228 static int
229 lyplg_type_sort_by_fractions(char *f1, char *f2)
230 {
231  ly_bool f1_is_zero, f2_is_zero;
232  int df;
233 
234  f1_is_zero = lyplg_type_fractions_is_zero(f1);
235  f2_is_zero = lyplg_type_fractions_is_zero(f2);
236 
237  if (f1_is_zero && !f2_is_zero) {
238  return -1;
239  } else if (!f1_is_zero && f2_is_zero) {
240  return 1;
241  } else if (f1_is_zero && f2_is_zero) {
242  return 0;
243  }
244 
245  /* both f1 and f2 have some non-zero number */
246  assert(!f1_is_zero && !f2_is_zero && f1 && f2);
247  df = strcmp(f1, f2);
248  if (df > 0) {
249  return 1;
250  } else if (df < 0) {
251  return -1;
252  } else {
253  return 0;
254  }
255 }
256 
260 static int
261 lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
262 {
263  struct lyd_value_date_and_time *v1, *v2;
264  double dt;
265 
266  LYD_VALUE_GET(val1, v1);
267  LYD_VALUE_GET(val2, v2);
268 
269  /* compare timestamps */
270  dt = difftime(v1->time, v2->time);
271  if (dt != 0) {
272  return dt;
273  }
274 
275  /* compare second fractions */
277 }
278 
282 static const void *
283 lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
284  void *UNUSED(prefix_data), ly_bool old_rev, ly_bool *dynamic, uint64_t *value_size_bits)
285 {
286  struct lyd_value_date_and_time *val;
287  struct tm tm;
288  char *ret;
289 
290  LYD_VALUE_GET(value, val);
291 
292  if (format == LY_VALUE_LYB) {
293  if (val->unknown_tz || val->fractions_s) {
294  ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
295  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
296 
297  *dynamic = 1;
298  if (value_size_bits) {
299  *value_size_bits = 64 + 8 + (val->fractions_s ? strlen(val->fractions_s) * 8 : 0);
300  }
301  memcpy(ret, &val->time, sizeof val->time);
302  memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
303  if (val->fractions_s) {
304  memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
305  }
306  } else {
307  *dynamic = 0;
308  if (value_size_bits) {
309  *value_size_bits = 64;
310  }
311  ret = (char *)&val->time;
312  }
313  return ret;
314  }
315 
316  /* generate canonical value if not already */
317  if (!value->_canonical) {
318  if (val->unknown_tz) {
319  /* ly_time_time2str but always using GMT */
320  if (!gmtime_r(&val->time, &tm)) {
321  return NULL;
322  }
323 
324  if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
325  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
326  val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "", old_rev ? "-00:00" : "Z") == -1) {
327  return NULL;
328  }
329  } else {
330  if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
331  return NULL;
332  }
333  }
334 
335  /* store it */
336  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
337  LOGMEM(ctx);
338  return NULL;
339  }
340  }
341 
342  /* use the cached canonical value */
343  if (dynamic) {
344  *dynamic = 0;
345  }
346  if (value_size_bits) {
347  *value_size_bits = strlen(value->_canonical) * 8;
348  }
349  return value->_canonical;
350 }
351 
352 static const void *
353 lyplg_type_print_date_and_time_old(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
354  void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
355 {
356  return lyplg_type_print_date_and_time(ctx, value, format, prefix_data, 1, dynamic, value_size_bits);
357 }
358 
359 static const void *
360 lyplg_type_print_date_and_time_new(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
361  void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
362 {
363  return lyplg_type_print_date_and_time(ctx, value, format, prefix_data, 0, dynamic, value_size_bits);
364 }
365 
369 static LY_ERR
370 lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
371 {
372  LY_ERR ret;
373  struct lyd_value_date_and_time *orig_val, *dup_val;
374 
375  memset(dup, 0, sizeof *dup);
376 
377  /* optional canonical value */
378  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
379  LY_CHECK_GOTO(ret, error);
380 
381  /* allocate value */
382  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
383  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
384 
385  LYD_VALUE_GET(original, orig_val);
386 
387  /* copy timestamp and unknown tz */
388  dup_val->time = orig_val->time;
389  dup_val->unknown_tz = orig_val->unknown_tz;
390 
391  /* duplicate second fractions */
392  if (orig_val->fractions_s) {
393  dup_val->fractions_s = strdup(orig_val->fractions_s);
394  LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
395  }
396 
397  dup->realtype = original->realtype;
398  return LY_SUCCESS;
399 
400 error:
401  lyplg_type_free_date_and_time(ctx, dup);
402  return ret;
403 }
404 
408 static void
409 lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
410 {
411  struct lyd_value_date_and_time *val;
412 
413  lydict_remove(ctx, value->_canonical);
414  value->_canonical = NULL;
415  LYD_VALUE_GET(value, val);
416  if (val) {
417  free(val->fractions_s);
419  }
420 }
421 
430  {
431  .module = "ietf-yang-types",
432  .revision = "2013-07-15",
433  .name = "date-and-time",
434 
435  .plugin.id = "ly2 date-and-time",
436  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
437  .plugin.store = lyplg_type_store_date_and_time_old,
438  .plugin.validate_value = NULL,
439  .plugin.validate_tree = NULL,
440  .plugin.compare = lyplg_type_compare_date_and_time,
441  .plugin.sort = lyplg_type_sort_date_and_time,
442  .plugin.print = lyplg_type_print_date_and_time_old,
443  .plugin.duplicate = lyplg_type_dup_date_and_time,
444  .plugin.free = lyplg_type_free_date_and_time,
445  },
446  {
447  .module = "ietf-yang-types",
448  .revision = "2025-12-22",
449  .name = "date-and-time",
450 
451  .plugin.id = "ly2 date-and-time",
452  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
453  .plugin.store = lyplg_type_store_date_and_time_new,
454  .plugin.validate_value = NULL,
455  .plugin.validate_tree = NULL,
456  .plugin.compare = lyplg_type_compare_date_and_time,
457  .plugin.sort = lyplg_type_sort_date_and_time,
458  .plugin.print = lyplg_type_print_date_and_time_new,
459  .plugin.duplicate = lyplg_type_dup_date_and_time,
460  .plugin.free = lyplg_type_free_date_and_time,
461  },
462  {0}
463 };
return difftime(vn1->time, vn2->time)
struct lysc_type * realtype
Definition: tree_data.h:551
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...
Compiled YANG data node.
Definition: tree_schema.h:1430
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s)
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...
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:255
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
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.
Definition: log.h:269
#define LYPLG_TYPE_STORE_DYNAMIC
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 LOGMEM(CTX)
Definition: tree_edit.h:22
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition: tree_data.h:683
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:257
The main libyang public header.
struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
YANG data representation.
Definition: tree_data.h:547
const char * _canonical
Definition: tree_data.h:548
Libyang full error structure.
Definition: log.h:300
Definition: log.h:292
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:590
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.
Definition: log.h:263
const char * module
double dt
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.
Definition: tree.h:234
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.
LY_DATA_TYPE basetype
Definition: tree_schema.h:1296
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&#39;s hints (if any) in the specified format.
API for (user) types plugins.
libyang context handler.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
#define LYPLG_TYPE_STORE_ONLY
assert(!value->_canonical)