libyang  3.7.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, 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),
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;
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_len < 8) {
69  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %zu "
70  "(expected at least 8).", value_len);
71  goto cleanup;
72  }
73  for (i = 9; i < value_len; ++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_len > 9) {
87  val->fractions_s = strndup(((char *)value) + 9, value_len - 9);
88  LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
89  }
90 
91  /* store unknown timezone */
92  if (value_len > 8) {
93  val->unknown_tz = *(((int8_t *)value) + 8) ? 1 : 0;
94  }
95 
96  /* success */
97  goto cleanup;
98  }
99 
100  /* check hints */
101  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
102  LY_CHECK_GOTO(ret, cleanup);
103 
104  /* convert to UNIX time and fractions of second, function must check for all the possible errors */
105  if (ly_time_str2time(value, &val->time, &val->fractions_s)) {
106  ret = ly_err_new(err, LY_EVALID, 0, NULL, NULL, "%s", ly_last_logmsg());
107  goto cleanup;
108  }
109 
110  if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
111  /* unknown timezone */
112  val->unknown_tz = 1;
113  }
114 
115  if (format == LY_VALUE_CANON) {
116  /* store canonical value */
117  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
118  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
119  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
120  LY_CHECK_GOTO(ret, cleanup);
121  } else {
122  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
123  LY_CHECK_GOTO(ret, cleanup);
124  }
125  }
126 
127 cleanup:
128  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
129  free((void *)value);
130  }
131 
132  if (ret) {
133  lyplg_type_free_date_and_time(ctx, storage);
134  }
135  return ret;
136 }
137 
141 static LY_ERR
142 lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
143  const struct lyd_value *val2)
144 {
145  struct lyd_value_date_and_time *v1, *v2;
146 
147  LYD_VALUE_GET(val1, v1);
148  LYD_VALUE_GET(val2, v2);
149 
150  /* compare timestamp and unknown tz */
151  if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
152  return LY_ENOT;
153  }
154 
155  /* compare second fractions */
156  if ((!v1->fractions_s && !v2->fractions_s) ||
157  (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
158  return LY_SUCCESS;
159  }
160  return LY_ENOT;
161 }
162 
170 static ly_bool
171 lyplg_type_fractions_is_zero(char *frac)
172 {
173  char *iter;
174 
175  if (!frac) {
176  return 1;
177  }
178 
179  for (iter = frac; *iter; iter++) {
180  if (*iter != '0') {
181  return 0;
182  }
183  }
184 
185  return 1;
186 }
187 
197 static int
198 lyplg_type_sort_by_fractions(char *f1, char *f2)
199 {
200  ly_bool f1_is_zero, f2_is_zero;
201  int df;
202 
203  f1_is_zero = lyplg_type_fractions_is_zero(f1);
204  f2_is_zero = lyplg_type_fractions_is_zero(f2);
205 
206  if (f1_is_zero && !f2_is_zero) {
207  return -1;
208  } else if (!f1_is_zero && f2_is_zero) {
209  return 1;
210  } else if (f1_is_zero && f2_is_zero) {
211  return 0;
212  }
213 
214  /* both f1 and f2 have some non-zero number */
215  assert(!f1_is_zero && !f2_is_zero && f1 && f2);
216  df = strcmp(f1, f2);
217  if (df > 0) {
218  return 1;
219  } else if (df < 0) {
220  return -1;
221  } else {
222  return 0;
223  }
224 }
225 
229 static int
230 lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
231 {
232  struct lyd_value_date_and_time *v1, *v2;
233  double dt;
234 
235  LYD_VALUE_GET(val1, v1);
236  LYD_VALUE_GET(val2, v2);
237 
238  /* compare timestamps */
239  dt = difftime(v1->time, v2->time);
240  if (dt != 0) {
241  return dt;
242  }
243 
244  /* compare second fractions */
246 }
247 
251 static const void *
252 lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
253  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
254 {
255  struct lyd_value_date_and_time *val;
256  struct tm tm;
257  char *ret;
258 
259  LYD_VALUE_GET(value, val);
260 
261  if (format == LY_VALUE_LYB) {
262  if (val->unknown_tz || val->fractions_s) {
263  ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
264  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
265 
266  *dynamic = 1;
267  if (value_len) {
268  *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
269  }
270  memcpy(ret, &val->time, sizeof val->time);
271  memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
272  if (val->fractions_s) {
273  memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
274  }
275  } else {
276  *dynamic = 0;
277  if (value_len) {
278  *value_len = 8;
279  }
280  ret = (char *)&val->time;
281  }
282  return ret;
283  }
284 
285  /* generate canonical value if not already */
286  if (!value->_canonical) {
287  if (val->unknown_tz) {
288  /* ly_time_time2str but always using GMT */
289  if (!gmtime_r(&val->time, &tm)) {
290  return NULL;
291  }
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,
294  val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
295  return NULL;
296  }
297  } else {
298  if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
299  return NULL;
300  }
301  }
302 
303  /* store it */
304  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
305  LOGMEM(ctx);
306  return NULL;
307  }
308  }
309 
310  /* use the cached canonical value */
311  if (dynamic) {
312  *dynamic = 0;
313  }
314  if (value_len) {
315  *value_len = strlen(value->_canonical);
316  }
317  return value->_canonical;
318 }
319 
323 static LY_ERR
324 lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
325 {
326  LY_ERR ret;
327  struct lyd_value_date_and_time *orig_val, *dup_val;
328 
329  memset(dup, 0, sizeof *dup);
330 
331  /* optional canonical value */
332  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
333  LY_CHECK_GOTO(ret, error);
334 
335  /* allocate value */
336  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
337  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
338 
339  LYD_VALUE_GET(original, orig_val);
340 
341  /* copy timestamp and unknown tz */
342  dup_val->time = orig_val->time;
343  dup_val->unknown_tz = orig_val->unknown_tz;
344 
345  /* duplicate second fractions */
346  if (orig_val->fractions_s) {
347  dup_val->fractions_s = strdup(orig_val->fractions_s);
348  LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
349  }
350 
351  dup->realtype = original->realtype;
352  return LY_SUCCESS;
353 
354 error:
355  lyplg_type_free_date_and_time(ctx, dup);
356  return ret;
357 }
358 
362 static void
363 lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
364 {
365  struct lyd_value_date_and_time *val;
366 
367  lydict_remove(ctx, value->_canonical);
368  value->_canonical = NULL;
369  LYD_VALUE_GET(value, val);
370  if (val) {
371  free(val->fractions_s);
373  }
374 }
375 
384  {
385  .module = "ietf-yang-types",
386  .revision = "2013-07-15",
387  .name = "date-and-time",
388 
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,
398  },
399  {0}
400 };
struct lysc_type * realtype
Definition: tree_data.h:579
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:1439
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.
Definition: log.h:28
Definition: log.h:242
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition: tree_data.h:711
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.
Definition: tree_data.h:575
const char * _canonical
Definition: tree_data.h:576
Libyang full error structure.
Definition: log.h:285
Definition: log.h:277
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:618
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&#39;s hints (if any) in the specified format.
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.
Definition: log.h:248
#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:1305
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
Definition: log.h:254
assert(!value->_canonical)