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

Parse ambiguous timezones #450

Merged
merged 8 commits into from
Mar 27, 2023
Merged

Conversation

JoakimNil
Copy link
Contributor

Closes #447. I changed my mind, and decided that a PR is probably the best way to focus the discussion 😄

Please see the note in #447 regarding CET. There were some existing tests that expected CET to be 60 minutes, even during DST. I changed those tests to reflect the nuance in interpreting CET. I expect there will be cases where people expect CET to be 60 minutes even during DST, but I firmly believe that it's more common to expect CET to be 120 minutes during DST.

I also made some structural changes: A lot of the timezone logic was duplicated in timezone.ts and ExtractTimezoneAbbrRefiner.ts, so I moved most of it to timezone.ts.

Finally, there's a drive-by commit to export ParsingResult, which is commonly needed by chrono users.

@wanasit
Copy link
Owner

wanasit commented Apr 24, 2022

Hello @JoakimNil! Thanks for your great work! I think your timezone change is pretty nice and sophisticated.

Could you please reply to my code review comments before I merge it?

@JoakimNil
Copy link
Contributor Author

@wanasit thanks for taking the time to follow up this PR. I can't seem to find your comments. Perhaps they are still pending in an unsubmitted review?

src/index.ts Outdated

export { en, Chrono, Parser, Refiner };
export { en, Chrono, Parser, Refiner, TimezoneAbbrMap, ParsingResult };
Copy link
Owner

Choose a reason for hiding this comment

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

If we are going to export TimezoneAbbrMap on the API, could we move the interface definition here and make it easier to use (updating documentation would be nice too)

This would be the API/interface, I recommend:

export interface ParsingOption {
   ...
   timezones?: TimezoneMap;
}

...

export type TimezoneMap = {
    [key: string]: number | TimezoneOffsetWithDstInfo ;
};

export interface TimezoneOffsetWithDstInfo {
    timezoneOffsetDst: number;
    timezoneOffsetNonDst: number;
    dstStart: (year: number) => Date;
    dstEnd: (year: number) => Date;
}

(see my comment about the TimezoneMap and TimezoneOffsetWithDstInfo below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see the newest changes. The changes are mostly according to your suggestion, and there's now better documentation :)

However, I kept the name AmbiguousTimezoneMap because I feel like it better describes the uncertainty associated with parsing these kinds of timezones. Moreover, I feel like "WithDstInfo" may incorrectly give the impression that these timezones are like normal timezone mappings, but with additional information. On the contrary, the use of these mappings reduces the certainty of the parsing due to the DST ambiguity.

@wanasit
Copy link
Owner

wanasit commented Apr 25, 2022

@JoakimNil Ops. Sorry I am not really familiar with GitHub review 😓

Addresses parsing of timezones like ET where it's unclear if DST should be applied or not. Uses the parsed date (fallback to refDate then current date) to determine whether to apply DST.
@JoakimNil JoakimNil force-pushed the ParseAmbiguousTimezones branch from d21b25f to 1b1199c Compare March 22, 2023 20:14
@JoakimNil
Copy link
Contributor Author

@wanasit I finally got around to addressing your suggestions. I think the PR is significantly improved after taking them into account.

@wanasit
Copy link
Owner

wanasit commented Mar 25, 2023

@JoakimNil Thanks for working on this. I added a few additional comments, but it's a significant improvement as you said.

If you don't have time to address the comments, I'll still merge it later this week.

@JoakimNil
Copy link
Contributor Author

@wanasit sure, I have some spare time. I'm not seeing the new comments though?

expect(result.text).toBe(text);
expect(result.start.isCertain("timezoneOffset")).toBe(true);
expect(result.start.get("timezoneOffset")).toBe(60);
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a new test rather than changing the current one?

similar comments for other changes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without changing the test, some of them will fail after the change. This is because they expect a date during DST with CET timezone to have an offset of 60 minutes, while in fact it should be 120 minutes. I refer to my previous notes (here and in #447) for more details regarding CET offsets.

I also changed a few tests that weren't incorrectly formulated and thus didn't strictly need to be changed. However, they all had really unfortunate reference dates that I think made them hard to understand.

The reference date March 28th happens during DST in some years, and outside of DST in other years. So to understand why these tests should result in offsets of 60 and not 120 requires the developer to be extremely conscious of the nuances of how ambiguous timezones are parsed. Testing ambiguous timezone edge cases is not the purpose of these tests. Therefore, I think it's better to keep those tests in en_timezone_exp.test.ts, and instead change these as I have done to make them easier to interpret for their intended purpose.

@wanasit
Copy link
Owner

wanasit commented Mar 26, 2023

Ops. Sorry. I forgot to click send the review again.

@JoakimNil
Copy link
Contributor Author

@wanasit I've now addressed your comments with a few new commits. Also see my reply regarding tests, which I didn't address with any code changes. Would you mind taking a look at the new changes?

@wanasit
Copy link
Owner

wanasit commented Mar 27, 2023

Cool! thanks again for working on this!.

@wanasit wanasit merged commit 48c8965 into wanasit:master Mar 27, 2023
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.

Ambiguous timezone abbreviations are incorrectly interpreted for DST dates
2 participants