libyang  2.1.148
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 <ctype.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 
26 #include "libyang.h"
27 
28 #include "common.h"
29 #include "compat.h"
30 
42 static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
43 
47 static LY_ERR
48 lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
49  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
50  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
51  struct ly_err_item **err)
52 {
53  LY_ERR ret = LY_SUCCESS;
54  struct lysc_type_str *type_dat = (struct lysc_type_str *)type;
55  struct lyd_value_date_and_time *val;
56  uint32_t i;
57  char c;
58 
59  /* init storage */
60  memset(storage, 0, sizeof *storage);
61  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
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_len < 8) {
68  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %zu "
69  "(expected at least 8).", value_len);
70  goto cleanup;
71  }
72  for (i = 9; i < value_len; ++i) {
73  c = ((char *)value)[i];
74  if (!isdigit(c)) {
75  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time character '%c' "
76  "(expected a digit).", c);
77  goto cleanup;
78  }
79  }
80 
81  /* store timestamp */
82  memcpy(&val->time, value, sizeof val->time);
83 
84  /* store fractions of second */
85  if (value_len > 9) {
86  val->fractions_s = strndup(((char *)value) + 9, value_len - 9);
87  LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
88  }
89 
90  /* store unknown timezone */
91  if (value_len > 8) {
92  val->unknown_tz = *(((int8_t *)value) + 8) ? 1 : 0;
93  }
94 
95  /* success */
96  goto cleanup;
97  }
98 
99  /* check hints */
100  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
101  LY_CHECK_GOTO(ret, cleanup);
102 
103  /* length restriction, there can be only ASCII chars */
104  if (type_dat->length) {
105  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_dat->length, value_len, value, value_len, err);
106  LY_CHECK_GOTO(ret, cleanup);
107  }
108 
109  /* date-and-time pattern */
110  ret = lyplg_type_validate_patterns(type_dat->patterns, value, value_len, err);
111  LY_CHECK_GOTO(ret, cleanup);
112 
113  /* convert to UNIX time and fractions of second */
114  ret = ly_time_str2time(value, &val->time, &val->fractions_s);
115  if (ret) {
116  ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_errmsg());
117  goto cleanup;
118  }
119 
120  if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
121  /* unknown timezone */
122  val->unknown_tz = 1;
123  }
124 
125  if (format == LY_VALUE_CANON) {
126  /* store canonical value */
127  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
128  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
129  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
130  LY_CHECK_GOTO(ret, cleanup);
131  } else {
132  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
133  LY_CHECK_GOTO(ret, cleanup);
134  }
135  }
136 
137 cleanup:
138  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
139  free((void *)value);
140  }
141 
142  if (ret) {
143  lyplg_type_free_date_and_time(ctx, storage);
144  }
145  return ret;
146 }
147 
151 static LY_ERR
152 lyplg_type_compare_date_and_time(const struct lyd_value *val1, const struct lyd_value *val2)
153 {
154  struct lyd_value_date_and_time *v1, *v2;
155 
156  LYD_VALUE_GET(val1, v1);
157  LYD_VALUE_GET(val2, v2);
158 
159  /* compare timestamp and unknown tz */
160  if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
161  return LY_ENOT;
162  }
163 
164  /* compare second fractions */
165  if ((!v1->fractions_s && !v2->fractions_s) ||
166  (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
167  return LY_SUCCESS;
168  }
169  return LY_ENOT;
170 }
171 
175 static const void *
176 lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
177  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
178 {
179  struct lyd_value_date_and_time *val;
180  struct tm tm;
181  char *ret;
182 
183  LYD_VALUE_GET(value, val);
184 
185  if (format == LY_VALUE_LYB) {
186  if (val->unknown_tz || val->fractions_s) {
187  ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
188  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
189 
190  *dynamic = 1;
191  if (value_len) {
192  *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
193  }
194  memcpy(ret, &val->time, sizeof val->time);
195  memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
196  if (val->fractions_s) {
197  memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
198  }
199  } else {
200  *dynamic = 0;
201  if (value_len) {
202  *value_len = 8;
203  }
204  ret = (char *)&val->time;
205  }
206  return ret;
207  }
208 
209  /* generate canonical value if not already */
210  if (!value->_canonical) {
211  if (val->unknown_tz) {
212  /* ly_time_time2str but always using GMT */
213  if (!gmtime_r(&val->time, &tm)) {
214  return NULL;
215  }
216  if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
217  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
218  val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
219  return NULL;
220  }
221  } else {
222  if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
223  return NULL;
224  }
225  }
226 
227  /* store it */
228  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
229  LOGMEM(ctx);
230  return NULL;
231  }
232  }
233 
234  /* use the cached canonical value */
235  if (dynamic) {
236  *dynamic = 0;
237  }
238  if (value_len) {
239  *value_len = strlen(value->_canonical);
240  }
241  return value->_canonical;
242 }
243 
247 static LY_ERR
248 lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
249 {
250  LY_ERR ret;
251  struct lyd_value_date_and_time *orig_val, *dup_val;
252 
253  memset(dup, 0, sizeof *dup);
254 
255  /* optional canonical value */
256  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
257  LY_CHECK_GOTO(ret, error);
258 
259  /* allocate value */
260  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
261  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
262 
263  LYD_VALUE_GET(original, orig_val);
264 
265  /* copy timestamp and unknown tz */
266  dup_val->time = orig_val->time;
267  dup_val->unknown_tz = orig_val->unknown_tz;
268 
269  /* duplicate second fractions */
270  if (orig_val->fractions_s) {
271  dup_val->fractions_s = strdup(orig_val->fractions_s);
272  LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
273  }
274 
275  dup->realtype = original->realtype;
276  return LY_SUCCESS;
277 
278 error:
279  lyplg_type_free_date_and_time(ctx, dup);
280  return ret;
281 }
282 
286 static void
287 lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
288 {
289  struct lyd_value_date_and_time *val;
290 
291  lydict_remove(ctx, value->_canonical);
292  value->_canonical = NULL;
293  LYD_VALUE_GET(value, val);
294  if (val) {
295  free(val->fractions_s);
297  }
298 }
299 
308  {
309  .module = "ietf-yang-types",
310  .revision = "2013-07-15",
311  .name = "date-and-time",
312 
313  .plugin.id = "libyang 2 - date-and-time, version 1",
314  .plugin.store = lyplg_type_store_date_and_time,
315  .plugin.validate = NULL,
316  .plugin.compare = lyplg_type_compare_date_and_time,
317  .plugin.sort = NULL,
318  .plugin.print = lyplg_type_print_date_and_time,
319  .plugin.duplicate = lyplg_type_dup_date_and_time,
320  .plugin.free = lyplg_type_free_date_and_time,
321  .plugin.lyb_data_len = -1,
322  },
323  {0}
324 };
struct lysc_type * realtype
Definition: tree_data.h:564
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:1414
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
Definition: log.h:253
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
LIBYANG_API_DECL const char * ly_last_errmsg(void)
Get the last (thread-specific) error message.
#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:696
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1327
struct lysc_range * length
Definition: tree_schema.h:1326
struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
YANG data representation.
Definition: tree_data.h:560
const char * _canonical
Definition: tree_data.h:561
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(struct lysc_pattern **patterns, const char *str, size_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
Libyang full error structure.
Definition: log.h:296
Definition: log.h:288
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:603
Definition: log.h:259
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
Definition: log.h:265
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
#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:1299
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:251
API for (user) types plugins.
libyang context handler.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, size_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.