-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt cookbook examples to ZonedDateTime
This adapts all the relevant cookbook examples (except the meeting planner which is blocked on a bug in ZonedDateTime.toLocaleString) to use ZonedDateTime. Most of these were identified in Justin's original pull request. Co-authored-by: Justin Grant <[email protected]> See: #569
- Loading branch information
Showing
11 changed files
with
66 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,6 @@ | ||
/** | ||
* Given a localized departure time and a flight duration, get a local arrival | ||
* time in the destination time zone. | ||
* | ||
* FIXME: This becomes a one-liner when Temporal.ZonedDateTime.add() is | ||
* implemented. | ||
* | ||
* @param {string} departure - Departure time with time zone | ||
* @param {Temporal.Duration} flightTime - Duration of the flight | ||
* @param {Temporal.TimeZone} destinationTimeZone - Time zone in which the | ||
* flight's destination is located | ||
* @param {Temporal.Calendar|string} calendar - Calendar system used for output | ||
* @returns {Temporal.PlainDateTime} Local arrival time | ||
*/ | ||
function getLocalizedArrival(departure, flightTime, destinationTimeZone, calendar) { | ||
const instant = departure.toInstant(); | ||
const arrival = instant.add(flightTime); | ||
return destinationTimeZone.getDateTimeFor(arrival, calendar); | ||
} | ||
const departure = Temporal.ZonedDateTime.from('2020-03-08T11:55:00+08:00[Asia/Hong_Kong]'); | ||
const flightTime = Temporal.Duration.from({ minutes: 775 }); | ||
|
||
const arrival = getLocalizedArrival( | ||
Temporal.ZonedDateTime.from('2020-03-08T11:55:00+08:00[Asia/Hong_Kong]'), | ||
Temporal.Duration.from({ minutes: 775 }), | ||
Temporal.TimeZone.from('America/Los_Angeles'), | ||
'iso8601' | ||
); | ||
assert.equal(arrival.toString(), '2020-03-08T09:50:00'); | ||
const arrival = departure.add(flightTime).withTimeZone('America/Los_Angeles'); | ||
|
||
assert.equal(arrival.toString(), '2020-03-08T09:50:00-07:00[America/Los_Angeles]'); |
38 changes: 0 additions & 38 deletions
38
docs/cookbook/getParseableZonedStringWithLocalTimeInOtherZone.mjs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,6 @@ | ||
/** | ||
* Given localized departure and arrival times, get a trip duration suitable | ||
* for display in an airline ticket website, for example. | ||
* | ||
* @param {string} parseableDeparture - Departure time with time zone | ||
* @param {string} parseableArrival - Arrival time with time zone | ||
* @returns {Temporal.Duration} A duration with units no larger than hours | ||
*/ | ||
function getTripDurationInHrMinSec(parseableDeparture, parseableArrival) { | ||
const departure = Temporal.Instant.from(parseableDeparture); | ||
const arrival = Temporal.Instant.from(parseableArrival); | ||
return departure.until(arrival, { largestUnit: 'hours' }); | ||
} | ||
const departure = Temporal.ZonedDateTime.from('2020-03-08T11:55:00+08:00[Asia/Hong_Kong]'); | ||
const arrival = Temporal.ZonedDateTime.from('2020-03-08T09:50:00-07:00[America/Los_Angeles]'); | ||
|
||
const flightTime = departure.until(arrival); | ||
|
||
const flightTime = getTripDurationInHrMinSec( | ||
'2020-03-08T11:55:00+08:00[Asia/Hong_Kong]', | ||
'2020-03-08T09:50:00-07:00[America/Los_Angeles]' | ||
); | ||
assert.equal(flightTime.toString(), 'PT12H55M'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const source = Temporal.ZonedDateTime.from('2020-01-09T00:00[America/Chicago]'); | ||
|
||
const result = source.withTimeZone('America/Los_Angeles'); | ||
|
||
// On this date, when it's midnight in Chicago, it's 10 PM the previous night in LA | ||
assert.equal(result.toString(), '2020-01-08T22:00:00-08:00[America/Los_Angeles]'); |