GS1 Barcode Syntax Tests reference
A reference to the AI component "linter" routines referred to by the GS1 Barcode Syntax Dictionary. Copyright (c) 2022-2024 GS1 AISBL.
lint_yyyymmd0.c File Reference

Purpose

The yyyymmd0 linter ensures that the data represents a meaningful date, in YYYYMMDD format, additionally permitting YYYYMM00 format indicating an unspecified day.

Functional Description

◆ gs1_lint_yyyymmd0()

GS1_SYNTAX_DICTIONARY_API gs1_lint_err_t gs1_lint_yyyymmd0 ( const char *const  data,
size_t  data_len,
size_t *const  err_pos,
size_t *const  err_len 
)

Used to ensure that an AI component conforms to the YYYYMMDD or YYYYMM00 formats.

Parameters
[in]dataPointer to the data to be linted. Must not be NULL.
[in]data_lenLength of the data to be linted.
[out]err_posTo facilitate error highlighting, the start position of the bad data is written to this pointer, if not NULL.
[out]err_lenThe length of the bad data is written to this pointer, if not NULL.
Returns
GS1_LINTER_OK if okay.
GS1_LINTER_DATE_TOO_SHORT if the data is too short for YYYYMMDD format.
GS1_LINTER_DATE_TOO_LONG if the data is too long for YYYYMMDD format.
GS1_LINTER_NON_DIGIT_CHARACTER if the data contains a non-digit character.
GS1_LINTER_ILLEGAL_MONTH if the data contains an invalid month.
GS1_LINTER_ILLEGAL_DAY if the data contains an invalid day of the month.
55{
56
57/// \cond
58#define XX(d) ( (data[d] - '0') * 10 + (data[d+1] - '0') )
59#define YY ( XX(0) * 100 + XX(2) )
60#define MM XX(4)
61#define DD XX(6)
62/// \endcond
63
64 static const unsigned char daysinmonth[] =
65 { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
66
67 size_t pos;
68 unsigned char maxdd;
69
70 assert(data);
71
72 /*
73 * Data must be eight characters.
74 *
75 */
76 if (GS1_LINTER_UNLIKELY(data_len != 8))
79 0,
80 data_len
81 );
82
83 /*
84 * Data must consist of all digits.
85 *
86 */
87 for (pos = 0; pos < 8; pos++) {
88 if (GS1_LINTER_UNLIKELY(data[pos] < '0' || data[pos] > '9'))
91 pos,
92 1
93 );
94 }
95
96 /*
97 * Validate that the month is 01 to 12.
98 *
99 */
100 if (GS1_LINTER_UNLIKELY(MM < 1 || MM > 12))
103 4,
104 2
105 );
106
107 /*
108 * Validate the day, accounting for leap years
109 *
110 */
111 if (MM == 2) { /* February; account for leap years */
112 maxdd = ((YY % 4 == 0 && YY % 100 != 0) || YY % 400 == 0) ? 29 : 28;
113 } else {
114 maxdd = daysinmonth[MM - 1]; /* Based at 0 */
115 }
116
117 if (GS1_LINTER_UNLIKELY(DD > maxdd)) /* Permit "00" */
120 6,
121 2
122 );
123
125
126}
#define GS1_LINTER_UNLIKELY(x)
Implementation may provide hint to the compiler that the expression is likely to be false.
Definition gs1syntaxdictionary-utils.h:76
#define GS1_LINTER_RETURN_ERROR(error, position, length)
Return from a linter indicating that a problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:103
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:88
@ GS1_LINTER_ILLEGAL_DAY
The date contains an illegal day of the month.
Definition gs1syntaxdictionary.h:114
@ GS1_LINTER_DATE_TOO_LONG
The date is too long for YYMMDD format.
Definition gs1syntaxdictionary.h:106
@ GS1_LINTER_ILLEGAL_MONTH
The date contains an illegal month of the year.
Definition gs1syntaxdictionary.h:113
@ GS1_LINTER_DATE_TOO_SHORT
The date is too short for YYMMDD format.
Definition gs1syntaxdictionary.h:105
@ GS1_LINTER_NON_DIGIT_CHARACTER
A non-digit character was found where a digit is expected.
Definition gs1syntaxdictionary.h:78