Skip to content
Open
29 changes: 14 additions & 15 deletions src/lib/components/timezone-select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import ToggleSwitch from '$lib/holocene/toggle-switch.svelte';
import { translate } from '$lib/i18n/translate';
import {
BASE_TIME_FORMAT_OPTIONS,
relativeTime,
type TimeFormat,
timeFormat,
type TimeFormatOptions,
timestampFormat,
TimezoneOptions,
Timezones,
} from '$lib/stores/time-format';
import { capitalize } from '$lib/utilities/format-camel-case';
import {
formatUTCOffset,
getLocalTime,
Expand All @@ -42,9 +41,9 @@
const QuickTimezoneOptions: TimeFormatOptions = [
{
label: translate('common.utc'),
value: 'UTC',
value: BASE_TIME_FORMAT_OPTIONS.UTC,
},
{ label: translate('common.local'), value: 'local' },
{ label: translate('common.local'), value: BASE_TIME_FORMAT_OPTIONS.LOCAL },
];

let search = '';
Expand All @@ -63,27 +62,25 @@
);
});

const selectTimezone = (value: TimeFormat) => {
if ($relativeTime && value !== 'local') $relativeTime = false;
const selectTimezone = (value: string) => {
if ($relativeTime && value !== BASE_TIME_FORMAT_OPTIONS.LOCAL)
$relativeTime = false;
$timeFormat = value;
search = '';
};

const handleRelativeToggle = (e: Event) => {
const target = e.target as HTMLInputElement;
if (target.checked && $timeFormat !== 'local') {
$timeFormat = 'local';
if (target.checked && $timeFormat !== BASE_TIME_FORMAT_OPTIONS.LOCAL) {
$timeFormat = BASE_TIME_FORMAT_OPTIONS.LOCAL;
}
};

const setTimestampFormat = (format: TimestampFormat) => {
$timestampFormat = format;
};

$: timezone =
Timezones[$timeFormat]?.abbr ??
Timezones[$timeFormat]?.label ??
capitalize($timeFormat);
$: timezone = Timezones[$timeFormat ?? '']?.abbr ?? $timeFormat;

openUnsubscriber = open.subscribe((isOpen) => {
if (isOpen) {
Expand All @@ -98,7 +95,7 @@

onMount(() => {
if (String($timeFormat) === 'relative') {
$timeFormat = 'local';
$timeFormat = BASE_TIME_FORMAT_OPTIONS.LOCAL;
$relativeTime = true;
}
});
Expand Down Expand Up @@ -188,9 +185,11 @@
{#each QuickTimezoneOptions as { value, label }}
<MenuItem
onclick={() => selectTimezone(value)}
data-testid={`timezones-${value}`}
data-testid="timezones-{value}"
selected={value === $timeFormat}
description={value === 'local' && localTime}
description={value === BASE_TIME_FORMAT_OPTIONS.LOCAL
? localTime
: undefined}
>
{label}
</MenuItem>
Expand Down
13 changes: 10 additions & 3 deletions src/lib/components/workflow/workflow-filters.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import Option from '$lib/holocene/select/simple-option.svelte';
import Select from '$lib/holocene/select/simple-select.svelte';
import { translate } from '$lib/i18n/translate';
import { timeFormat } from '$lib/stores/time-format';
import {
BASE_TIME_FORMAT_OPTIONS,
timeFormat,
} from '$lib/stores/time-format';
import { toListWorkflowQuery } from '$lib/utilities/query/list-workflow-query';
import { toListWorkflowParameters } from '$lib/utilities/query/to-list-workflow-parameters';
import { durations } from '$lib/utilities/to-duration';
Expand Down Expand Up @@ -148,8 +151,12 @@
label={translate('common.time-format')}
>
<Option value={'relative'}>{translate('common.relative')}</Option>
<Option value={'UTC'}>{translate('common.utc')}</Option>
<Option value={'local'}>{translate('common.local')}</Option>
<Option value={BASE_TIME_FORMAT_OPTIONS.UTC}
>{translate('common.utc')}</Option
>
<Option value={BASE_TIME_FORMAT_OPTIONS.LOCAL}
>{translate('common.local')}</Option
>
</Select>
</div>
{/if}
Expand Down
44 changes: 38 additions & 6 deletions src/lib/stores/time-format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { describe, expect, test } from 'vitest';

import {
formatOffset,
getAdjustedTimeformat,
getTimezone,
getUTCOffset,
relativeTime,
timeFormat,
type TimeFormat,
Timezones,
} from './time-format';

describe('time format store', () => {
test('should return UTC as the default timeFormat', () => {
expect(get(timeFormat)).toBe('UTC');
test('should return local as the default timeFormat', () => {
expect(get(timeFormat)).toBe('local');
});
test('should return false as the default for relativeTime', () => {
expect(get(relativeTime)).toBe(false);
Expand All @@ -23,7 +23,7 @@ describe('time format store', () => {

describe('getTimezone', () => {
test('should return the first zone for the specified time format in the Timezones object', () => {
expect(getTimezone('Pacific Daylight Time')).toBe('America/Los_Angeles');
expect(getTimezone('Central Standard Time')).toBe('America/Bahia_Banderas');
expect(getTimezone('Greenwich Mean Time')).toBe('Africa/Abidjan');
});

Expand Down Expand Up @@ -54,7 +54,7 @@ describe('formatOffset', () => {
describe('getUTCOffset', () => {
test('should return a formatted UTC offset for all Timezone options', () => {
Object.entries(Timezones).forEach(([format, { offset }]) => {
expect(getUTCOffset(format as TimeFormat)).toBe(formatOffset(offset));
expect(getUTCOffset(format)).toBe(formatOffset(offset));
});
});

Expand All @@ -63,6 +63,38 @@ describe('getUTCOffset', () => {
});

test('should return a formatted UTC offset for a timezone', () => {
expect(getUTCOffset('America/Phoenix' as TimeFormat)).toBe('-07:00');
expect(getUTCOffset('America/Phoenix')).toBe('-07:00');
});
});

describe('getAdjustedTimeformat', () => {
test('should replace daylight with standard and vice versa', () => {
expect(
getAdjustedTimeformat('Mountain Daylight Time', {
'Mountain Standard Time': { abbr: 'MST', offset: -7, zones: [] },
}),
).toBe('Mountain Standard Time');
expect(
getAdjustedTimeformat('Mountain Standard Time', {
'Mountain Daylight Time': { abbr: 'MDT', offset: -7, zones: [] },
}),
).toBe('Mountain Daylight Time');
});

test('should replace standard with summer and vice versa', () => {
expect(
getAdjustedTimeformat('Central European Summer Time', {
'Central European Standard Time': {
abbr: 'GMT+1',
offset: 1,
zones: [],
},
}),
).toBe('Central European Standard Time');
expect(
getAdjustedTimeformat('Central European Standard Time', {
'Central European Summer Time': { abbr: 'GMT+2', offset: 2, zones: [] },
}),
).toBe('Central European Summer Time');
});
});
Loading
Loading