Skip to content

Commit

Permalink
Avoid split in offset parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Nov 18, 2023
1 parent 7f60f4a commit 66c7ef5
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions std/assembly/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ export class Date {
if (i == timeString.length - 1) {
throw new RangeError(E_INVALIDDATE);
}
let offsetParts = timeString.substring(i+1).split(":");
let offsetHours = i32.parse(offsetParts[0]);
let offsetMinutes = offsetParts.length >= 2 ? i32.parse(offsetParts[1]) : 0;
offsetMs = (offsetHours * 60 + offsetMinutes) * MILLIS_PER_MINUTE;

let posColon = timeString.indexOf(":", i + 1);
if (~posColon) {
let offsetHours = i32.parse(timeString.substring(i + 1, posColon));
let offsetMinutes = i32.parse(timeString.substring(posColon + 1));
offsetMs = (offsetHours * 60 + offsetMinutes) * MILLIS_PER_MINUTE;
} else {
let offsetHours = i32.parse(timeString.substring(i + 1));
offsetMs = offsetHours * MILLIS_PER_HOUR;
}

if (c == 45) offsetMs = -offsetMs; // negative offset
timeString = timeString.substring(0, i);
break;
Expand Down

0 comments on commit 66c7ef5

Please sign in to comment.