-
Notifications
You must be signed in to change notification settings - Fork 114
/
CldrZoneTextPrinterParser.js
192 lines (171 loc) · 7.3 KB
/
CldrZoneTextPrinterParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* @copyright (c) 2017, Philipp Thuerwaechter & Pattrick Hueper
* @license BSD-3-Clause (see LICENSE.md in the root directory of this source tree)
*/
import {
_ as jodaInternal,
TextStyle,
TemporalQueries,
ZoneId,
ZoneOffset,
ZoneRulesProvider,
} from '@js-joda/core';
import {getOrCreateCldrInstance, getOrCreateMapZones, loadCldrData} from './CldrCache';
const { assert: { requireNonNull, requireInstance } } = jodaInternal;
//-----------------------------------------------------------------------
const LENGTH_COMPARATOR = (str1, str2) => {
let cmp = str2.length - str1.length;
if (cmp === 0) {
cmp = str1.localeCompare(str2);
}
return cmp;
};
/**
* Cache for `_cachedResolveZoneIdText`.
*
* Its basic structure is:
* Obj { locale: zoneId }
* Obj { zoneId: style}
* Obj { style: type}
* Obj { type: resolvedZoneIdText}
*/
const resolveZoneIdTextCache = {};
/**
* Prints or parses a zone ID.
*/
export default class CldrZoneTextPrinterParser {
/** The text style to output. */
constructor(textStyle) {
requireNonNull(textStyle, 'textStyle');
requireInstance(textStyle, TextStyle, 'textStyle');
this._textStyle = textStyle;
loadCldrData('supplemental/likelySubtags.json');
loadCldrData('supplemental/metaZones.json');
}
_cachedResolveZoneIdText(cldr, zoneId, style, type) {
if (resolveZoneIdTextCache[cldr.locale] == null) {
resolveZoneIdTextCache[cldr.locale] = {};
}
const zoneIdToStyle = resolveZoneIdTextCache[cldr.locale];
if (zoneIdToStyle[zoneId] == null) {
zoneIdToStyle[zoneId] = {};
}
const styleToType = zoneIdToStyle[zoneId];
if (styleToType[style] == null) {
styleToType[style] = {};
}
const typeToResolvedZoneIdText = styleToType[style];
if (typeToResolvedZoneIdText[type] == null) {
typeToResolvedZoneIdText[type] = this._resolveZoneIdText(cldr, zoneId, style, type);
}
return typeToResolvedZoneIdText[type];
}
_resolveZoneIdText(cldr, zoneId, style, type) {
const zoneData = cldr.main(`dates/timeZoneNames/zone/${zoneId}/${style}/${type}`);
if (zoneData) {
return zoneData;
} else {
const metazoneInfo = cldr.get(`supplemental/metaZones/metazoneInfo/timezone/${zoneId}`);
if (metazoneInfo) {
// const zoneData = cldr.main(`dates/timeZoneNames/metazone/Acre`);
// TODO: determine metaZone for current temporal, for now, we use the last one :/
const metazone = metazoneInfo[metazoneInfo.length - 1]['usesMetazone']['_mzone'];
let metaZoneData = cldr.main(`dates/timeZoneNames/metazone/${metazone}/${style}/${type}`);
if (metaZoneData) {
return metaZoneData;
} else {
// type fallback, first generic, then standard
metaZoneData = cldr.main(`dates/timeZoneNames/metazone/${metazone}/${style}/generic`);
if (!metaZoneData) {
metaZoneData = cldr.main(`dates/timeZoneNames/metazone/${metazone}/${style}/standard`);
}
if (metaZoneData) {
return metaZoneData;
} else {
const mapZones = getOrCreateMapZones(cldr);
// find preferred Zone and resolve again
const preferredZone = mapZones[metazone][cldr.attributes.territory];
if (preferredZone) {
if (preferredZone !== zoneId) {
return this._cachedResolveZoneIdText(cldr, preferredZone, style, type);
}
} else {
// find golden Zone and resolve again
const goldenZone = mapZones[metazone]['001'];
if (goldenZone !== zoneId) {
return this._cachedResolveZoneIdText(cldr, goldenZone, style, type);
}
}
}
}
}
}
}
//-----------------------------------------------------------------------
print(context, buf) {
//see http://www.unicode.org/reports/tr35/tr35-dates.html#Time_Zone_Names
const zone = context.getValueQuery(TemporalQueries.zoneId());
/* istanbul ignore if */ // shouldn't happen... getValueQuery throws before returning null
if (zone == null) {
return false;
}
if (zone.normalized() instanceof ZoneOffset) {
buf.append(zone.id());
return true;
}
const daylight = false;
const hasDaylightSupport = false;
/* TODO: currently js-joda-timezone does not support ZoneRules.isDaylightSavings() ... uncomment if it does
const temporal = context.temporal();
if (temporal.isSupported(ChronoField.INSTANT_SECONDS)) {
hasDaylightSupport = true;
const instant = Instant.ofEpochSecond(temporal.getLong(ChronoField.INSTANT_SECONDS));
daylight = zone.rules().isDaylightSavings(instant);
}*/
const tzType = hasDaylightSupport ? (daylight ? 'daylight' : 'standard') : 'generic';
const tzstyle = (this._textStyle.asNormal() === TextStyle.FULL ? 'long' : 'short');
loadCldrData(`main/${context.locale().localeString()}/timeZoneNames.json`);
const cldr = getOrCreateCldrInstance(context.locale().localeString());
const text = this._cachedResolveZoneIdText(cldr, zone.id(), tzstyle, tzType);
if (text) {
buf.append(text);
} else {
// fallback, print zoneId
buf.append(zone.id());
}
return true;
}
parse(context, text, position) {
const ids = {};
loadCldrData(`main/${context.locale().localeString()}/timeZoneNames.json`);
const cldr = getOrCreateCldrInstance(context.locale().localeString());
for (const id of ZoneRulesProvider.getAvailableZoneIds()) {
ids[id] = id;
const tzstyle = (this._textStyle.asNormal() === TextStyle.FULL ? 'long' : 'short');
const genericText = this._cachedResolveZoneIdText(cldr, id, tzstyle, 'generic');
if (genericText) {
ids[genericText] = id;
}
const standardText = this._cachedResolveZoneIdText(cldr, id, tzstyle, 'standard');
if (standardText) {
ids[standardText] = id;
}
const daylightText = this._cachedResolveZoneIdText(cldr, id, tzstyle, 'daylight');
if (daylightText) {
ids[daylightText] = id;
}
}
// threeten is using a (sorted) TreeMap... so we need to sort the keys
const sortedKeys = Object.keys(ids).sort(LENGTH_COMPARATOR);
for (const name of sortedKeys) {
if (context.subSequenceEquals(text, position, name, 0, name.length)) {
context.setParsedZone(ZoneId.of(ids[name]));
return position + name.length;
}
}
return ~position;
}
toString() {
return `ZoneText(${this._textStyle})`;
}
}