-
Notifications
You must be signed in to change notification settings - Fork 43
Add typings for JSDate #484
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
Open
nex3
wants to merge
4
commits into
dart-lang:main
Choose a base branch
from
nex3:date
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: package:js_interop | ||
| permissions: read-all | ||
|
|
||
| on: | ||
| # Run CI on pushes to the main branch and on PRs. | ||
| push: | ||
| branches: [ main ] | ||
| paths: | ||
| - '.github/workflows/js_interop.yaml' | ||
| - 'js_interop/**' | ||
| pull_request: | ||
| paths: | ||
| - '.github/workflows/js_interop.yaml' | ||
| - 'js_interop/**' | ||
| schedule: | ||
| - cron: "0 0 * * 0" | ||
|
|
||
| defaults: | ||
| run: | ||
| working-directory: js_interop/ | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| sdk: [3.9, beta, dev] | ||
| test_config: ['-p chrome', '-p chrome -c dart2wasm', '-p node'] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 | ||
| - uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c | ||
| with: | ||
| sdk: ${{ matrix.sdk }} | ||
|
|
||
| - run: dart pub get | ||
| - run: dart format --output=none --set-exit-if-changed . | ||
| if: ${{ matrix.sdk == 'dev' }} | ||
| - run: dart analyze --fatal-infos | ||
| - run: dart test ${{ matrix.test_config }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| platforms: [chrome, node] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import '../date.dart'; | ||
|
|
||
| extension DateTimeToJSDate on DateTime { | ||
| /// Converts this to a [JSDate] that represents the same instant in time. | ||
| JSDate get toJS => JSDate.fromMillisecondsSinceEpoch(millisecondsSinceEpoch); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ extension type JSDate._(JSObject _) implements JSObject { | |
| /// The [Date constructor] that returns the current date and time. | ||
| /// | ||
| /// [Date constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date | ||
| external JSDate.now(); | ||
| external JSDate.nowAsDate(); | ||
|
|
||
| /// The [Date constructor] with the number of milliseconds since the epoch of | ||
| /// January 1, 1970, UTC. | ||
|
|
@@ -24,20 +24,83 @@ extension type JSDate._(JSObject _) implements JSObject { | |
| /// | ||
| /// [Date constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date | ||
| /// [from a string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#date_string | ||
| external JSDate.parse(String dateString); | ||
| external JSDate.parseAsDate(String dateString); | ||
|
|
||
| /// The [Date constructor] that copies its value [from an existing Date]. | ||
| /// | ||
| /// [Date constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date | ||
| /// [from an existing Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#date_object | ||
| external JSDate.from(JSDate other); | ||
|
|
||
| /// The [Date constructor] that uses [individual component integers]. | ||
| /// The [Date constructor] that uses [individual component integers], | ||
| /// interpreted in the local time zone. | ||
| /// | ||
| /// [Date constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date | ||
| /// [individual component integers]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#individual_date_and_time_component_values | ||
| external JSDate(int year, int month, | ||
| [int? day, int? hours, int? minutes, int? seconds, int? milliseconds]); | ||
| external JSDate.localDate( | ||
| int year, | ||
| int month, [ | ||
| int? day, | ||
| int? hours, | ||
| int? minutes, | ||
| int? seconds, | ||
| int? milliseconds, | ||
| ]); | ||
|
|
||
| /// The [Date constructor] that uses [individual component integers], | ||
| /// interpreted in the UTC time zone. | ||
| /// | ||
| /// [Date constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date | ||
| /// [individual component integers]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#individual_date_and_time_component_values | ||
| factory JSDate.utcDate( | ||
| int year, | ||
| int month, [ | ||
| int? day, | ||
| int? hours, | ||
| int? minutes, | ||
| int? seconds, | ||
| int? milliseconds, | ||
| ]) { | ||
| var ms = switch ((day, hours, minutes, seconds, milliseconds)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love me some switch! |
||
| (_, _, _, _, var milliseconds?) => JSDate.utcAsMillisecondsSinceEpoch( | ||
| year, | ||
| month, | ||
| day, | ||
| hours, | ||
| minutes, | ||
| seconds, | ||
| milliseconds, | ||
| ), | ||
| (_, _, _, var seconds?, _) => JSDate.utcAsMillisecondsSinceEpoch( | ||
| year, | ||
| month, | ||
| day, | ||
| hours, | ||
| minutes, | ||
| seconds, | ||
| ), | ||
| (_, _, var minutes?, _, _) => JSDate.utcAsMillisecondsSinceEpoch( | ||
| year, | ||
| month, | ||
| day, | ||
| hours, | ||
| minutes, | ||
| ), | ||
| (_, var hours?, _, _, _) => JSDate.utcAsMillisecondsSinceEpoch( | ||
| year, | ||
| month, | ||
| day, | ||
| hours, | ||
| ), | ||
| (var day?, _, _, _, _) => JSDate.utcAsMillisecondsSinceEpoch( | ||
| year, | ||
| month, | ||
| day, | ||
| ), | ||
| _ => JSDate.utcAsMillisecondsSinceEpoch(year, month), | ||
| }; | ||
| return JSDate.fromMillisecondsSinceEpoch(ms); | ||
| } | ||
|
|
||
| /// Dee [`Date.now()`]. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| /// | ||
|
|
@@ -55,13 +118,15 @@ extension type JSDate._(JSObject _) implements JSObject { | |
| /// | ||
| /// [`Date.utc()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC | ||
| @JS('UTC') | ||
| external static int utc(int year, | ||
| [int? month, | ||
| int? day, | ||
| int? hours, | ||
| int? minutes, | ||
| int? seconds, | ||
| int? milliseconds]); | ||
| external static int utcAsMillisecondsSinceEpoch( | ||
| int year, [ | ||
| int? month, | ||
| int? day, | ||
| int? hours, | ||
| int? minutes, | ||
| int? seconds, | ||
| int? milliseconds, | ||
| ]); | ||
|
|
||
| /// See [`Date.getDate()`] and [`Date.setDate()`]. | ||
| /// | ||
|
|
@@ -81,14 +146,10 @@ extension type JSDate._(JSObject _) implements JSObject { | |
| /// [`Date.getDay()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay | ||
| /// [`Date.setDay()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDay | ||
| int get day => _getDay(); | ||
| set day(int value) => _setDay(value); | ||
|
|
||
| @JS('getDay') | ||
| external int _getDay(); | ||
|
|
||
| @JS('setDay') | ||
| external void _setDay(int value); | ||
|
|
||
| /// See [`Date.getFullYear()`] and [`Date.setFullYear()`]. | ||
| /// | ||
| /// [`Date.getFullYear()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ repository: https://github.com/dart-lang/web | |
|
|
||
| environment: | ||
| sdk: ^3.9.0 | ||
|
|
||
| dev_dependencies: | ||
| test: ^1.26.0 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we think
JSDate.fromMillisecondsSinceEpoch(JSDate.utcAsMillisecondsSinceEpoch(...))is bad enough that we should include this? I mostly ask because our treatment of nullability makes this factory a bit more complex. If we do want this we should document that treatment (e.g. "ifdayis null, we only pass year and month. if not and ifhoursis nullable,...").There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think given that there's already a local-time constructor, having a symmetrical one for UTC makes sense (especially because it's often going to be the safer option).
It actually works in the opposite way: it passes all arguments through the last non-null. This makes the observable behavior identical to
localDate: if you pass any intermediate nulls, you get an "Invalid Date". Since the behaviors match (and match the behavior of callingJSDate.fromMillisecondsSinceEpoch(JSDate.utcAsMillisecondsSinceEpoch(...))I don't think it needs special documentation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, I thought you were trying to avoid passing
nullregardless of whether the user passed it.My follow-up question would've been "why not implement the body as
JSDate.fromMillisecondsSinceEpoch(JSDate.utcAsMillisecondsSinceEpoch(...))", but it looks like someone decided to make the default fordays1: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#parameters, so there is an observable difference between passingnulland not passing anything. This LGTM then.Is this true? I'm constructing e.g.
Date(2025, 11, null, 1)and that results in a valid date.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the behavior differs between null and undefined 😰.
nullgets coerced to 0,undefinedgets coerced toNaNand produces an invalid date.