Fixed RSS date/time
All checks were successful
Docker Deploy / build-and-push (push) Successful in 4m20s
All checks were successful
Docker Deploy / build-and-push (push) Successful in 4m20s
This commit is contained in:
@@ -1,6 +1,38 @@
|
||||
import rss from '@astrojs/rss';
|
||||
import { getCollection } from 'astro:content';
|
||||
|
||||
function formatPubDate(date) {
|
||||
const timezone = process.env.PUBLIC_RSS_TIMEZONE
|
||||
? process.env.PUBLIC_RSS_TIMEZONE
|
||||
: import.meta.env.PUBLIC_RSS_TIMEZONE;
|
||||
|
||||
if (!timezone) {
|
||||
return date;
|
||||
}
|
||||
|
||||
try {
|
||||
const year = date.getUTCFullYear();
|
||||
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getUTCDate()).padStart(2, '0');
|
||||
|
||||
const formatter = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone: timezone,
|
||||
timeZoneName: 'longOffset',
|
||||
});
|
||||
|
||||
const parts = formatter.formatToParts(date);
|
||||
const offsetPart = parts.find(p => p.type === 'timeZoneName');
|
||||
const offset = offsetPart ? offsetPart.value.replace('GMT', '') : '+00:00';
|
||||
|
||||
const dateStr = `${year}-${month}-${day}T00:00:00${offset}`;
|
||||
|
||||
return new Date(dateStr);
|
||||
} catch (e) {
|
||||
console.warn(`Invalid timezone "${timezone}":`, e.message);
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(context) {
|
||||
const posts = await getCollection('posts');
|
||||
|
||||
@@ -10,7 +42,7 @@ export async function GET(context) {
|
||||
site: context.site,
|
||||
items: posts.map((post) => ({
|
||||
title: post.data.title,
|
||||
pubDate: post.data.pubDate,
|
||||
pubDate: formatPubDate(post.data.pubDate),
|
||||
description: post.data.description || '',
|
||||
link: `/post/${post.slug}/`,
|
||||
})),
|
||||
|
||||
Reference in New Issue
Block a user