diff --git a/package-lock.json b/package-lock.json index 5394e42e..e22e46c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -812,6 +812,12 @@ "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": { "version": "0.25.6", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.6.tgz", @@ -18949,6 +18955,7 @@ "name": "@yaak/template-function-timestamp", "version": "0.1.0", "dependencies": { + "@date-fns/tz": "^1.4.1", "date-fns": "^4.1.0" } }, diff --git a/plugins/template-function-timestamp/package.json b/plugins/template-function-timestamp/package.json index d11ee831..fb3e4f2f 100755 --- a/plugins/template-function-timestamp/package.json +++ b/plugins/template-function-timestamp/package.json @@ -5,10 +5,11 @@ "scripts": { "build": "yaakcli build", "dev": "yaakcli dev", - "lint":"tsc --noEmit && eslint . --ext .ts,.tsx", + "lint": "tsc --noEmit && eslint . --ext .ts,.tsx", "test": "vitest --run tests" }, "dependencies": { + "@date-fns/tz": "^1.4.1", "date-fns": "^4.1.0" } } diff --git a/plugins/template-function-timestamp/src/index.ts b/plugins/template-function-timestamp/src/index.ts index b1647403..20d5bf27 100755 --- a/plugins/template-function-timestamp/src/index.ts +++ b/plugins/template-function-timestamp/src/index.ts @@ -1,6 +1,7 @@ import type { TemplateFunctionArg } from '@yaakapp-internal/plugins'; import type { PluginDefinition } from '@yaakapp/api'; +import type { ContextFn } from 'date-fns'; import { addDays, addHours, @@ -24,7 +25,8 @@ const dateArg: TemplateFunctionArg = { name: 'date', label: 'Timestamp', 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(), }; @@ -148,8 +150,12 @@ export function calculateDatetime(args: { date?: string; expression?: string }): return jsDate.toISOString(); } -export function formatDatetime(args: { date?: string; format?: string }): string { +export function formatDatetime(args: { + date?: string; + format?: string; + in?: ContextFn; +}): string { const { date, format = 'yyyy-MM-dd HH:mm:ss' } = args; const d = parseDateString(date ?? ''); - return formatDate(d, String(format)); + return formatDate(d, String(format), { in: args.in }); } diff --git a/plugins/template-function-timestamp/tests/formatDatetime.test.ts b/plugins/template-function-timestamp/tests/formatDatetime.test.ts index 549b574a..b53b560c 100644 --- a/plugins/template-function-timestamp/tests/formatDatetime.test.ts +++ b/plugins/template-function-timestamp/tests/formatDatetime.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest'; import { calculateDatetime, formatDatetime } from '../src'; +import { tz } from "@date-fns/tz"; describe('formatDatetime', () => { it('returns formatted current date', () => { @@ -13,12 +14,12 @@ describe('formatDatetime', () => { }); 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'); }); 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'); });