libyang 3.1.0
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
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
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, 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 lysc_type_str *type_dat = (struct lysc_type_str *)type;
57 struct lyd_value_date_and_time *val;
58 uint32_t i;
59 char c;
60
61 /* init storage */
62 memset(storage, 0, sizeof *storage);
64 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
65 storage->realtype = type;
66
67 if (format == LY_VALUE_LYB) {
68 /* validation */
69 if (value_len < 8) {
70 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %zu "
71 "(expected at least 8).", value_len);
72 goto cleanup;
73 }
74 for (i = 9; i < value_len; ++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_len > 9) {
88 val->fractions_s = strndup(((char *)value) + 9, value_len - 9);
89 LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
90 }
91
92 /* store unknown timezone */
93 if (value_len > 8) {
94 val->unknown_tz = *(((int8_t *)value) + 8) ? 1 : 0;
95 }
96
97 /* success */
98 goto cleanup;
99 }
100
101 /* check hints */
102 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
103 LY_CHECK_GOTO(ret, cleanup);
104
105 /* length restriction, there can be only ASCII chars */
106 if (type_dat->length) {
107 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_dat->length, value_len, value, value_len, err);
108 LY_CHECK_GOTO(ret, cleanup);
109 }
110
111 /* date-and-time pattern */
112 ret = lyplg_type_validate_patterns(type_dat->patterns, value, value_len, err);
113 LY_CHECK_GOTO(ret, cleanup);
114
115 /* convert to UNIX time and fractions of second */
116 ret = ly_time_str2time(value, &val->time, &val->fractions_s);
117 if (ret) {
118 ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
119 goto cleanup;
120 }
121
122 if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
123 /* unknown timezone */
124 val->unknown_tz = 1;
125 }
126
127 if (format == LY_VALUE_CANON) {
128 /* store canonical value */
129 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
130 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
131 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
132 LY_CHECK_GOTO(ret, cleanup);
133 } else {
134 ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
135 LY_CHECK_GOTO(ret, cleanup);
136 }
137 }
138
139cleanup:
140 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
141 free((void *)value);
142 }
143
144 if (ret) {
145 lyplg_type_free_date_and_time(ctx, storage);
146 }
147 return ret;
148}
149
153static LY_ERR
154lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
155 const struct lyd_value *val2)
156{
157 struct lyd_value_date_and_time *v1, *v2;
158
159 LYD_VALUE_GET(val1, v1);
160 LYD_VALUE_GET(val2, v2);
161
162 /* compare timestamp and unknown tz */
163 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
164 return LY_ENOT;
165 }
166
167 /* compare second fractions */
168 if ((!v1->fractions_s && !v2->fractions_s) ||
169 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
170 return LY_SUCCESS;
171 }
172 return LY_ENOT;
173}
174
182static ly_bool
183lyplg_type_fractions_is_zero(char *frac)
184{
185 char *iter;
186
187 if (!frac) {
188 return 1;
189 }
190
191 for (iter = frac; *iter; iter++) {
192 if (*iter != '0') {
193 return 0;
194 }
195 }
196
197 return 1;
198}
199
209static int
210lyplg_type_sort_by_fractions(char *f1, char *f2)
211{
212 ly_bool f1_is_zero, f2_is_zero;
213 int df;
214
215 f1_is_zero = lyplg_type_fractions_is_zero(f1);
216 f2_is_zero = lyplg_type_fractions_is_zero(f2);
217
218 if (f1_is_zero && !f2_is_zero) {
219 return -1;
220 } else if (!f1_is_zero && f2_is_zero) {
221 return 1;
222 } else if (f1_is_zero && f2_is_zero) {
223 return 0;
224 }
225
226 /* both f1 and f2 have some non-zero number */
227 assert(!f1_is_zero && !f2_is_zero && f1 && f2);
228 df = strcmp(f1, f2);
229 if (df > 0) {
230 return 1;
231 } else if (df < 0) {
232 return -1;
233 } else {
234 return 0;
235 }
236}
237
241static int
242lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
243{
244 struct lyd_value_date_and_time *v1, *v2;
245 double dt;
246
247 LYD_VALUE_GET(val1, v1);
248 LYD_VALUE_GET(val2, v2);
249
250 /* compare timestamps */
251 dt = difftime(v1->time, v2->time);
252 if (dt != 0) {
253 return dt;
254 }
255
256 /* compare second fractions */
257 return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s);
258}
259
263static const void *
264lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
265 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
266{
267 struct lyd_value_date_and_time *val;
268 struct tm tm;
269 char *ret;
270
271 LYD_VALUE_GET(value, val);
272
273 if (format == LY_VALUE_LYB) {
274 if (val->unknown_tz || val->fractions_s) {
275 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
276 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
277
278 *dynamic = 1;
279 if (value_len) {
280 *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
281 }
282 memcpy(ret, &val->time, sizeof val->time);
283 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
284 if (val->fractions_s) {
285 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
286 }
287 } else {
288 *dynamic = 0;
289 if (value_len) {
290 *value_len = 8;
291 }
292 ret = (char *)&val->time;
293 }
294 return ret;
295 }
296
297 /* generate canonical value if not already */
298 if (!value->_canonical) {
299 if (val->unknown_tz) {
300 /* ly_time_time2str but always using GMT */
301 if (!gmtime_r(&val->time, &tm)) {
302 return NULL;
303 }
304 if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
305 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
306 val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
307 return NULL;
308 }
309 } else {
310 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
311 return NULL;
312 }
313 }
314
315 /* store it */
316 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
317 LOGMEM(ctx);
318 return NULL;
319 }
320 }
321
322 /* use the cached canonical value */
323 if (dynamic) {
324 *dynamic = 0;
325 }
326 if (value_len) {
327 *value_len = strlen(value->_canonical);
328 }
329 return value->_canonical;
330}
331
335static LY_ERR
336lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
337{
338 LY_ERR ret;
339 struct lyd_value_date_and_time *orig_val, *dup_val;
340
341 memset(dup, 0, sizeof *dup);
342
343 /* optional canonical value */
344 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
345 LY_CHECK_GOTO(ret, error);
346
347 /* allocate value */
348 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
349 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
350
351 LYD_VALUE_GET(original, orig_val);
352
353 /* copy timestamp and unknown tz */
354 dup_val->time = orig_val->time;
355 dup_val->unknown_tz = orig_val->unknown_tz;
356
357 /* duplicate second fractions */
358 if (orig_val->fractions_s) {
359 dup_val->fractions_s = strdup(orig_val->fractions_s);
360 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
361 }
362
363 dup->realtype = original->realtype;
364 return LY_SUCCESS;
365
366error:
367 lyplg_type_free_date_and_time(ctx, dup);
368 return ret;
369}
370
374static void
375lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
376{
377 struct lyd_value_date_and_time *val;
378
379 lydict_remove(ctx, value->_canonical);
380 value->_canonical = NULL;
381 LYD_VALUE_GET(value, val);
382 if (val) {
383 free(val->fractions_s);
385 }
386}
387
396 {
397 .module = "ietf-yang-types",
398 .revision = "2013-07-15",
399 .name = "date-and-time",
400
401 .plugin.id = "libyang 2 - date-and-time, version 1",
402 .plugin.store = lyplg_type_store_date_and_time,
403 .plugin.validate = NULL,
404 .plugin.compare = lyplg_type_compare_date_and_time,
405 .plugin.sort = lyplg_type_sort_date_and_time,
406 .plugin.print = lyplg_type_print_date_and_time,
407 .plugin.duplicate = lyplg_type_dup_date_and_time,
408 .plugin.free = lyplg_type_free_date_and_time,
409 .plugin.lyb_data_len = -1,
410 },
411 {0}
412};
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
const char *const char * revision
#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.
#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.
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_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
struct lysc_pattern ** patterns
struct lysc_range * length
Compiled YANG data node.
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: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:575
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:614
const char * _canonical
Definition tree_data.h:572
YANG data representation.
Definition tree_data.h:571
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition tree_data.h:707
#define LOGMEM(CTX)
Definition tree_edit.h:22