mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-11 17:46:41 -05:00
Fix test with timezone
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Date>;
|
||||
}): 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 });
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user