libyang  2.0.194
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
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  struct tm tm;
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  /* length restriction, there can be only ASCII chars */
105  if (type_dat->length) {
106  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_dat->length, value_len, value, value_len, err);
107  LY_CHECK_GOTO(ret, cleanup);
108  }
109 
110  /* date-and-time pattern */
111  ret = lyplg_type_validate_patterns(type_dat->patterns, value, value_len, err);
112  LY_CHECK_GOTO(ret, cleanup);
113 
114  /* pattern validation succeeded, convert to UNIX time and fractions of second */
115  ret = ly_time_str2time(value, &val->time, &val->fractions_s);
116  LY_CHECK_GOTO(ret, cleanup);
117 
118 #ifdef HAVE_TIME_H_TIMEZONE
119  if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
120  /* unknown timezone, move the timestamp to UTC */
121  tzset();
122  val->time += timezone;
123  val->unknown_tz = 1;
124 
125  if (daylight) {
126  /* DST may apply, adjust accordingly */
127  if (!localtime_r(&val->time, &tm)) {
128  ret = ly_err_new(err, LY_ESYS, LYVE_DATA, NULL, NULL, "localtime_r() call failed (%s).", strerror(errno));
129  goto cleanup;
130  } else if (tm.tm_isdst < 0) {
131  ret = ly_err_new(err, LY_EINT, LYVE_DATA, NULL, NULL, "Failed to get DST information.");
132  goto cleanup;
133  }
134  if (tm.tm_isdst) {
135  /* move an hour back */
136  val->time -= 3600;
137  }
138  }
139  }
140 #else
141  (void)tm;
142  val->unknown_tz = 1;
143 #endif
144 
145  if (format == LY_VALUE_CANON) {
146  /* store canonical value */
147  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
148  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
149  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
150  LY_CHECK_GOTO(ret, cleanup);
151  } else {
152  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
153  LY_CHECK_GOTO(ret, cleanup);
154  }
155  }
156 
157 cleanup:
158  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
159  free((void *)value);
160  }
161 
162  if (ret) {
163  lyplg_type_free_date_and_time(ctx, storage);
164  }
165  return ret;
166 }
167 
171 static LY_ERR
172 lyplg_type_compare_date_and_time(const struct lyd_value *val1, const struct lyd_value *val2)
173 {
174  struct lyd_value_date_and_time *v1, *v2;
175 
176  if (val1->realtype != val2->realtype) {
177  return LY_ENOT;
178  }
179 
180  LYD_VALUE_GET(val1, v1);
181  LYD_VALUE_GET(val2, v2);
182 
183  /* compare timestamp and unknown tz */
184  if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
185  return LY_ENOT;
186  }
187 
188  /* compare second fractions */
189  if ((!v1->fractions_s && !v2->fractions_s) ||
190  (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
191  return LY_SUCCESS;
192  }
193  return LY_ENOT;
194 }
195 
199 static const void *
200 lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
201  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
202 {
203  struct lyd_value_date_and_time *val;
204  char *ret;
205 
206  LYD_VALUE_GET(value, val);
207 
208  if (format == LY_VALUE_LYB) {
209  if (val->unknown_tz || val->fractions_s) {
210  ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
211  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
212 
213  *dynamic = 1;
214  if (value_len) {
215  *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
216  }
217  memcpy(ret, &val->time, sizeof val->time);
218  memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
219  if (val->fractions_s) {
220  memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
221  }
222  } else {
223  *dynamic = 0;
224  if (value_len) {
225  *value_len = 8;
226  }
227  ret = (char *)&val->time;
228  }
229  return ret;
230  }
231 
232  /* generate canonical value if not already */
233  if (!value->_canonical) {
234  /* get the canonical value */
235  if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
236  return NULL;
237  }
238 
239  if (val->unknown_tz) {
240  /* date and time is correct, fix only the timezone */
241  strcpy((ret + strlen(ret)) - 6, "-00:00");
242  }
243 
244  /* store it */
245  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
246  LOGMEM(ctx);
247  return NULL;
248  }
249  }
250 
251  /* use the cached canonical value */
252  if (dynamic) {
253  *dynamic = 0;
254  }
255  if (value_len) {
256  *value_len = strlen(value->_canonical);
257  }
258  return value->_canonical;
259 }
260 
264 static LY_ERR
265 lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
266 {
267  LY_ERR ret;
268  struct lyd_value_date_and_time *orig_val, *dup_val;
269 
270  memset(dup, 0, sizeof *dup);
271 
272  /* optional canonical value */
273  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
274  LY_CHECK_GOTO(ret, error);
275 
276  /* allocate value */
277  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
278  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
279 
280  LYD_VALUE_GET(original, orig_val);
281 
282  /* copy timestamp and unknown tz */
283  dup_val->time = orig_val->time;
284  dup_val->unknown_tz = orig_val->unknown_tz;
285 
286  /* duplicate second fractions */
287  if (orig_val->fractions_s) {
288  dup_val->fractions_s = strdup(orig_val->fractions_s);
289  LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
290  }
291 
292  dup->realtype = original->realtype;
293  return LY_SUCCESS;
294 
295 error:
296  lyplg_type_free_date_and_time(ctx, dup);
297  return ret;
298 }
299 
303 static void
304 lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
305 {
306  struct lyd_value_date_and_time *val;
307 
308  lydict_remove(ctx, value->_canonical);
309  value->_canonical = NULL;
310  LYD_VALUE_GET(value, val);
311  if (val) {
312  free(val->fractions_s);
314  }
315 }
316 
324 const struct lyplg_type_record plugins_date_and_time[] = {
325  {
326  .module = "ietf-yang-types",
327  .revision = "2013-07-15",
328  .name = "date-and-time",
329 
330  .plugin.id = "libyang 2 - date-and-time, version 1",
331  .plugin.store = lyplg_type_store_date_and_time,
332  .plugin.validate = NULL,
333  .plugin.compare = lyplg_type_compare_date_and_time,
334  .plugin.sort = NULL,
335  .plugin.print = lyplg_type_print_date_and_time,
336  .plugin.duplicate = lyplg_type_dup_date_and_time,
337  .plugin.free = lyplg_type_free_date_and_time,
338  .plugin.lyb_data_len = -1,
339  },
340  {0}
341 };
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,...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:244
@ LYVE_DATA
Definition: log.h:281
@ LY_ESYS
Definition: log.h:247
@ LY_EMEM
Definition: log.h:246
@ LY_ENOT
Definition: log.h:258
@ LY_EVALID
Definition: log.h:252
@ LY_EINT
Definition: log.h:251
@ LY_SUCCESS
Definition: log.h:245
Libyang full error structure.
Definition: log.h:289
const char * module
#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_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_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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
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.
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's hints (if any) in the specified format.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1536
struct lysc_pattern ** patterns
Definition: tree_schema.h:1564
struct lysc_range * length
Definition: tree_schema.h:1563
Compiled YANG data node.
Definition: tree_schema.h:1650
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
@ LY_TYPE_STRING
Definition: tree.h:210
@ LY_VALUE_CANON
Definition: tree.h:236
@ LY_VALUE_LYB
Definition: tree.h:241
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:27
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: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.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:603
const char * _canonical
Definition: tree_data.h:561
YANG data representation.
Definition: tree_data.h:560
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition: tree_data.h:696
#define LOGMEM(CTX)
Definition: tree_edit.h:22