Validity of DateTime from setZone with a valid Zone #1740
-
|
I incorrectly raised an issue about the TS types for Luxon: #1739 Before I head off to DefinitelyTyped.. ie. const validDateTime = DateTime.now(); // now is always valid
const maybeValidZone = IANAZone.create(userInput);
if (maybeValidZone.isValid) {
const zonedDateTime = validDateTime.setZone(maybeValidZone); // maybeValidZone is valid as passed check;
// therefore is zoneDateTime guranteed to be valid?
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The correct answer is: No. // the following date is valid, but it is the maximum Date
const date = DateTime.fromMillis(8640000000000000, { zone: "UTC" });
// this zone is valid
const zone = IANAZone.create("Europe/Berlin");
// this date is invalid, because adding the zone offset pushes it over the "maximum date"
const zonedDate = date.setZone(zone); |
Beta Was this translation helpful? Give feedback.
The correct answer is: No.
However this can only happen in the edge case where the zone conversion causes Luxon to have to create an invalid Date (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date).
Let me demonstrate: