Skip to content

ISO week-numbering year incorrect for some years #53813

Closed

Description

@chucknelson

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

No

Description

When using the ISO week-numbering year format in a date pipe (e.g., 'YYYY' format string), for some years it seems to calculate incorrectly versus other common date libraries and other external sources like various calculators on the web.

Below is a quick summary looking at the calendar year and ISO week-numbering years as calculated by different implementations for December 31st (one of the days where the ISO week-number year often differs from the calendar year). The yellow rows are examples of years where the date pipe seems to be incorrect.

image

This was sanity checked against independent sources on the web, such as:

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-tdkfa6?file=src%2Fmain.ts

Note that the reproduction uses the most current versions of packages at this time, including Angular 17.

Please provide the exception or error you saw

See description for examples of the issue.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.2.10
Node: 16.20.2
Package Manager: yarn 1.22.19
OS: darwin arm64

Angular: 15.2.10
... animations, cli, common, compiler, compiler-cli, core
... elements, forms, localize, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1502.10
@angular-devkit/build-angular   15.2.10
@angular-devkit/core            15.2.10
@angular-devkit/schematics      13.3.11
@schematics/angular             15.2.10
rxjs                            7.8.1
typescript                      4.9.5

Anything else?

Versions of other date libraries:

As noted above, the reproduction in StackBlitz uses the most current versions of packages at this time, including Angular 17.

Activity

  1. added
    area: common
    on Jan 6, 2024
  2. modified the milestone: needsTriage on Jan 6, 2024
  3. chucknelson commented on Jan 6, 2024

    @chucknelson
    Author

    FYI added a link to a reproduction in StackBlitz - just the simple code that generates the table in the description screenshot.

  4. JeanMeche commented on Jan 7, 2024

    @JeanMeche
    Member

    Quick investigation, this issue happens every year where Dec 31st is a sunday.
    Looks like there is a bug in our algorithm.

  5. JeanMeche commented on Jan 7, 2024

    @JeanMeche
    Member

    It looks like to boil down to: What do we consider the start of the week ?

    Should it be sunday like in the US ? Or should it be monday like defined by ISO 8106.
    Angular currently implements the former.

    Repro : https://stackblitz.com/edit/angular-zcyztw?file=src%2Fmain.ts

  6. JeanMeche commented on Jan 7, 2024

    @JeanMeche
    Member

    Also I wanted to add about this topic:

    I believe the definition of the week numbering year / end of week is unclear in the framework : Is it ISO based or Locale based ?

    In the case of date-fns, it has 2 functions getISOWeek() and getWeek(). The Former is ISO based while the latter is Locale based. luxon has the same distinctions : kkkk is the ISO week year, iiii is the locale week year.

    In both cases Angular has a bug, the concept of week is neither locale based (the dedicated function doesn't rely on a locale or the dedicated function we have getLocaleFirstDayOfWeek) nor ISO based (is assumes the week starts on Sunday).

  7. chucknelson commented on Jan 8, 2024

    @chucknelson
    Author

    Thanks @JeanMeche for looking into this.

    Good catch on the "local" vs "ISO" week year - I didn't even notice local options in formatting and assumed week years was entirely from an ISO perspective.

    • Using the "local week numbering year" in both date-fns and luxon matches what DatePipe is doing if I use the US locale (which is where I am) - which makes sense considering you're saying Angular is using the US-type definition of "start of week = Sunday".
    • Using other locales like French (👍), though, matches the ISO week year as expected, since the start of the week is Monday.

    Not that I have much say in how you all proceed, but I guess it seems like Angular is all about honoring the locale of the browser/system, so maybe defaulting to "local" week behavior for day of week, week number and week-numbering year makes the most sense?

    I guess it would make sense to have formatting string options for both the "local" and "ISO" week items also?

  8. chucknelson commented on Jan 8, 2024

    @chucknelson
    Author

    Maybe I spoke to soon - it looks like there is still a discrepancy in how date-fns and luxon handle local week years versus DatePipe.

    Added the local week year's to the repro link, and right in 2020 you can see they differ from the DatePipe...but I was thinking since Angular was assuming Sunday as the start of week, they'd match. 🤔

    image

    Also discovered another online tool that works well for ad-hoc comparison of the "local" vs "ISO" week numbers and years: https://www.calendar-12.com/week_number. Basically let's you see the "north american" version where the week starts on Sunday, vs the "ISO"/everyone else version where the week starts on Monday.

  9. JeanMeche commented on Jan 8, 2024

    @JeanMeche
    Member

    Supposing you're in the US, you locale is en-US.

    For this locale, if at least 1 day of the week is the next year, the week is from next year (new Intl.Locale('en-US').weekInfo.minimalDays on Chrome browsers, or check the CLDR).

    So for locale week year this is correct (and anguar is wrong by mixin ISO (thursday bases the weekyear) and week starting on Sunday).

  10. self-assigned this
    on Jan 10, 2024
  11. chucknelson commented on Jan 14, 2024

    @chucknelson
    Author

    @JeanMeche Thanks again. So it sounds like you have all the info you need to adjust how Angular is calculating ISO weeks and week years?

  12. JeanMeche commented on Jan 14, 2024

    @JeanMeche
    Member

    Yes, I have a pending PR at #53879, we'll test it against Google's codebase to see how breaking the fix is consideredd !

  13. angular-automatic-lock-bot commented on Mar 13, 2024

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: commonbugcore: pipes

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions