libyang  2.1.55
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  if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
119  /* unknown timezone, move the timestamp to UTC */
120  val->time -= ly_time_tz_offset();
121  val->unknown_tz = 1;
122 
123  /* DST may apply, adjust accordingly */
124  if (!localtime_r(&val->time, &tm)) {
125  ret = ly_err_new(err, LY_ESYS, LYVE_DATA, NULL, NULL, "localtime_r() call failed (%s).", strerror(errno));
126  goto cleanup;
127  } else if (tm.tm_isdst < 0) {
128  ret = ly_err_new(err, LY_EINT, LYVE_DATA, NULL, NULL, "Failed to get DST information.");
129  goto cleanup;
130  }
131  if (tm.tm_isdst) {
132  /* move an hour back */
133  val->time -= 3600;
134  }
135  }
136 
137  if (format == LY_VALUE_CANON) {
138  /* store canonical value */
139  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
140  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
141  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
142  LY_CHECK_GOTO(ret, cleanup);
143  } else {
144  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
145  LY_CHECK_GOTO(ret, cleanup);
146  }
147  }
148 
149 cleanup:
150  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
151  free((void *)value);
152  }
153 
154  if (ret) {
155  lyplg_type_free_date_and_time(ctx, storage);
156  }
157  return ret;
158 }
159 
163 static LY_ERR
164 lyplg_type_compare_date_and_time(const struct lyd_value *val1, const struct lyd_value *val2)
165 {
166  struct lyd_value_date_and_time *v1, *v2;
167 
168  if (val1->realtype != val2->realtype) {
169  return LY_ENOT;
170  }
171 
172  LYD_VALUE_GET(val1, v1);
173  LYD_VALUE_GET(val2, v2);
174 
175  /* compare timestamp and unknown tz */
176  if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
177  return LY_ENOT;
178  }
179 
180  /* compare second fractions */
181  if ((!v1->fractions_s && !v2->fractions_s) ||
182  (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
183  return LY_SUCCESS;
184  }
185  return LY_ENOT;
186 }
187 
191 static const void *
192 lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
193  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
194 {
195  struct lyd_value_date_and_time *val;
196  char *ret;
197 
198  LYD_VALUE_GET(value, val);
199 
200  if (format == LY_VALUE_LYB) {
201  if (val->unknown_tz || val->fractions_s) {
202  ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
203  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
204 
205  *dynamic = 1;
206  if (value_len) {
207  *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
208  }
209  memcpy(ret, &val->time, sizeof val->time);
210  memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
211  if (val->fractions_s) {
212  memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
213  }
214  } else {
215  *dynamic = 0;
216  if (value_len) {
217  *value_len = 8;
218  }
219  ret = (char *)&val->time;
220  }
221  return ret;
222  }
223 
224  /* generate canonical value if not already */
225  if (!value->_canonical) {
226  /* get the canonical value */
227  if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
228  return NULL;
229  }
230 
231  if (val->unknown_tz) {
232  /* date and time is correct, fix only the timezone */
233  strcpy((ret + strlen(ret)) - 6, "-00:00");
234  }
235 
236  /* store it */
237  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
238  LOGMEM(ctx);
239  return NULL;
240  }
241  }
242 
243  /* use the cached canonical value */
244  if (dynamic) {
245  *dynamic = 0;
246  }
247  if (value_len) {
248  *value_len = strlen(value->_canonical);
249  }
250  return value->_canonical;
251 }
252 
256 static LY_ERR
257 lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
258 {
259  LY_ERR ret;
260  struct lyd_value_date_and_time *orig_val, *dup_val;
261 
262  memset(dup, 0, sizeof *dup);
263 
264  /* optional canonical value */
265  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
266  LY_CHECK_GOTO(ret, error);
267 
268  /* allocate value */
269  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
270  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
271 
272  LYD_VALUE_GET(original, orig_val);
273 
274  /* copy timestamp and unknown tz */
275  dup_val->time = orig_val->time;
276  dup_val->unknown_tz = orig_val->unknown_tz;
277 
278  /* duplicate second fractions */
279  if (orig_val->fractions_s) {
280  dup_val->fractions_s = strdup(orig_val->fractions_s);
281  LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
282  }
283 
284  dup->realtype = original->realtype;
285  return LY_SUCCESS;
286 
287 error:
288  lyplg_type_free_date_and_time(ctx, dup);
289  return ret;
290 }
291 
295 static void
296 lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
297 {
298  struct lyd_value_date_and_time *val;
299 
300  lydict_remove(ctx, value->_canonical);
301  value->_canonical = NULL;
302  LYD_VALUE_GET(value, val);
303  if (val) {
304  free(val->fractions_s);
306  }
307 }
308 
316 const struct lyplg_type_record plugins_date_and_time[] = {
317  {
318  .module = "ietf-yang-types",
319  .revision = "2013-07-15",
320  .name = "date-and-time",
321 
322  .plugin.id = "libyang 2 - date-and-time, version 1",
323  .plugin.store = lyplg_type_store_date_and_time,
324  .plugin.validate = NULL,
325  .plugin.compare = lyplg_type_compare_date_and_time,
326  .plugin.sort = NULL,
327  .plugin.print = lyplg_type_print_date_and_time,
328  .plugin.duplicate = lyplg_type_dup_date_and_time,
329  .plugin.free = lyplg_type_free_date_and_time,
330  .plugin.lyb_data_len = -1,
331  },
332  {0}
333 };
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:251
@ LYVE_DATA
Definition: log.h:288
@ LY_ESYS
Definition: log.h:254
@ LY_EMEM
Definition: log.h:253
@ LY_ENOT
Definition: log.h:265
@ LY_EVALID
Definition: log.h:259
@ LY_EINT
Definition: log.h:258
@ LY_SUCCESS
Definition: log.h:252
Libyang full error structure.
Definition: log.h:296
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:1283
struct lysc_pattern ** patterns
Definition: tree_schema.h:1311
struct lysc_range * length
Definition: tree_schema.h:1310
Compiled YANG data node.
Definition: tree_schema.h:1398
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
@ LY_TYPE_STRING
Definition: tree.h:209
@ 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:28
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.
LIBYANG_API_DECL int ly_time_tz_offset(void)
Get current timezone UTC (GMT) time offset in seconds.
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