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