'Is there a way in luxon to deal with impossible or "dual times" caused by DST
So to set the scene we have a requirement that every night at 01:30 UK time now this is fine for most days of the year but if I create use
DateTime.fromObject({year:2022, month:3, day:27, hour: 1, minute: 30, second: 0 }, { zone: 'Europe/London' });
What I get back is 2022-03-27T02:30:00.000+01:00 not 2022-03-27T01:00:00.000+01:00 or 2022-03-27T02:00:00.000+01:00 is there any way for me to force this functionality within luxon or another more suitable library that can handle this?
function dateTimeDisambiguation(timezone,plaindate) {
tz = new Temporal.TimeZone(timezone);
const instants = tz.getPossibleInstantsFor(plaindate);
switch(instants.length){
case 0:
const transition = tz.getPreviousTransition(tz.getInstantFor(plaindate, { disambiguation: 'later'}));
const beforeTransition = transition.subtract({ nanoseconds: 1 });
return beforeTransition;
default:
return instants[0];
}
}
tz = new Temporal.TimeZone('Europe/London');
skipSimple = {year: 2022, month: 3, day: 27, hour: 1, minute: 30 };
doubleSimple = {year: 2022, month: 10, day: 30, hour: 1, minute: 30 };
normalSimple = {year: 2022, month: 10, day: 30, hour: 10, minute: 30 };
const skip = dateTimeDisambiguation('Europe/London', skipSimple);
const double = dateTimeDisambiguation('Europe/London', doubleSimple);
const normal = dateTimeDisambiguation('Europe/London', normalSimple);
console.log(skip.toString()); => // "2022-03-27T00:59:59.999999999Z"
console.log(double.toString()); => // "2022-10-30T00:30:00Z"
console.log(normal.toString()); => // "2022-10-30T10:30:00Z"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
