-
Notifications
You must be signed in to change notification settings - Fork 157
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
Should LocalDateTime.from
accept a timezoneOffsetNanoseconds
field?
#718
Comments
|
Yep, that's correct.
The only way to do it with // Get a DateTime during the repeated-by-DST clock hour.
const dt = new Temporal.DateTime(2020, 11, 1, 1, 30);
// Convert to LocalDateTime. Default disambiguation will pick the earlier (-07:00) local time.
const ldt = new Temporal.LocalDateTime({...dt.getFields(), timeZone: 'America/Los_Angeles'});
// => "2020-11-01T01:30-07:00[America/Los_Angeles]"
// desired: one hour later in real world, but same local time
const newAbsolute = ldt.absolute.plus({nanoseconds: 3.6e12});
const newLocalDateTime = ldt.with({absolute: newAbsolute});
// => "2020-11-01T01:30-08:00[America/Los_Angeles]"
Nope, the offset is calculated fields as a function of (
Currently in the prototype, both // Deal with the rest of the fields. If there's a change in tz offset, it'll
// be handled by `from`. Also, unless we're changing the `absolute` then
// omit it so that it won't conflict with any other fields that we're adding
// here.
const { absolute: baseAbsolute, timeZoneOffsetNanoseconds: baseOffset, ...fields } = base.getFields();
if (updateAbsolute) (fields as LocalDateTimeFields).absolute = baseAbsolute;
if (updateOffset) (fields as LocalDateTimeFields).timeZoneOffsetNanoseconds = baseOffset;
const merged = { ...fields, ...localDateTimeLike };
return LocalDateTime.from(merged, options); So you can probably see why I'm on the fence about supporting this field or not. There are some clear use cases why it'd be helpful, but those are also relatively obscure and adding the field adds some complexity. |
Ah, right, it's Absolute+TimeZone, not DateTime+TimeZone. So I didn't understand it correctly. In that case I'd agree it's not necessary to make this field persistent or support it in |
Yep. After writing a lot of sample code using
// current implementation
Temporal.LocalDateTime.from({...date.getFields(), ...time.getFields(), timeZone: 'Europe/Paris'});
// Alternate idea could be to accept fake fields: `date`, `time`, `monthDay`, `yearMonth`.
// These would not be emitted via getFields().
// If there's a conflict (e.g. `date` vs. `year`) then always throw.
// If we did this here, we'd also want to do it in `DateTime.from` and `Date.from`.
Temporal.LocalDateTime.from({date, time, timeZone: 'Europe/Paris'}); I think that all these questions boil down to one core issue: Are After spending a lot of time writing code with Temporal, I come down squarely in the latter, ergonomics-first camp. I care more about the quack of the duck than its internal organs. The fact that I think this is related to what @pipobscure was saying here:
|
Here's another use case that's related to this issue: "Truncate minutes and smaller units" // "second" 1:15AM on a day that DST ends
const ldt = LocalDateTime.from({
year: 2020,
month: 11,
day: 1,
hour: 1,
minutes: 15,
seconds: 30,
milliseconds: 123,
microseconds: 456,
nanoseconds: 789
}, {disambiguation: 'later');
// should this return a time in the earlier or later 1-2AM hour?
ldt.with({minutes:0, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0}).toString(); There are two options:
My sense is that this is a corner case and we should do the easiest thing (Option 2). But it's an example of how a ldt.with({
minutes: 0,
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0,
timeZoneOffsetNanoseconds: ldt.timeZoneOffsetNanoseconds
}).toString(); That said, this behavior is still available without function getCurrentDisambiguation(ldt) {
const possibleAbsolutes = ldt.timeZone.getPossibleAbsolutesFor(ldt.toDateTime());
if (possibleAbsolutes.length < 2) return 'compatible';
return possibleAbsolutes[0].equals(ldt.absolute) ? 'earlier' : 'later';
}
ldt.with({
minutes: 0,
seconds: 0,
milliseconds: 0,
microseconds: 0,
nanoseconds: 0
}, {disambiguation: getCurrentDisambiguation(ldt)).toString(); So even though removing Also, it's arguable that the "harder" approach is actually more correct too, because if a DST transition happens in the middle of a clock hour or isn't a full hour of offset change, then the "Option 1" code could throw if the old So in the end I'm still leaning towards removing this prop from |
I realized that there's a potential compromise position: remove
Here's an example that illustrates both problems: function otherTimeZoneSameClockTime(ldt, newTimeZone) {
const { timeZoneOffsetNanoseconds, ...rest } = ldt.getFields();
return Temporal.LocalDateTime.from({...rest, timeZone: newTimeZone});
} With this change, it'd be simpler: function otherTimeZoneSameClockTime(ldt, newTimeZone) {
return Temporal.LocalDateTime.from({...ldt.getFields(), timeZone: newTimeZone});
} Another problem I've been concerned about (which doesn't go away if we take this prop out of What I'm proposing here seems closer in spirit to the other two "advanced users only" features in Option 3 of #706 (comment), because it'd be completely invisible to novice users who only use defaults. Anyway, I'm planning to make this initial change today or tomorrow. Then we can decide on the larger change of removing this prop from Sound OK?
BTW, I found a simpler and more consistent way to get the former behavior: if the resulting I'm still not 100% sure that this will work--gotta try a few more use cases-- but so far it looks promising. |
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
Changes described above are in db8a8db. In addition to better DX, the new |
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
If this is done in the proof-of-concept code, can we close this issue? (By the way, it seems to me that |
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
Yep. Closing.
Yep. Here's the canonical samples excerpted from LocalDateTime JSDoc:
|
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
- github.com/tc39/issues/718#issuecomment-655817615 has details about these changes. - Remove `timeZoneOffsetNanoseconds` from `getFields()` - Rename `prefer` option to `offset`, and rename its choices to `use` (always use offset), `ignore` (never use it), and `reject` (throw if conflict). - Add new `offset: 'prefer'` (use if valid for that time zone, ignore if not) option and make it the default for `with` - Update `from` and `with` to use new options - Simplify `from` business logic & refactor to share string vs object code (net reduction of 50+ LOC) - Updated JSDoc with changes above - Fix various bugs with non-ISO calendars, e.g. toString() - Added __repr__ for console debugging - Fixed lint configs
There were a few reasons for accepting this field:
from
, which does accept an offsetBut looking through that list, I'm now questioning whether these reasons are so compelling that they justify the additional complexity of accepting
timezoneOffsetNanoseconds
infrom
andwith
(and emitting it ingetFields
). Do you think any of those reasons are enough to warrant having this be a persisted field?If not, then I'm also inclined to do exactly what you recommend: keep the field but make it a non-persisted like
hoursInDay
. If it's removed, it will shave a lot of complexity fromfrom
andwith
.The text was updated successfully, but these errors were encountered: