diff options
author | 2023-05-23 00:40:12 -0700 | |
---|---|---|
committer | 2023-05-23 00:40:12 -0700 | |
commit | 5b38c55c3db018a94505f61cd785f0dd40f442ac (patch) | |
tree | af522e38ffa9b6c400c500c76de1fdca4ab931db /test/js | |
parent | 83e7b9e198b25c7af7905c5dcabe1e325c5a38fb (diff) | |
download | bun-5b38c55c3db018a94505f61cd785f0dd40f442ac.tar.gz bun-5b38c55c3db018a94505f61cd785f0dd40f442ac.tar.zst bun-5b38c55c3db018a94505f61cd785f0dd40f442ac.zip |
Support setting a timezone with `process.env.TZ` and `Bun.env.TZ` (#3018)
* Support setting a timezone via `process.env.TZ`
* Implement `setTimeZone` in `bun:jsc` module
* [breaking] `bun:test` now defaults to `Etc/UTC` timezone
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/bun/jsc/bun-jsc.test.ts | 30 | ||||
-rw-r--r-- | test/js/node/process/process.test.js | 22 |
2 files changed, 52 insertions, 0 deletions
diff --git a/test/js/bun/jsc/bun-jsc.test.ts b/test/js/bun/jsc/bun-jsc.test.ts index aa93ce90a..f16a6a781 100644 --- a/test/js/bun/jsc/bun-jsc.test.ts +++ b/test/js/bun/jsc/bun-jsc.test.ts @@ -22,6 +22,7 @@ import { reoptimizationRetryCount, drainMicrotasks, startRemoteDebugger, + setTimeZone, } from "bun:jsc"; describe("bun:jsc", () => { @@ -110,4 +111,33 @@ describe("bun:jsc", () => { it("getProtectedObjects", () => { expect(getProtectedObjects().length).toBeGreaterThan(0); }); + + it("setTimeZone", () => { + var origTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; + const realOrigTimezone = origTimezone; + if (origTimezone === "America/Anchorage") { + origTimezone = "America/New_York"; + } + const origDate = new Date(); + origDate.setSeconds(0); + origDate.setMilliseconds(0); + origDate.setMinutes(0); + const origDateString = origDate.toString(); + expect(origTimezone).toBeDefined(); + expect(origTimezone).not.toBe("America/Anchorage"); + expect(setTimeZone("America/Anchorage")).toBe("America/Anchorage"); + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe("America/Anchorage"); + if (realOrigTimezone === origTimezone) { + const newDate = new Date(); + newDate.setSeconds(0); + newDate.setMilliseconds(0); + newDate.setMinutes(0); + const newDateString = newDate.toString(); + expect(newDateString).not.toBe(origDateString); + } + + expect(setTimeZone(realOrigTimezone)).toBe(realOrigTimezone); + + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(origTimezone); + }); }); diff --git a/test/js/node/process/process.test.js b/test/js/node/process/process.test.js index 414392a79..c9f92362c 100644 --- a/test/js/node/process/process.test.js +++ b/test/js/node/process/process.test.js @@ -98,6 +98,28 @@ it("process.env is spreadable and editable", () => { expect(eval(`globalThis.process.env.USER = "${orig}"`)).toBe(orig); }); +it("process.env.TZ", () => { + var origTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; + + // the default timezone is Etc/UTC + if (!"TZ" in process.env) { + expect(origTimezone).toBe("Etc/UTC"); + } + + const realOrigTimezone = origTimezone; + if (origTimezone === "America/Anchorage") { + origTimezone = "America/New_York"; + } + + const target = "America/Anchorage"; + const tzKey = String("TZ" + " ").substring(0, 2); + process.env[tzKey] = target; + expect(process.env[tzKey]).toBe(target); + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(target); + process.env[tzKey] = origTimezone; + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(realOrigTimezone); +}); + it("process.version starts with v", () => { expect(process.version.startsWith("v")).toBeTruthy(); }); |