Use browser timezone in test generator
This commit is contained in:
1 parent
2cd9a9dbae
commit
98b856d1d6
3 files changed
+81
-36
No files matched your search
@@ -4,7 +4,7 @@
|
||||
const DEFAULTS = Object.freeze({
|
||||
start: "2026-09-21T17:00:00-07:00",
|
||||
end: "2026-09-23T17:00:00-07:00",
|
||||
tz: "America/Los_Angeles",
|
||||
tz: "",
|
||||
freq: "910.1",
|
||||
bw: "500",
|
||||
sf: "8",
|
||||
@@ -98,12 +98,47 @@
|
||||
}
|
||||
}
|
||||
|
||||
function configFromSearch(search) {
|
||||
function browserTimeZone() {
|
||||
try {
|
||||
return validateTimeZone(
|
||||
new Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC"
|
||||
);
|
||||
} catch (error) {
|
||||
return "UTC";
|
||||
}
|
||||
}
|
||||
|
||||
function supportedTimeZones(selectedTimeZone) {
|
||||
const zones = new Set(["UTC"]);
|
||||
if (typeof Intl.supportedValuesOf === "function") {
|
||||
Intl.supportedValuesOf("timeZone").forEach(function (zone) {
|
||||
zones.add(zone);
|
||||
});
|
||||
} else {
|
||||
[
|
||||
"Africa/Johannesburg", "America/Chicago", "America/Denver",
|
||||
"America/Los_Angeles", "America/New_York", "America/Phoenix",
|
||||
"Asia/Kolkata", "Asia/Tokyo", "Australia/Sydney", "Europe/Berlin",
|
||||
"Europe/London", "Pacific/Auckland", "Pacific/Honolulu",
|
||||
].forEach(function (zone) { zones.add(zone); });
|
||||
}
|
||||
if (selectedTimeZone) zones.add(validateTimeZone(selectedTimeZone));
|
||||
return Array.from(zones).sort(function (left, right) {
|
||||
if (left === "UTC") return -1;
|
||||
if (right === "UTC") return 1;
|
||||
return left.localeCompare(right);
|
||||
});
|
||||
}
|
||||
|
||||
function configFromSearch(search, fallbackTimeZone) {
|
||||
const params = new URLSearchParams(search || "");
|
||||
const raw = {};
|
||||
Object.keys(DEFAULTS).forEach(function (key) {
|
||||
raw[key] = params.has(key) ? params.get(key) : DEFAULTS[key];
|
||||
});
|
||||
if (!params.has("tz")) {
|
||||
raw.tz = fallbackTimeZone || browserTimeZone();
|
||||
}
|
||||
|
||||
const startMs = parseTimestamp(raw.start, "start");
|
||||
const endMs = parseTimestamp(raw.end, "end");
|
||||
@@ -500,7 +535,10 @@
|
||||
function init(root) {
|
||||
let config;
|
||||
try {
|
||||
config = configFromSearch(global.location ? global.location.search : "");
|
||||
config = configFromSearch(
|
||||
global.location ? global.location.search : "",
|
||||
browserTimeZone()
|
||||
);
|
||||
} catch (error) {
|
||||
const box = root.querySelector('[data-role="config-error"]');
|
||||
box.hidden = false;
|
||||
@@ -548,7 +586,15 @@
|
||||
if (generator) {
|
||||
generator.elements.start.value = zonedInputValue(config.startMs, config.tz);
|
||||
generator.elements.end.value = zonedInputValue(config.endMs, config.tz);
|
||||
generator.elements.tz.value = config.tz;
|
||||
const zoneSelect = generator.elements.tz;
|
||||
zoneSelect.textContent = "";
|
||||
supportedTimeZones(config.tz).forEach(function (zone) {
|
||||
const option = document.createElement("option");
|
||||
option.value = zone;
|
||||
option.textContent = zone;
|
||||
zoneSelect.appendChild(option);
|
||||
});
|
||||
zoneSelect.value = config.tz;
|
||||
generator.elements.freq.value = config.freqText;
|
||||
generator.elements.bw.value = config.bwText;
|
||||
generator.elements.sf.value = String(config.sf);
|
||||
@@ -559,17 +605,6 @@
|
||||
generator.elements.normalcr.value = String(config.normalCr);
|
||||
generator.elements.tx.value = config.txText;
|
||||
|
||||
const zoneList = root.querySelector("#preset-test-time-zones");
|
||||
if (zoneList && typeof Intl.supportedValuesOf === "function") {
|
||||
Intl.supportedValuesOf("timeZone").forEach(function (zone) {
|
||||
if (!zoneList.querySelector('option[value="' + zone + '"]')) {
|
||||
const option = document.createElement("option");
|
||||
option.value = zone;
|
||||
zoneList.appendChild(option);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function generateUrl() {
|
||||
const errorBox = root.querySelector('[data-role="generator-error"]');
|
||||
try {
|
||||
@@ -732,6 +767,8 @@
|
||||
configFromSearch: configFromSearch,
|
||||
configFromGenerator: configFromGenerator,
|
||||
validateTimeZone: validateTimeZone,
|
||||
browserTimeZone: browserTimeZone,
|
||||
supportedTimeZones: supportedTimeZones,
|
||||
phaseAt: phaseAt,
|
||||
remainingMinutes: remainingMinutes,
|
||||
immediateAvailable: immediateAvailable,
|
||||
|
||||
+7
-19
@@ -282,9 +282,10 @@ clock</code></pre>
|
||||
</label>
|
||||
<label>
|
||||
<span>Time zone</span>
|
||||
<input type="text" name="tz" list="preset-test-time-zones" required
|
||||
autocomplete="off" spellcheck="false">
|
||||
<small>Use an IANA name such as America/Los_Angeles or UTC.</small>
|
||||
<select name="tz" required>
|
||||
<option value="UTC">UTC</option>
|
||||
</select>
|
||||
<small>All IANA time zones supported by this browser are listed.</small>
|
||||
</label>
|
||||
<div class="preset-test-generator-subheading">Test radio profile</div>
|
||||
<label>
|
||||
@@ -365,20 +366,6 @@ clock</code></pre>
|
||||
</aside>
|
||||
</div>
|
||||
|
||||
<datalist id="preset-test-time-zones">
|
||||
<option value="America/Los_Angeles"></option>
|
||||
<option value="America/Denver"></option>
|
||||
<option value="America/Chicago"></option>
|
||||
<option value="America/New_York"></option>
|
||||
<option value="America/Phoenix"></option>
|
||||
<option value="Pacific/Honolulu"></option>
|
||||
<option value="UTC"></option>
|
||||
<option value="Europe/London"></option>
|
||||
<option value="Europe/Berlin"></option>
|
||||
<option value="Asia/Tokyo"></option>
|
||||
<option value="Australia/Sydney"></option>
|
||||
</datalist>
|
||||
|
||||
<p class="preset-test-error preset-test-generator-error"
|
||||
data-role="generator-error" role="alert" hidden></p>
|
||||
<pre class="preset-test-generated-url"><code data-command="generated-url"></code></pre>
|
||||
@@ -389,8 +376,9 @@ clock</code></pre>
|
||||
|
||||
<p class="preset-test-note">
|
||||
Advanced use: <code>start</code> and <code>end</code> also accept ISO-8601
|
||||
timestamps with explicit UTC offsets or Unix epoch seconds. The other URL
|
||||
parameters are <code>tz</code>, <code>freq</code>, <code>bw</code>,
|
||||
timestamps with explicit UTC offsets or Unix epoch seconds. If
|
||||
<code>tz</code> is omitted, the page uses the browser's time zone. The
|
||||
other URL parameters are <code>freq</code>, <code>bw</code>,
|
||||
<code>sf</code>, <code>cr</code>, <code>normalfreq</code>,
|
||||
<code>normalbw</code>, <code>normalsf</code>, <code>normalcr</code>, and
|
||||
estimate-only <code>tx</code>.
|
||||
|
||||
@@ -6,9 +6,10 @@ const tool = require("../docs/_javascript/preset_test.js");
|
||||
const query =
|
||||
"?start=2026-09-21T17:00:00-07:00" +
|
||||
"&end=2026-09-23T17:00:00-07:00" +
|
||||
"&tz=America%2FLos_Angeles" +
|
||||
"&freq=910.1&bw=500&sf=8&cr=7";
|
||||
const config = tool.configFromSearch(query);
|
||||
const defaults = tool.configFromSearch("");
|
||||
const defaults = tool.configFromSearch("", "America/Los_Angeles");
|
||||
|
||||
assert.strictEqual(config.startEpoch, 1790035200);
|
||||
assert.strictEqual(config.endEpoch, 1790208000);
|
||||
@@ -27,6 +28,24 @@ assert.strictEqual(defaults.endEpoch, config.endEpoch);
|
||||
assert.strictEqual(defaults.freq, config.freq);
|
||||
assert.strictEqual(defaults.tz, config.tz);
|
||||
|
||||
const browserZoneFallback = tool.configFromSearch("", "America/New_York");
|
||||
assert.strictEqual(browserZoneFallback.tz, "America/New_York");
|
||||
assert.strictEqual(
|
||||
tool.configFromSearch("?tz=UTC", "America/New_York").tz,
|
||||
"UTC"
|
||||
);
|
||||
assert.doesNotThrow(() => tool.validateTimeZone(tool.browserTimeZone()));
|
||||
const availableZones = tool.supportedTimeZones("America/Los_Angeles");
|
||||
assert.strictEqual(availableZones[0], "UTC");
|
||||
assert.ok(availableZones.includes("America/Los_Angeles"));
|
||||
assert.ok(availableZones.includes("America/New_York"));
|
||||
if (typeof Intl.supportedValuesOf === "function") {
|
||||
assert.strictEqual(
|
||||
availableZones.length,
|
||||
new Set(["UTC", ...Intl.supportedValuesOf("timeZone")]).size
|
||||
);
|
||||
}
|
||||
|
||||
const before = Date.parse("2026-09-21T16:59:00-07:00");
|
||||
const beforeSetup = Date.parse("2026-09-21T15:59:59-07:00");
|
||||
const setupOpens = Date.parse("2026-09-21T16:00:00-07:00");
|
||||
@@ -162,7 +181,8 @@ assert.strictEqual(
|
||||
);
|
||||
|
||||
const changed = tool.configFromSearch(
|
||||
"?start=1790035200&end=1790208000&freq=915.25&bw=125&sf=10&cr=5"
|
||||
"?start=1790035200&end=1790208000&tz=America%2FLos_Angeles" +
|
||||
"&freq=915.25&bw=125&sf=10&cr=5"
|
||||
);
|
||||
assert.strictEqual(
|
||||
tool.commandsFor(changed, changed.startMs).stockNow,
|
||||
|
||||
Reference in new issue
Block a user