UNPKG

2.67 kBJavaScriptView Raw
1/*
2 * @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
3 * @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
4 * @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
5 */
6
7import {ChronoField} from './ChronoField';
8import {createTemporalQuery} from './TemporalQuery';
9import {TemporalQueries} from './TemporalQueries';
10
11import {LocalDate} from '../LocalDate';
12import {LocalTime} from '../LocalTime';
13import {ZoneOffset} from '../ZoneOffset';
14
15
16export function _init() {
17 //-----------------------------------------------------------------------
18 /**
19 * A strict query for the {@link ZoneId}.
20 */
21 TemporalQueries.ZONE_ID = createTemporalQuery('ZONE_ID', (temporal) => {
22 return temporal.query(TemporalQueries.ZONE_ID);
23 });
24
25 /**
26 * A query for the {@link Chronology}.
27 */
28 TemporalQueries.CHRONO = createTemporalQuery('CHRONO', (temporal) => {
29 return temporal.query(TemporalQueries.CHRONO);
30 });
31
32 /**
33 * A query for the smallest supported unit.
34 */
35 TemporalQueries.PRECISION = createTemporalQuery('PRECISION', (temporal) => {
36 return temporal.query(TemporalQueries.PRECISION);
37 });
38
39 //-----------------------------------------------------------------------
40 /**
41 * A query for {@link ZoneOffset} returning null if not found.
42 */
43 TemporalQueries.OFFSET = createTemporalQuery('OFFSET', (temporal) => {
44 if (temporal.isSupported(ChronoField.OFFSET_SECONDS)) {
45 return ZoneOffset.ofTotalSeconds(temporal.get(ChronoField.OFFSET_SECONDS));
46 }
47 return null;
48 });
49
50 /**
51 * A lenient query for the {@link ZoneId}, falling back to the {@link ZoneOffset}.
52 */
53 TemporalQueries.ZONE = createTemporalQuery('ZONE', (temporal) => {
54 const zone = temporal.query(TemporalQueries.ZONE_ID);
55 return (zone != null ? zone : temporal.query(TemporalQueries.OFFSET));
56 });
57
58 /**
59 * A query for {@link LocalDate} returning null if not found.
60 */
61 TemporalQueries.LOCAL_DATE = createTemporalQuery('LOCAL_DATE', (temporal) => {
62 if (temporal.isSupported(ChronoField.EPOCH_DAY)) {
63 return LocalDate.ofEpochDay(temporal.getLong(ChronoField.EPOCH_DAY));
64 }
65 return null;
66 });
67
68 /**
69 * A query for {@link LocalTime} returning null if not found.
70 */
71 TemporalQueries.LOCAL_TIME = createTemporalQuery('LOCAL_TIME', (temporal) => {
72 if (temporal.isSupported(ChronoField.NANO_OF_DAY)) {
73 return LocalTime.ofNanoOfDay(temporal.getLong(ChronoField.NANO_OF_DAY));
74 }
75 return null;
76 });
77}