Skip to content

Commit

Permalink
Allow null reference timezone; result timezone will not be implied or…
Browse files Browse the repository at this point in the history
… assigned unless one is read from a parsed date
  • Loading branch information
James Addison committed Oct 15, 2021
1 parent 1f38c0b commit d337a53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dayjs.extend(quarterOfYear);

export class ReferenceWithTimezone {
readonly instant: Date;
readonly timezoneOffset: number;
readonly timezoneOffset?: number;

constructor(input?: ParsingReference | Date) {
input = input ?? new Date();
Expand All @@ -17,7 +17,7 @@ export class ReferenceWithTimezone {
this.timezoneOffset = -input.getTimezoneOffset();
} else {
this.instant = input.instant ?? new Date();
this.timezoneOffset = toTimezoneOffset(input.timezone ?? -this.instant.getTimezoneOffset());
this.timezoneOffset = toTimezoneOffset(input.timezone);
}
}
}
Expand Down Expand Up @@ -180,10 +180,14 @@ export class ParsingComponents implements ParsedComponents {
if (fragments["hour"] || fragments["minute"] || fragments["second"]) {
assignSimilarTime(components, date);
assignSimilarDate(components, date);
components.assign("timezoneOffset", -reference.instant.getTimezoneOffset());
if (reference.timezoneOffset !== null) {
components.assign("timezoneOffset", -reference.instant.getTimezoneOffset());
}
} else {
implySimilarTime(components, date);
components.imply("timezoneOffset", -reference.instant.getTimezoneOffset());
if (reference.timezoneOffset !== null) {
components.imply("timezoneOffset", -reference.instant.getTimezoneOffset());
}

if (fragments["d"]) {
components.assign("day", date.date());
Expand Down
4 changes: 4 additions & 0 deletions src/timezone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ export const TIMEZONE_ABBR_MAP = {
};

export function toTimezoneOffset(timezoneInput: string | number): number {
if (timezoneInput === null) {
return null;
}

if (typeof timezoneInput === "number") {
return timezoneInput;
}
Expand Down

0 comments on commit d337a53

Please sign in to comment.