libyang 4.3.4
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
date_and_time.c
Go to the documentation of this file.
1
14
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
43
44static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
45
49static LY_ERR
50lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_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), const struct lysc_ext_instance *UNUSED(top_ext),
53 struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), 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);
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 %" PRIu32
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 /* convert to UNIX time and fractions of second */
111 ret = ly_time_str2time(value, &val->time, &val->fractions_s);
112 if (ret) {
113 ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
114 goto cleanup;
115 }
116
117 if (!strncmp(((char *)value + value_size) - 6, "-00:00", 6)) {
118 /* unknown timezone */
119 val->unknown_tz = 1;
120 }
121
122 if (format == LY_VALUE_CANON) {
123 /* store canonical value */
124 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
125 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
126 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
127 LY_CHECK_GOTO(ret, cleanup);
128 } else {
129 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
130 LY_CHECK_GOTO(ret, cleanup);
131 }
132 }
133
134cleanup:
135 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
136 free((void *)value);
137 }
138
139 if (ret) {
140 lyplg_type_free_date_and_time(ctx, storage);
141 }
142 return ret;
143}
144
148static LY_ERR
149lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
150 const struct lyd_value *val2)
151{
152 struct lyd_value_date_and_time *v1, *v2;
153
154 LYD_VALUE_GET(val1, v1);
155 LYD_VALUE_GET(val2, v2);
156
157 /* compare timestamp and unknown tz */
158 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
159 return LY_ENOT;
160 }
161
162 /* compare second fractions */
163 if ((!v1->fractions_s && !v2->fractions_s) ||
164 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
165 return LY_SUCCESS;
166 }
167 return LY_ENOT;
168}
169
177static ly_bool
178lyplg_type_fractions_is_zero(char *frac)
179{
180 char *iter;
181
182 if (!frac) {
183 return 1;
184 }
185
186 for (iter = frac; *iter; iter++) {
187 if (*iter != '0') {
188 return 0;
189 }
190 }
191
192 return 1;
193}
194
204static int
205lyplg_type_sort_by_fractions(char *f1, char *f2)
206{
207 ly_bool f1_is_zero, f2_is_zero;
208 int df;
209
210 f1_is_zero = lyplg_type_fractions_is_zero(f1);
211 f2_is_zero = lyplg_type_fractions_is_zero(f2);
212
213 if (f1_is_zero && !f2_is_zero) {
214 return -1;
215 } else if (!f1_is_zero && f2_is_zero) {
216 return 1;
217 } else if (f1_is_zero && f2_is_zero) {
218 return 0;
219 }
220
221 /* both f1 and f2 have some non-zero number */
222 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
223 df = strcmp(f1, f2);
224 if (df > 0) {
225 return 1;
226 } else if (df < 0) {
227 return -1;
228 } else {
229 return 0;
230 }
231}
232
236static int
237lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
238{
239 struct lyd_value_date_and_time *v1, *v2;
240 double dt;
241
242 LYD_VALUE_GET(val1, v1);
243 LYD_VALUE_GET(val2, v2);
244
245 /* compare timestamps */
246 dt = difftime(v1->time, v2->time);
247 if (dt != 0) {
248 return dt;
249 }
250
251 /* compare second fractions */
252 return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s);
253}
254
258static const void *
259lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
260 void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
261{
262 struct lyd_value_date_and_time *val;
263 struct tm tm;
264 char *ret;
265
266 LYD_VALUE_GET(value, val);
267
268 if (format == LY_VALUE_LYB) {
269 if (val->unknown_tz || val->fractions_s) {
270 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
271 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
272
273 *dynamic = 1;
274 if (value_size_bits) {
275 *value_size_bits = 64 + 8 + (val->fractions_s ? strlen(val->fractions_s) * 8 : 0);
276 }
277 memcpy(ret, &val->time, sizeof val->time);
278 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
279 if (val->fractions_s) {
280 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
281 }
282 } else {
283 *dynamic = 0;
284 if (value_size_bits) {
285 *value_size_bits = 64;
286 }
287 ret = (char *)&val->time;
288 }
289 return ret;
290 }
291
292 /* generate canonical value if not already */
293 if (!value->_canonical) {
294 if (val->unknown_tz) {
295 /* ly_time_time2str but always using GMT */
296 if (!gmtime_r(&val->time, &tm)) {
297 return NULL;
298 }
299 if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
300 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
301 val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
302 return NULL;
303 }
304 } else {
305 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
306 return NULL;
307 }
308 }
309
310 /* store it */
311 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
312 LOGMEM(ctx);
313 return NULL;
314 }
315 }
316
317 /* use the cached canonical value */
318 if (dynamic) {
319 *dynamic = 0;
320 }
321 if (value_size_bits) {
322 *value_size_bits = strlen(value->_canonical) * 8;
323 }
324 return value->_canonical;
325}
326
330static LY_ERR
331lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
332{
333 LY_ERR ret;
334 struct lyd_value_date_and_time *orig_val, *dup_val;
335
336 memset(dup, 0, sizeof *dup);
337
338 /* optional canonical value */
339 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
340 LY_CHECK_GOTO(ret, error);
341
342 /* allocate value */
343 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
344 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
345
346 LYD_VALUE_GET(original, orig_val);
347
348 /* copy timestamp and unknown tz */
349 dup_val->time = orig_val->time;
350 dup_val->unknown_tz = orig_val->unknown_tz;
351
352 /* duplicate second fractions */
353 if (orig_val->fractions_s) {
354 dup_val->fractions_s = strdup(orig_val->fractions_s);
355 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
356 }
357
358 dup->realtype = original->realtype;
359 return LY_SUCCESS;
360
361error:
362 lyplg_type_free_date_and_time(ctx, dup);
363 return ret;
364}
365
369static void
370lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
371{
372 struct lyd_value_date_and_time *val;
373
374 lydict_remove(ctx, value->_canonical);
375 value->_canonical = NULL;
376 LYD_VALUE_GET(value, val);
377 if (val) {
378 free(val->fractions_s);
380 }
381}
382
391 {
392 .module = "ietf-yang-types",
393 .revision = "2013-07-15",
394 .name = "date-and-time",
395
396 .plugin.id = "ly2 date-and-time",
397 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
398 .plugin.store = lyplg_type_store_date_and_time,
399 .plugin.validate_value = NULL,
400 .plugin.validate_tree = NULL,
401 .plugin.compare = lyplg_type_compare_date_and_time,
402 .plugin.sort = lyplg_type_sort_date_and_time,
403 .plugin.print = lyplg_type_print_date_and_time,
404 .plugin.duplicate = lyplg_type_dup_date_and_time,
405 .plugin.free = lyplg_type_free_date_and_time,
406 },
407 {0}
408};
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,...
LIBYANG_API_DECL const char * ly_last_logmsg(void)
Get the last (thread-specific) full logged error message.
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
@ LYVE_DATA
Definition log.h:274
@ LY_EMEM
Definition log.h:239
@ LY_ENOT
Definition log.h:251
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
Libyang full error structure.
Definition log.h:282
YANG extension compiled instance.
#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_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's hints (if any) in the specified format.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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.
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.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
@ LYPLG_LYB_SIZE_VARIABLE_BYTES
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ 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:35
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:567
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:606
const char * _canonical
Definition tree_data.h:564
YANG data representation.
Definition tree_data.h:563
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition tree_data.h:700
#define LOGMEM(CTX)
Definition tree_edit.h:22