Fix test with timezone

This commit is contained in:
Gregory Schier
2025-09-25 08:03:07 -07:00
parent 3c438b3da7
commit b77f1375fd
4 changed files with 21 additions and 6 deletions

7
package-lock.json generated
View File

@@ -812,6 +812,12 @@
"w3c-keyname": "^2.2.4" "w3c-keyname": "^2.2.4"
} }
}, },
"node_modules/@date-fns/tz": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/@date-fns/tz/-/tz-1.4.1.tgz",
"integrity": "sha512-P5LUNhtbj6YfI3iJjw5EL9eUAG6OitD0W3fWQcpQjDRc/QIsL0tRNuO1PcDvPccWL1fSTXXdE1ds+l95DV/OFA==",
"license": "MIT"
},
"node_modules/@esbuild/aix-ppc64": { "node_modules/@esbuild/aix-ppc64": {
"version": "0.25.6", "version": "0.25.6",
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.6.tgz", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.6.tgz",
@@ -18949,6 +18955,7 @@
"name": "@yaak/template-function-timestamp", "name": "@yaak/template-function-timestamp",
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@date-fns/tz": "^1.4.1",
"date-fns": "^4.1.0" "date-fns": "^4.1.0"
} }
}, },

View File

@@ -5,10 +5,11 @@
"scripts": { "scripts": {
"build": "yaakcli build", "build": "yaakcli build",
"dev": "yaakcli dev", "dev": "yaakcli dev",
"lint":"tsc --noEmit && eslint . --ext .ts,.tsx", "lint": "tsc --noEmit && eslint . --ext .ts,.tsx",
"test": "vitest --run tests" "test": "vitest --run tests"
}, },
"dependencies": { "dependencies": {
"@date-fns/tz": "^1.4.1",
"date-fns": "^4.1.0" "date-fns": "^4.1.0"
} }
} }

View File

@@ -1,6 +1,7 @@
import type { TemplateFunctionArg } from '@yaakapp-internal/plugins'; import type { TemplateFunctionArg } from '@yaakapp-internal/plugins';
import type { PluginDefinition } from '@yaakapp/api'; import type { PluginDefinition } from '@yaakapp/api';
import type { ContextFn } from 'date-fns';
import { import {
addDays, addDays,
addHours, addHours,
@@ -24,7 +25,8 @@ const dateArg: TemplateFunctionArg = {
name: 'date', name: 'date',
label: 'Timestamp', label: 'Timestamp',
optional: true, optional: true,
description: 'Can be a timestamp in milliseconds, ISO string, or anything parseable by JS `new Date()`', description:
'Can be a timestamp in milliseconds, ISO string, or anything parseable by JS `new Date()`',
placeholder: new Date().toISOString(), placeholder: new Date().toISOString(),
}; };
@@ -148,8 +150,12 @@ export function calculateDatetime(args: { date?: string; expression?: string }):
return jsDate.toISOString(); return jsDate.toISOString();
} }
export function formatDatetime(args: { date?: string; format?: string }): string { export function formatDatetime(args: {
date?: string;
format?: string;
in?: ContextFn<Date>;
}): string {
const { date, format = 'yyyy-MM-dd HH:mm:ss' } = args; const { date, format = 'yyyy-MM-dd HH:mm:ss' } = args;
const d = parseDateString(date ?? ''); const d = parseDateString(date ?? '');
return formatDate(d, String(format)); return formatDate(d, String(format), { in: args.in });
} }

View File

@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { calculateDatetime, formatDatetime } from '../src'; import { calculateDatetime, formatDatetime } from '../src';
import { tz } from "@date-fns/tz";
describe('formatDatetime', () => { describe('formatDatetime', () => {
it('returns formatted current date', () => { it('returns formatted current date', () => {
@@ -13,12 +14,12 @@ describe('formatDatetime', () => {
}); });
it('returns formatted specific timestamp', () => { it('returns formatted specific timestamp', () => {
const result = formatDatetime({ date: '1752435296000' }); const result = formatDatetime({ date: '1752435296000', in: tz('America/Vancouver') });
expect(result).toBe('2025-07-13 12:34:56'); expect(result).toBe('2025-07-13 12:34:56');
}); });
it('returns formatted specific timestamp with decimals', () => { it('returns formatted specific timestamp with decimals', () => {
const result = formatDatetime({ date: '1752435296000.19' }); const result = formatDatetime({ date: '1752435296000.19', in: tz('America/Vancouver') });
expect(result).toBe('2025-07-13 12:34:56'); expect(result).toBe('2025-07-13 12:34:56');
}); });