Sitelet https://github.com/angular/angular/pull/38009
Skip to content

fix(common): do not round up fractions of a millisecond in DatePipe - #38009

Closed
ajitsinghkaler wants to merge 1 commit into
angular:masterfrom
ajitsinghkaler:milli-date
Closed

ajitsinghkaler wants to merge 1 commit into
angular:masterfrom
ajitsinghkaler:milli-date

Conversation

@ajitsinghkaler

Copy link
Copy Markdown
Contributor

Date pipe rounds up milliseconds when passed fractional dates.
ECMAScript spec defines that non-integer values passed for any of the parameters to
new Date(year, month, date, hours, minutes, seconds, ms)
should call floor() to remove the fractional part.

BREAKING CHANGE: Some people may rely on the rounding functionality of date pipe
Fixes #37989

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Date pipe rounds fractional part of seconds

Issue Number: #37989

What is the new behavior?

Datepipe floors fractional seconds

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@ajitsinghkaler

Copy link
Copy Markdown
Contributor Author

Test failures seem unrelated

@petebacondarwin petebacondarwin left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for putting this PR together @ajitsinghkaler. Can we tweak the commit message a bit.

  1. Fixes lines should come before BREAKING CHANGE blocks. Otherwise they become part of the breaking change notice.
  2. We should refer to DatePipe rather than date pipe since that is the name of the class.
  3. BREAKING CHANGE notices should describe what has changed, what type of application code could be affected, and, if possible, how to update your code to cope if you are affected.

My suggestion for the commit message would be:

fix(common): do not round up fractions of a millisecond in `DatePipe`

Currently, the `DatePipe` (via `formatDate()` rounds fractions of a millisecond to the
nearest millisecond. This can cause dates that are less than a millisecond before midnight
to be incremented to the following day.

The [ECMAScript specification](https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.11)
defines that `DateTime` milliseconds should always be rounded down, so that `999.9ms`
becomes `999ms`.

This change brings `formatDate()` and so `DatePipe` inline with the ECMAScript
specification.

Fixes #37989

BREAKING CHANGE:

When passing a date-time formatted string to the `DatePipe` in a format that contains
fractions of a millisecond, the milliseconds will now always be rounded down rather than
to the nearest millisecond.

Most applications will not be affected by this change. If this is not the desired behaviour
then consider pre-processing the string to round the millisecond part before passing
it to the `DatePipe`.

Comment thread packages/common/test/pipes/date_pipe_spec.ts Outdated
@petebacondarwin petebacondarwin added area: common Issues related to APIs in the @angular/common package area: i18n Issues related to localization and internationalization breaking changes target: major This PR is targeted for the next major release type: bug/fix labels Jul 11, 2020
@ngbot ngbot Bot modified the milestone: needsTriage Jul 11, 2020
@petebacondarwin petebacondarwin added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jul 11, 2020
Comment thread packages/common/src/i18n/format_date.ts Outdated
@ajitsinghkaler ajitsinghkaler changed the title fix(common): stop date pipe from rounding fix(common): do not round up fractions of a millisecond in DatePipe Jul 12, 2020

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great! Just a tiny typo in the commit message... a missing closing bracket.

Comment thread packages/common/src/i18n/format_date.ts Outdated
@AndrewKushnir AndrewKushnir modified the milestones: needsTriage, v11-candidates Jul 12, 2020
@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit state: blocked labels Jul 13, 2020

@AndrewKushnir AndrewKushnir left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for updating the comment in the code 👍

Since this is a breaking change, I've also started a global presubmit in Google's codebase and will share results as soon as I have them.

FYI, I've also added the "blocked" label for now, since we can merge this PR only when master branch becomes available for the changes related to v11 (since this is a breaking change).

Thank you.

@AndrewKushnir
AndrewKushnir removed the request for review from alxhub July 13, 2020 05:16
@petebacondarwin petebacondarwin removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jul 14, 2020
@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Jul 14, 2020
@AndrewKushnir

Copy link
Copy Markdown
Contributor

FYI, the presubmit went well, so we should be able to land it in v11. We would need to rebase and run another global presubmit before merging (once master becomes available for v11 changes). Thank you.

@petebacondarwin

Copy link
Copy Markdown
Contributor

master is now open for breaking changes (to land in 11.0.0). So @ajitsinghkaler - would you like to rebase and we can see if we can land this?

Currently, the `DatePipe` (via `formatDate()`) rounds fractions of a millisecond to the
nearest millisecond. This can cause dates that are less than a millisecond before midnight
to be incremented to the following day.

The [ECMAScript specification](https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.11)
defines that `DateTime` milliseconds should always be rounded down, so that `999.9ms`
becomes `999ms`.

This change brings `formatDate()` and so `DatePipe` inline with the ECMAScript
specification.

Fixes angular#37989

BREAKING CHANGE:

When passing a date-time formatted string to the `DatePipe` in a format that contains
fractions of a millisecond, the milliseconds will now always be rounded down rather than
to the nearest millisecond.

Most applications will not be affected by this change. If this is not the desired behaviour
then consider pre-processing the string to round the millisecond part before passing
it to the `DatePipe`.
@ajitsinghkaler

Copy link
Copy Markdown
Contributor Author

@petebacondarwin rebased it

@AndrewKushnir

AndrewKushnir commented Sep 9, 2020 •

Copy link
Copy Markdown
Contributor

Presubmit + Global Presubmit.

@AndrewKushnir AndrewKushnir added the action: merge The PR is ready for merge by the caretaker label Sep 9, 2020
@angular-automatic-lock-bot

Copy link
Copy Markdown

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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Oct 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: common Issues related to APIs in the @angular/common package area: i18n Issues related to localization and internationalization breaking changes cla: yes target: major This PR is targeted for the next major release type: bug/fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(common): date pipe rounds up date when fractional second is more than 3 digits

4 participants