Loading...
Searching...
No Matches
date_and_time.c
Go to the documentation of this file.
#define _GNU_SOURCE /* strdup */
#include "plugins_types.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libyang.h"
#include "compat.h"
#include "ly_common.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
static LY_ERR
lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_str *type_dat = (struct lysc_type_str *)type;
struct lyd_value_date_and_time *val;
uint32_t i;
char c;
/* init storage */
memset(storage, 0, sizeof *storage);
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
storage->realtype = type;
if (format == LY_VALUE_LYB) {
/* validation */
if (value_len < 8) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %zu "
"(expected at least 8).", value_len);
goto cleanup;
}
for (i = 9; i < value_len; ++i) {
c = ((char *)value)[i];
if (!isdigit(c)) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time character '%c' "
"(expected a digit).", c);
goto cleanup;
}
}
/* store timestamp */
memcpy(&val->time, value, sizeof val->time);
/* store fractions of second */
if (value_len > 9) {
val->fractions_s = strndup(((char *)value) + 9, value_len - 9);
LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
}
/* store unknown timezone */
if (value_len > 8) {
val->unknown_tz = *(((int8_t *)value) + 8) ? 1 : 0;
}
/* success */
goto cleanup;
}
/* check hints */
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
LY_CHECK_GOTO(ret, cleanup);
/* length restriction, there can be only ASCII chars */
if (type_dat->length) {
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_dat->length, value_len, value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
}
/* date-and-time pattern */
ret = lyplg_type_validate_patterns(type_dat->patterns, value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
/* convert to UNIX time and fractions of second */
ret = ly_time_str2time(value, &val->time, &val->fractions_s);
if (ret) {
ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
goto cleanup;
}
if (!strncmp(((char *)value + value_len) - 6, "-00:00", 6)) {
/* unknown timezone */
val->unknown_tz = 1;
}
if (format == LY_VALUE_CANON) {
/* store canonical value */
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
LY_CHECK_GOTO(ret, cleanup);
} else {
ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
LY_CHECK_GOTO(ret, cleanup);
}
}
cleanup:
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
free((void *)value);
}
if (ret) {
lyplg_type_free_date_and_time(ctx, storage);
}
return ret;
}
static LY_ERR
lyplg_type_compare_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
const struct lyd_value *val2)
{
struct lyd_value_date_and_time *v1, *v2;
LYD_VALUE_GET(val1, v1);
LYD_VALUE_GET(val2, v2);
/* compare timestamp and unknown tz */
if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
return LY_ENOT;
}
/* compare second fractions */
if ((!v1->fractions_s && !v2->fractions_s) ||
(v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
return LY_SUCCESS;
}
return LY_ENOT;
}
static ly_bool
lyplg_type_fractions_is_zero(char *frac)
{
char *iter;
if (!frac) {
return 1;
}
for (iter = frac; *iter; iter++) {
if (*iter != '0') {
return 0;
}
}
return 1;
}
static int
lyplg_type_sort_by_fractions(char *f1, char *f2)
{
ly_bool f1_is_zero, f2_is_zero;
int df;
f1_is_zero = lyplg_type_fractions_is_zero(f1);
f2_is_zero = lyplg_type_fractions_is_zero(f2);
if (f1_is_zero && !f2_is_zero) {
return -1;
} else if (!f1_is_zero && f2_is_zero) {
return 1;
} else if (f1_is_zero && f2_is_zero) {
return 0;
}
/* both f1 and f2 have some non-zero number */
assert(!f1_is_zero && !f2_is_zero && f1 && f2);
df = strcmp(f1, f2);
if (df > 0) {
return 1;
} else if (df < 0) {
return -1;
} else {
return 0;
}
}
static int
lyplg_type_sort_date_and_time(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
{
struct lyd_value_date_and_time *v1, *v2;
double dt;
LYD_VALUE_GET(val1, v1);
LYD_VALUE_GET(val2, v2);
/* compare timestamps */
dt = difftime(v1->time, v2->time);
if (dt != 0) {
return dt;
}
/* compare second fractions */
return lyplg_type_sort_by_fractions(v1->fractions_s, v2->fractions_s);
}
static const void *
lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
{
struct lyd_value_date_and_time *val;
struct tm tm;
char *ret;
LYD_VALUE_GET(value, val);
if (format == LY_VALUE_LYB) {
if (val->unknown_tz || val->fractions_s) {
ret = malloc(8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0));
LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
*dynamic = 1;
if (value_len) {
*value_len = 8 + 1 + (val->fractions_s ? strlen(val->fractions_s) : 0);
}
memcpy(ret, &val->time, sizeof val->time);
memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
if (val->fractions_s) {
memcpy(ret + 9, val->fractions_s, strlen(val->fractions_s));
}
} else {
*dynamic = 0;
if (value_len) {
*value_len = 8;
}
ret = (char *)&val->time;
}
return ret;
}
/* generate canonical value if not already */
if (!value->_canonical) {
if (val->unknown_tz) {
/* ly_time_time2str but always using GMT */
if (!gmtime_r(&val->time, &tm)) {
return NULL;
}
if (asprintf(&ret, "%04d-%02d-%02dT%02d:%02d:%02d%s%s-00:00",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
val->fractions_s ? "." : "", val->fractions_s ? val->fractions_s : "") == -1) {
return NULL;
}
} else {
if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
return NULL;
}
}
/* store it */
if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
LOGMEM(ctx);
return NULL;
}
}
/* use the cached canonical value */
if (dynamic) {
*dynamic = 0;
}
if (value_len) {
*value_len = strlen(value->_canonical);
}
return value->_canonical;
}
static LY_ERR
lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
{
LY_ERR ret;
struct lyd_value_date_and_time *orig_val, *dup_val;
memset(dup, 0, sizeof *dup);
/* optional canonical value */
ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
LY_CHECK_GOTO(ret, error);
/* allocate value */
LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
LYD_VALUE_GET(original, orig_val);
/* copy timestamp and unknown tz */
dup_val->time = orig_val->time;
dup_val->unknown_tz = orig_val->unknown_tz;
/* duplicate second fractions */
if (orig_val->fractions_s) {
dup_val->fractions_s = strdup(orig_val->fractions_s);
LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
}
dup->realtype = original->realtype;
return LY_SUCCESS;
error:
lyplg_type_free_date_and_time(ctx, dup);
return ret;
}
static void
lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
{
struct lyd_value_date_and_time *val;
lydict_remove(ctx, value->_canonical);
value->_canonical = NULL;
LYD_VALUE_GET(value, val);
if (val) {
free(val->fractions_s);
LYPLG_TYPE_VAL_INLINE_DESTROY(val);
}
}
const struct lyplg_type_record plugins_date_and_time[] = {
{
.module = "ietf-yang-types",
.revision = "2013-07-15",
.name = "date-and-time",
.plugin.id = "libyang 2 - date-and-time, version 1",
.plugin.store = lyplg_type_store_date_and_time,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_date_and_time,
.plugin.sort = lyplg_type_sort_date_and_time,
.plugin.print = lyplg_type_print_date_and_time,
.plugin.duplicate = lyplg_type_dup_date_and_time,
.plugin.free = lyplg_type_free_date_and_time,
.plugin.lyb_data_len = -1,
},
{0}
};
plugins_date_and_time
const struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
Definition date_and_time.c:395
ly_ctx
libyang context handler.
lydict_insert
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...
lydict_remove
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 ...
lydict_insert_zc
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_last_logmsg
LIBYANG_API_DECL const char * ly_last_logmsg(void)
Get the last (thread-specific) full logged error message.
LY_ERR
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_EMEM
@ LY_EMEM
Definition log.h:239
LY_ENOT
@ LY_ENOT
Definition log.h:251
LY_EVALID
@ LY_EVALID
Definition log.h:245
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
ly_err_item
Libyang full error structure.
Definition log.h:282
lyplg_type_record::revision
const char *const char * revision
Definition plugins_types.h:638
LYPLG_TYPE_VAL_INLINE_PREPARE
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
Definition plugins_types.h:194
lyplg_type_validate_patterns
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.
LYPLG_TYPE_VAL_INLINE_DESTROY
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
Definition plugins_types.h:206
lyplg_type_validate_range
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.
lyplg_type_check_hints
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.
ly_err_new
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.
lyplg_type_record
Definition plugins_types.h:635
LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_DYNAMIC
Definition plugins_types.h:471
lysc_type::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1287
lysc_type_str::patterns
struct lysc_pattern ** patterns
Definition tree_schema.h:1321
lysc_type_str::length
struct lysc_range * length
Definition tree_schema.h:1320
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_str
Definition tree_schema.h:1313
LY_VALUE_FORMAT
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
LY_TYPE_STRING
@ LY_TYPE_STRING
Definition tree.h:209
LY_VALUE_CANON
@ LY_VALUE_CANON
Definition tree.h:235
LY_VALUE_LYB
@ LY_VALUE_LYB
Definition tree.h:240
libyang.h
The main libyang public header.
ly_bool
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
plugins_types.h
API for (user) types plugins.
ly_time_str2time
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.
lyd_value::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_value_date_and_time::unknown_tz
ly_bool unknown_tz
Definition tree_data.h:706
lyd_value_date_and_time::time
time_t time
Definition tree_data.h:704
ly_time_time2str
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.
LYD_VALUE_GET
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:610
lyd_value_date_and_time::fractions_s
char * fractions_s
Definition tree_data.h:705
lyd_value::_canonical
const char * _canonical
Definition tree_data.h:568
lyd_value
YANG data representation.
Definition tree_data.h:567
lyd_value_date_and_time
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition tree_data.h:703
LOGMEM
#define LOGMEM(CTX)
Definition tree_edit.h:22