Skip to content
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

doc: converting LocalDateTime instance to ZonedDateTime #1

Closed
mikeal opened this issue Mar 15, 2017 · 4 comments
Closed

doc: converting LocalDateTime instance to ZonedDateTime #1

mikeal opened this issue Mar 15, 2017 · 4 comments

Comments

@mikeal
Copy link

mikeal commented Mar 15, 2017

What is the API for converting between the two base types?

@maggiepint
Copy link
Member

maggiepint commented Mar 16, 2017

Haven't decided, but the following mirrors Java 8 mostly and I think it works well:

var ldt = new temporal.LocalDateTime(2017, 12, 31, 23, 59);
var zoned = temporal.ZonedDateTime.of(ldt, 'America/Chicago', {
    resolver: (mapping) => ({
        skipped: mapping.forwardShifted(),
        ambiguous: mapping.firstOccurrence()
    })
}); //the options object with the resolver is optional here

var backToLocal = zoned.toLocalDateTime();

Java 8 has three methods for converting from local to zoned - .of(ltd, zoneid) which is shown, .ofLocal(ldt, zoneid, preferredOffset) which allows the user to specify a preferred offset for ambiguous times, and ofStrict(ldt, offset, zoneid) which will validate the combination of the three parameters.

I'm not sure if I want to get that deep. To me the resolver seems reasonable enough for control of ambiguous times. Use of the 'throws' mapping would create all the same functionality of the three method system, but allow it all in one.

@timrwood
Copy link
Contributor

I like the withZone examples for converting a ZonedDateTime between zones, could something similar be used here?

var ldt = new temporal.LocalDateTime(2017, 12, 31, 23, 59);
var zoned = ldt.withZone('America/Chicago', {
    resolver: (mapping) => ({
        skipped: mapping.forwardShifted(),
        ambiguous: mapping.firstOccurrence()
    })
});

I like the parallel of using withoutZone to convert from ZonedDateTime to LocalDateTime, but toLocalDateTime is a bit more explicit, especially if other types are included in the proposal, like toLocalDate or toLocalTime.

@maggiepint
Copy link
Member

@timrwood both could probably quite reasonably be implemented. They both exist in Java. Will have to think how many options we want to give people.

@mattjohnsonpint
Copy link
Collaborator

With the revised proposal, the types are now called PlainDateTime and ZonedInstant respectively. The conversion is as follows:

let dt = temporal.createDateTime(2017, 12, 31, 23, 59);
let zi = dt.withZone('America/Chicago', options);

options can be omitted, and is not currently spec'd - but the intention is to allow things like the resolver to be adjusted. We will eventually cover this in the spec, and describe the default resolver behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants