-
Notifications
You must be signed in to change notification settings - Fork 149
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
Allow to create temporal objects from standard JS Date #390
Conversation
This commit adds `#fromStandardDate()` functions to all temporal types except `Duration`. Such functions allow to create temporal objects from the provided standard JavaScript `Date`.
6e679bc
to
c6c1a08
Compare
src/v1/temporal-types.js
Outdated
|
||
return new Date( | ||
standardDate.getFullYear(), | ||
standardDate.getMonth(), |
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 there's a problem about conversion of month
value from/to standard date as javascript Date
object stores months as 0
based values - with 0
= January
and 11
= December
. It seems to me that we do not consider this difference in conversions.
Maybe add value range checks on temporal type constructors?
src/v1/internal/temporal-util.js
Outdated
} | ||
|
||
/** | ||
* Get the total number of nanoseconds from the given standard JavaScript 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 seems that the comment is outdated.
src/v1/temporal-types.js
Outdated
|
||
return new LocalDateTime( | ||
standardDate.getFullYear(), | ||
standardDate.getMonth(), |
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.
Same here.
src/v1/temporal-types.js
Outdated
|
||
return new DateTime( | ||
standardDate.getFullYear(), | ||
standardDate.getMonth(), |
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.
Same here.
@@ -658,6 +659,147 @@ describe('temporal-types', () => { | |||
expect(() => new neo4j.types.DateTime(1, 2, 3, 4, 5, 6, 7, 8, 'UK')).toThrow(); | |||
}); | |||
|
|||
it('should convert standard Date to neo4j LocalTime', () => { |
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.
Maybe add some integration tests that incorporate standard date -> temporal type conversions?
Standard dates have zero-based month. Neo4j temporal types have 1-based month. Conversion from standard date with zero month was not handled correctly and resulted in zero month in neo4j temporal types.
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.
Good stuff, I have nothing to add!
Any reason to not apply this to native JS dates automatically? |
@ivan-kleshnin the problem is that driver can't know which Neo4j type should the native JS date be converted to. It can be converted to either That's why this PR adds explicit conversion methods so application code can tell driver which Neo4j temporal type is required. Does this make sense to you? |
@lutovich yes, thanks. That what I thought, but as a Neo4j noob wanted to ensure :) |
This PR adds
#fromStandardDate()
functions to all temporal types exceptDuration
. Such functions allow to create temporal objects from the provided standard JavaScriptDate
.