libyang 2.1.80
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 <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
42static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
43
47static LY_ERR
48lyplg_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);
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 LY_CHECK_GOTO(ret, cleanup);
116
117 if (!strncmp(((char *)value + value_len) - 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_len, &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 lyd_value *val1, const struct lyd_value *val2)
150{
151 struct lyd_value_date_and_time *v1, *v2;
152
153 if (val1->realtype != val2->realtype) {
154 return LY_ENOT;
155 }
156
157 LYD_VALUE_GET(val1, v1);
158 LYD_VALUE_GET(val2, v2);
159
160 /* compare timestamp and unknown tz */
161 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
162 return LY_ENOT;
163 }
164
165 /* compare second fractions */
166 if ((!v1->fractions_s && !v2->fractions_s) ||
167 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
168 return LY_SUCCESS;
169 }
170 return LY_ENOT;
171}
172
176static const void *
177lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
178 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
179{
180 struct lyd_value_date_and_time *val;
181 struct tm tm;
182 char *ret;
183
184 LYD_VALUE_GET(value, val);
185
186 if (format == LY_VALUE_LYB) {
187 if (val->unknown_tz || val->fractions_s) {
188 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
189 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
190
191 *dynamic = 1;
192 if (value_len) {
193 *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
194 }
195 memcpy(ret, &val->time, sizeof val->time);
196 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
197 if (val->fractions_s) {
198 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
199 }
200 } else {
201 *dynamic = 0;
202 if (value_len) {
203 *value_len = 8;
204 }
205 ret = (char *)&val->time;
206 }
207 return ret;
208 }
209
210 /* generate canonical value if not already */
211 if (!value->_canonical) {
212 if (val->unknown_tz) {
213 /* ly_time_time2str but always using GMT */
214 if (!gmtime_r(&val->time, &tm)) {
215 return NULL;
216 }
217 if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
218 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
219 val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
220 return NULL;
221 }
222 } else {
223 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
224 return NULL;
225 }
226 }
227
228 /* store it */
229 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
230 LOGMEM(ctx);
231 return NULL;
232 }
233 }
234
235 /* use the cached canonical value */
236 if (dynamic) {
237 *dynamic = 0;
238 }
239 if (value_len) {
240 *value_len = strlen(value->_canonical);
241 }
242 return value->_canonical;
243}
244
248static LY_ERR
249lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
250{
251 LY_ERR ret;
252 struct lyd_value_date_and_time *orig_val, *dup_val;
253
254 memset(dup, 0, sizeof *dup);
255
256 /* optional canonical value */
257 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
258 LY_CHECK_GOTO(ret, error);
259
260 /* allocate value */
261 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
262 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
263
264 LYD_VALUE_GET(original, orig_val);
265
266 /* copy timestamp and unknown tz */
267 dup_val->time = orig_val->time;
268 dup_val->unknown_tz = orig_val->unknown_tz;
269
270 /* duplicate second fractions */
271 if (orig_val->fractions_s) {
272 dup_val->fractions_s = strdup(orig_val->fractions_s);
273 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
274 }
275
276 dup->realtype = original->realtype;
277 return LY_SUCCESS;
278
279error:
280 lyplg_type_free_date_and_time(ctx, dup);
281 return ret;
282}
283
287static void
288lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
289{
290 struct lyd_value_date_and_time *val;
291
292 lydict_remove(ctx, value->_canonical);
293 value->_canonical = NULL;
294 LYD_VALUE_GET(value, val);
295 if (val) {
296 free(val->fractions_s);
298 }
299}
300
309 {
310 .module = "ietf-yang-types",
311 .revision = "2013-07-15",
312 .name = "date-and-time",
313
314 .plugin.id = "libyang 2 - date-and-time, version 1",
315 .plugin.store = lyplg_type_store_date_and_time,
316 .plugin.validate = NULL,
317 .plugin.compare = lyplg_type_compare_date_and_time,
318 .plugin.sort = NULL,
319 .plugin.print = lyplg_type_print_date_and_time,
320 .plugin.duplicate = lyplg_type_dup_date_and_time,
321 .plugin.free = lyplg_type_free_date_and_time,
322 .plugin.lyb_data_len = -1,
323 },
324 {0}
325};
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_EMEM
Definition log.h:253
@ LY_ENOT
Definition log.h:265
@ LY_EVALID
Definition log.h:259
@ 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
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: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