-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Conversation
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
325965f
to
e1bdde6
Compare
The 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Parses ISO-8601 date time string. | |
* Parses ISO-8601 extended format date time string. |
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'; |
There was a problem hiding this comment.
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?
}; | ||
const consumeSeparator = (sep: '-' | ':') => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}; | |
const consumeSeparator = (sep: '-' | ':') => { | |
}; | |
const consumeSeparator = (sep: '-' | ':') => { |
}; | ||
const skip = (count = 1) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}; | |
const skip = (count = 1) => { | |
}; | |
const skip = (count = 1) => { |
}; | ||
const peek = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}; | |
const peek = () => { | |
}; | |
const peek = () => { |
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; | ||
} |
There was a problem hiding this comment.
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
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.