Skip to content

CFE-3882: Implemented date parser#284

Open
victormlg wants to merge 1 commit into
NorthernTechHQ:masterfrom
victormlg:date-parser
Open

CFE-3882: Implemented date parser#284
victormlg wants to merge 1 commit into
NorthernTechHQ:masterfrom
victormlg:date-parser

Conversation

@victormlg

@victormlg victormlg commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Probably not the most optimal implementation, but works for most usecases. I tried followed the specs from gnu date input format and inspired myself from the behavior of $ date --date="" when in doubt.

Some comments:

  • unsure if the timezone math is correct, but it looks ok to me (I got a bit confused)
  • it only supports UTC and Z as timezones for now
  • @second-until-epoch is not supported yet

@mender-test-bot

Copy link
Copy Markdown

There was an error running your pipeline, see logs for details.

@mender-test-bot

Copy link
Copy Markdown

There was an error running your pipeline, see logs for details.

@mender-test-bot

Copy link
Copy Markdown

There was an error running your pipeline, see logs for details.

@mender-test-bot

Copy link
Copy Markdown

There was an error running your pipeline, see logs for details.

@mender-test-bot

Copy link
Copy Markdown

There was an error running your pipeline, see logs for details.

Comment thread libutils/date_parser.c
olehermanse
olehermanse previously approved these changes Jul 16, 2026
Ticket: CFE-3882
Signed-off-by: Victor Moene <victor.moene@northern.tech>
@mender-test-bot

Copy link
Copy Markdown

There was an error running your pipeline, see logs for details.

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using YACC/Bison grammar?

Comment thread libutils/date_parser.c
{
Seq *tokens = SeqNew(10, free);

DateToken token = {0};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably allocate it on the heap to begin with.

Comment thread libutils/date_parser.c
Comment on lines +463 to +472
static DateToken *NextToken(ParserState *state)
{
if (state->error || state->idx + 1 >= SeqLength(state->tokens))
{
state->idx = SeqLength(state->tokens);
return NULL;
}
return (DateToken *) SeqAt(state->tokens, ++state->idx);

}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could reuse function above?

Suggested change
static DateToken *NextToken(ParserState *state)
{
if (state->error || state->idx + 1 >= SeqLength(state->tokens))
{
state->idx = SeqLength(state->tokens);
return NULL;
}
return (DateToken *) SeqAt(state->tokens, ++state->idx);
}
static inline DateToken *NextToken(ParserState *state)
{
return PeekTokenAhead(state, 1);
}

Comment thread libutils/date_parser.c

}

static int64_t ToYear(int64_t year, size_t ndigit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a comment here to explain what this function is supposed to do.

Comment thread libutils/date_parser.c
/* =========================== */


static bool ParseInt(DateToken *token, int64_t *out)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token should be const?

Suggested change
static bool ParseInt(DateToken *token, int64_t *out)
static bool ParseInt(const DateToken *token, int64_t *out)

Comment thread libutils/date_parser.c

static bool ParseInt(DateToken *token, int64_t *out)
{
assert(token->type == DATE_TOKEN_INTEGER);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(token->type == DATE_TOKEN_INTEGER);
assert(token != NULL);
assert(token->type == DATE_TOKEN_INTEGER);

Comment thread libutils/date_parser.c
}


static DateToken *PeekTokenAhead(ParserState *state, size_t ahead)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these functions should return const DateToken *. You generally don't want to modify the tokens in any way when parsing text

Comment on lines +39 to +40
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) \
&& !defined(__NetBSD__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining why this is here

assert(t == 1783246210);
assert(StringToTimeFrom("10:10 am", &t, &mydate) == 0);
assert(t == 1783246200);
assert(StringToTimeFrom("10:10 p.m.", &t, &mydate) == 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if?

Suggested change
assert(StringToTimeFrom("10:10 p.m.", &t, &mydate) == 0);
assert(StringToTimeFrom("10:10 p.m. am", &t, &mydate) == 0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants