|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +import { test, expect } from "@perspective-dev/test"; |
| 14 | +import perspective from "../perspective_client"; |
| 15 | + |
| 16 | +const JAN_1_2024_UTC = 1704067200000; |
| 17 | + |
| 18 | +test.describe("Date timezone invariance", function () { |
| 19 | + let SAVED_TZ: string | undefined; |
| 20 | + |
| 21 | + test.beforeEach(() => { |
| 22 | + SAVED_TZ = process.env.TZ; |
| 23 | + process.env.TZ = "Europe/Amsterdam"; |
| 24 | + }); |
| 25 | + |
| 26 | + test.afterEach(() => { |
| 27 | + process.env.TZ = SAVED_TZ; |
| 28 | + }); |
| 29 | + |
| 30 | + test("date strings serialize to UTC midnight epoch ms", async function () { |
| 31 | + const table = await perspective.table({ d: "date" }); |
| 32 | + await table.update({ d: ["2024-01-01"] }); |
| 33 | + const view = await table.view(); |
| 34 | + const cols = (await view.to_columns()) as { d: number[] }; |
| 35 | + expect(cols.d).toEqual([JAN_1_2024_UTC]); |
| 36 | + await view.delete(); |
| 37 | + await table.delete(); |
| 38 | + }); |
| 39 | + |
| 40 | + test("date epoch ms input round-trips exactly", async function () { |
| 41 | + const table = await perspective.table({ d: "date" }); |
| 42 | + await table.update({ d: [JAN_1_2024_UTC] }); |
| 43 | + const view = await table.view(); |
| 44 | + const cols = (await view.to_columns()) as { d: number[] }; |
| 45 | + expect(cols.d).toEqual([JAN_1_2024_UTC]); |
| 46 | + await view.delete(); |
| 47 | + await table.delete(); |
| 48 | + }); |
| 49 | + |
| 50 | + test("formatted output prints the stored calendar day", async function () { |
| 51 | + const table = await perspective.table({ d: "date" }); |
| 52 | + await table.update({ d: ["2024-01-01"] }); |
| 53 | + const view = await table.view(); |
| 54 | + expect(await view.to_columns_string({ formatted: true })).toEqual( |
| 55 | + '{"d":["2024-01-01"]}', |
| 56 | + ); |
| 57 | + expect(await view.to_csv()).toEqual('"d"\n2024-01-01\n'); |
| 58 | + await view.delete(); |
| 59 | + await table.delete(); |
| 60 | + }); |
| 61 | + |
| 62 | + test("day_bucket stays on the UTC calendar day at the day boundary", async function () { |
| 63 | + const table = await perspective.table({ t: "datetime" }); |
| 64 | + |
| 65 | + // 23:59 UTC is already the next day in Europe/Amsterdam |
| 66 | + await table.update({ t: ["2020-01-31 23:59:00"] }); |
| 67 | + const view = await table.view({ |
| 68 | + expressions: { bucket: `bucket("t", 'D')` }, |
| 69 | + }); |
| 70 | + |
| 71 | + const cols = (await view.to_columns()) as { bucket: number[] }; |
| 72 | + |
| 73 | + // 2020-01-31T00:00:00Z |
| 74 | + expect(cols.bucket).toEqual([1580428800000]); |
| 75 | + await view.delete(); |
| 76 | + await table.delete(); |
| 77 | + }); |
| 78 | + |
| 79 | + test("day_of_week and hour_of_day compute in UTC", async function () { |
| 80 | + const table = await perspective.table({ t: "datetime" }); |
| 81 | + await table.update({ t: ["2020-01-31 23:59:00"] }); |
| 82 | + const view = await table.view({ |
| 83 | + expressions: { |
| 84 | + dow: `day_of_week("t")`, |
| 85 | + hod: `hour_of_day("t")`, |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + const cols = (await view.to_columns()) as { |
| 90 | + dow: string[]; |
| 91 | + hod: number[]; |
| 92 | + }; |
| 93 | + |
| 94 | + // Friday 23:00 UTC, not Saturday 00:59 Amsterdam |
| 95 | + expect(cols.dow).toEqual(["6 Friday"]); |
| 96 | + expect(cols.hod).toEqual([23]); |
| 97 | + await view.delete(); |
| 98 | + await table.delete(); |
| 99 | + }); |
| 100 | + |
| 101 | + test("JSON date output agrees with Arrow date32 day arithmetic", async function () { |
| 102 | + const table = await perspective.table({ d: "date" }); |
| 103 | + await table.update({ d: ["2024-01-01"] }); |
| 104 | + const view = await table.view(); |
| 105 | + const cols = (await view.to_columns()) as { d: number[] }; |
| 106 | + expect(cols.d[0] % 86400000).toEqual(0); |
| 107 | + expect(cols.d[0] / 86400000).toEqual(19723); // days since epoch |
| 108 | + await view.delete(); |
| 109 | + await table.delete(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments