libyang 2.1.128
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 <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
41
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 if (ret) {
116 ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_errmsg());
117 goto cleanup;
118 }
119
120 if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
121 /* unknown timezone */
122 val->unknown_tz = 1;
123 }
124
125 if (format == LY_VALUE_CANON) {
126 /* store canonical value */
127 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
128 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
129 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
130 LY_CHECK_GOTO(ret, cleanup);
131 } else {
132 ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
133 LY_CHECK_GOTO(ret, cleanup);
134 }
135 }
136
137cleanup:
138 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
139 free((void *)value);
140 }
141
142 if (ret) {
143 lyplg_type_free_date_and_time(ctx, storage);
144 }
145 return ret;
146}
147
151static LY_ERR
152lyplg_type_compare_date_and_time(const struct lyd_value *val1, const struct lyd_value *val2)
153{
154 struct lyd_value_date_and_time *v1, *v2;
155
156 if (val1->realtype != val2->realtype) {
157 return LY_ENOT;
158 }
159
160 LYD_VALUE_GET(val1, v1);
161 LYD_VALUE_GET(val2, v2);
162
163 /* compare timestamp and unknown tz */
164 if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
165 return LY_ENOT;
166 }
167
168 /* compare second fractions */
169 if ((!v1->fractions_s && !v2->fractions_s) ||
170 (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
171 return LY_SUCCESS;
172 }
173 return LY_ENOT;
174}
175
179static const void *
180lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
181 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
182{
183 struct lyd_value_date_and_time *val;
184 struct tm tm;
185 char *ret;
186
187 LYD_VALUE_GET(value, val);
188
189 if (format == LY_VALUE_LYB) {
190 if (val->unknown_tz || val->fractions_s) {
191 ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
192 LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
193
194 *dynamic = 1;
195 if (value_len) {
196 *value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
197 }
198 memcpy(ret, &val->time, sizeof val->time);
199 memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
200 if (val->fractions_s) {
201 memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
202 }
203 } else {
204 *dynamic = 0;
205 if (value_len) {
206 *value_len = 8;
207 }
208 ret = (char *)&val->time;
209 }
210 return ret;
211 }
212
213 /* generate canonical value if not already */
214 if (!value->_canonical) {
215 if (val->unknown_tz) {
216 /* ly_time_time2str but always using GMT */
217 if (!gmtime_r(&val->time, &tm)) {
218 return NULL;
219 }
220 if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
221 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
222 val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
223 return NULL;
224 }
225 } else {
226 if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
227 return NULL;
228 }
229 }
230
231 /* store it */
232 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
233 LOGMEM(ctx);
234 return NULL;
235 }
236 }
237
238 /* use the cached canonical value */
239 if (dynamic) {
240 *dynamic = 0;
241 }
242 if (value_len) {
243 *value_len = strlen(value->_canonical);
244 }
245 return value->_canonical;
246}
247
251static LY_ERR
252lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
253{
254 LY_ERR ret;
255 struct lyd_value_date_and_time *orig_val, *dup_val;
256
257 memset(dup, 0, sizeof *dup);
258
259 /* optional canonical value */
260 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
261 LY_CHECK_GOTO(ret, error);
262
263 /* allocate value */
264 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
265 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
266
267 LYD_VALUE_GET(original, orig_val);
268
269 /* copy timestamp and unknown tz */
270 dup_val->time = orig_val->time;
271 dup_val->unknown_tz = orig_val->unknown_tz;
272
273 /* duplicate second fractions */
274 if (orig_val->fractions_s) {
275 dup_val->fractions_s = strdup(orig_val->fractions_s);
276 LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
277 }
278
279 dup->realtype = original->realtype;
280 return LY_SUCCESS;
281
282error:
283 lyplg_type_free_date_and_time(ctx, dup);
284 return ret;
285}
286
290static void
291lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
292{
293 struct lyd_value_date_and_time *val;
294
295 lydict_remove(ctx, value->_canonical);
296 value->_canonical = NULL;
297 LYD_VALUE_GET(value, val);
298 if (val) {
299 free(val->fractions_s);
301 }
302}
303
312 {
313 .module = "ietf-yang-types",
314 .revision = "2013-07-15",
315 .name = "date-and-time",
316
317 .plugin.id = "libyang 2 - date-and-time, version 1",
318 .plugin.store = lyplg_type_store_date_and_time,
319 .plugin.validate = NULL,
320 .plugin.compare = lyplg_type_compare_date_and_time,
321 .plugin.sort = NULL,
322 .plugin.print = lyplg_type_print_date_and_time,
323 .plugin.duplicate = lyplg_type_dup_date_and_time,
324 .plugin.free = lyplg_type_free_date_and_time,
325 .plugin.lyb_data_len = -1,
326 },
327 {0}
328};
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
LIBYANG_API_DECL const char * ly_last_errmsg(void)
Get the last (thread-specific) error message.
@ 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
#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