Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing iso-8601 dates #215

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Add support for parsing iso-8601 dates #215

wants to merge 8 commits into from

Conversation

ritave
Copy link
Member

@ritave ritave commented Oct 14, 2024

SIP-28: Background Events requires support for ISO-8601 date and times with timezones.

I found no viable library to support our use-case and thus implemented our own.

Copy link

socket-security bot commented Oct 14, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@date-fns/[email protected] None 0 66.3 kB kossnocorp

View full report↗︎

@ritave ritave marked this pull request as ready for review October 15, 2024 12:51
@ritave ritave requested a review from a team as a code owner October 15, 2024 12:51
@legobeat legobeat changed the title Added support for parsing iso-8601 dates Add support for parsing iso-8601 dates Oct 15, 2024
@legobeat
Copy link
Contributor

legobeat commented Oct 15, 2024

The TZDate returned in API here is implementation-specific for and requires @date-fns/tz to use, I believe?

Maybe this is fine for our purposes at the moment but did you also consider the new Temporal API? I feel that would make it more future-proof and reduce reliance on any single implementation.

Libraries:

export const InvalidIso8601Date = new Error('Invalid ISO-8601 date');

/**
* Parses ISO-8601 date time string.

Choose a reason for hiding this comment

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

Suggested change
* Parses ISO-8601 date time string.
* Parses ISO-8601 extended format date time string.

Comment on lines +225 to +232
assert(year !== undefined, 'Invalid ISO-8601 parser state');
month = month ?? '01';
date = date ?? '01';
hours = hours ?? '00';
minutes = minutes ?? '00';
seconds = seconds ?? '00';
offsetHours = offsetHours ?? '00';
offsetMinutes = offsetMinutes ?? '00';

Choose a reason for hiding this comment

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

Mind leaving a comment about how this is accounting for partial dates as that is something that ISO-8601 allows?

Comment on lines +57 to +58
};
const consumeSeparator = (sep: '-' | ':') => {

Choose a reason for hiding this comment

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

Suggested change
};
const consumeSeparator = (sep: '-' | ':') => {
};
const consumeSeparator = (sep: '-' | ':') => {

Comment on lines +53 to +54
};
const skip = (count = 1) => {

Choose a reason for hiding this comment

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

Suggested change
};
const skip = (count = 1) => {
};
const skip = (count = 1) => {

Comment on lines +47 to +48
};
const peek = () => {

Choose a reason for hiding this comment

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

Suggested change
};
const peek = () => {
};
const peek = () => {

Comment on lines +179 to +191
case ParseDateState.Timezone:
timezone = consume();

if (timezone === 'Z') {
if (peek() !== END) {
throw InvalidIso8601Date;
}
state = ParseDateState.End;
} else if (['-', '+'].includes(timezone)) {
state = ParseDateState.TimezoneHour;
} else {
throw InvalidIso8601Date;
}

Choose a reason for hiding this comment

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

This logic should probably be updated in accordance with MetaMask/SIPs#152

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.

3 participants