Skip to content

Commit

Permalink
Use UTC to fill date in entry slug
Browse files Browse the repository at this point in the history
Fix #278
  • Loading branch information
kyoshino committed Dec 20, 2024
1 parent a781ffc commit 0af8ab3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/services/contents/entry/slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const fillSlugTemplate = (
currentSlug,
entryFilePath,
locale,
dateTimeParts = getDateTimeParts(),
dateTimeParts = getDateTimeParts({ timeZone: 'UTC' }),
},
) => {
const {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/services/contents/entry/slug.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDateTimeParts } from '@sveltia/utils/datetime';
import { describe, expect, test } from 'vitest';
import { siteConfig } from '$lib/services/config';
import { fillSlugTemplate } from '$lib/services/contents/entry/slug';
Expand Down Expand Up @@ -45,6 +46,16 @@ describe('Test fillSlugTemplate()', () => {
);
});

test('date/time', () => {
// The time zone should be UTC, not local
const { year, month, day, hour, minute, second } = getDateTimeParts({ timeZone: 'UTC' });
const template = '{{year}}-{{month}}-{{day}}-{{hour}}-{{minute}}-{{second}}';

expect(fillSlugTemplate(template, { collection, content: {} })).toEqual(
`${year}-${month}-${day}-${hour}-${minute}-${second}`,
);
});

test('random ID fallback', () => {
expect(fillSlugTemplate('{{title}}', { collection, content: {} })).toMatch(/[0-9a-f]{12}/);
expect(fillSlugTemplate('{{name}}', { collection, content: {} })).toMatch(/[0-9a-f]{12}/);
Expand Down

0 comments on commit 0af8ab3

Please sign in to comment.