mirror of https://github.com/YunYouJun/valaxy
fix: date timezone test
This commit is contained in:
parent
072290535a
commit
1f48f64179
|
@ -4,14 +4,14 @@ import { computed } from 'vue'
|
|||
|
||||
const props = defineProps<{
|
||||
date?: string | number | Date
|
||||
format?: string
|
||||
template?: string
|
||||
timezone?: string
|
||||
keepLocalTime?: boolean
|
||||
}>()
|
||||
|
||||
const formattedDate = computed(() => {
|
||||
return formatDate(props.date, {
|
||||
formatStr: props.format,
|
||||
template: props.template,
|
||||
timezone: props.timezone,
|
||||
keepLocalTime: props.keepLocalTime,
|
||||
})
|
||||
|
|
|
@ -5,6 +5,8 @@ import { addonAlgolia } from 'valaxy-addon-algolia'
|
|||
import { addonComponents } from 'valaxy-addon-components'
|
||||
import { addonGitLog } from 'valaxy-addon-git-log'
|
||||
|
||||
import pkg from '../packages/valaxy/package.json'
|
||||
|
||||
const COMMIT_ID = process.env.CF_PAGES_COMMIT_SHA || process.env.COMMIT_REF
|
||||
const commitRef = COMMIT_ID?.slice(0, 8) || 'dev'
|
||||
|
||||
|
@ -170,6 +172,15 @@ export default defineValaxyConfig<PressTheme.Config>({
|
|||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text: pkg.version,
|
||||
items: [
|
||||
{
|
||||
text: 'Release Notes',
|
||||
link: 'https://github.com/YunYouJun/valaxy/releases',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
footer: {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
export const dateExamples: {
|
||||
date: string
|
||||
format: string
|
||||
template: string
|
||||
timezone: string
|
||||
keepLocalTime?: boolean
|
||||
expected: string
|
||||
|
@ -8,19 +8,19 @@ export const dateExamples: {
|
|||
// simple
|
||||
{
|
||||
date: '2023-07-19',
|
||||
format: 'YYYYMMDD',
|
||||
template: 'YYYYMMDD',
|
||||
timezone: 'Asia/Shanghai',
|
||||
expected: '20230719',
|
||||
},
|
||||
{
|
||||
date: '2021-03-01T12:00:00',
|
||||
format: 'YYYYMMDDHHmmss',
|
||||
template: 'YYYYMMDDHHmmss',
|
||||
timezone: 'Asia/Shanghai',
|
||||
expected: '20210301120000',
|
||||
},
|
||||
{
|
||||
date: '2021-12-03T01:07:00',
|
||||
format: 'YYYY/MM/DD HH:mm',
|
||||
template: 'YYYY/MM/DD HH:mm',
|
||||
timezone: 'Asia/Shanghai',
|
||||
expected: '2021/12/03 01:07',
|
||||
},
|
||||
|
@ -28,13 +28,13 @@ export const dateExamples: {
|
|||
// timezone
|
||||
{
|
||||
date: '2004-06-16 00:00:00',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Asia/Shanghai',
|
||||
expected: '2004-06-16 00:00:00+08:00',
|
||||
},
|
||||
{
|
||||
date: '2004-06-16 00:00:00',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Asia/Shanghai',
|
||||
keepLocalTime: true,
|
||||
expected: '2004-06-16 00:00:00+08:00',
|
||||
|
@ -42,13 +42,13 @@ export const dateExamples: {
|
|||
|
||||
{
|
||||
date: '2004-06-16T00:00:00Z',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Asia/Shanghai',
|
||||
expected: '2004-06-16 08:00:00+08:00',
|
||||
},
|
||||
{
|
||||
date: '2004-06-16T00:00:00Z',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Asia/Shanghai',
|
||||
keepLocalTime: true,
|
||||
expected: '2004-06-16 08:00:00+08:00',
|
||||
|
@ -56,13 +56,13 @@ export const dateExamples: {
|
|||
|
||||
{
|
||||
date: '2004-06-16 00:00:00',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Europe/Berlin',
|
||||
expected: '2004-06-15 18:00:00+02:00',
|
||||
},
|
||||
{
|
||||
date: '2004-06-16 00:00:00',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Europe/Berlin',
|
||||
keepLocalTime: true,
|
||||
expected: '2004-06-16 00:00:00+02:00',
|
||||
|
@ -70,13 +70,13 @@ export const dateExamples: {
|
|||
|
||||
{
|
||||
date: '2004-06-16T00:00:00',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Europe/Berlin',
|
||||
expected: '2004-06-15 18:00:00+02:00',
|
||||
},
|
||||
{
|
||||
date: '2004-06-16T00:00:00',
|
||||
format: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
template: 'YYYY-MM-DD HH:mm:ssZ',
|
||||
timezone: 'Europe/Berlin',
|
||||
keepLocalTime: true,
|
||||
expected: '2004-06-16 00:00:00+02:00',
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<script lang="ts" setup>
|
||||
import type { Post } from 'valaxy'
|
||||
import { formatDate, sortByDate, useSiteConfig } from 'valaxy'
|
||||
import { formatDate, sortByDate } from 'valaxy'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
|
@ -13,8 +13,6 @@ const { t } = useI18n()
|
|||
const years = ref<number[]>([])
|
||||
const postListByYear = ref<Record<string, Post[]>>({})
|
||||
|
||||
const siteConfig = useSiteConfig()
|
||||
|
||||
watch(() => props.posts, () => {
|
||||
postListByYear.value = {}
|
||||
years.value = []
|
||||
|
@ -22,7 +20,9 @@ watch(() => props.posts, () => {
|
|||
if (post.hide && post.hide !== 'index')
|
||||
return
|
||||
if (post.date) {
|
||||
const year = Number.parseInt(formatDate(post.date, 'yyyy', siteConfig.value.timezone))
|
||||
const year = Number.parseInt(formatDate(post.date, {
|
||||
template: 'YYYY',
|
||||
}))
|
||||
if (postListByYear.value[year]) {
|
||||
postListByYear.value[year].push(post)
|
||||
}
|
||||
|
|
|
@ -41,7 +41,9 @@ useMotion(itemRef, {
|
|||
>
|
||||
<div class="post-meta">
|
||||
<time v-if="post.date" class="post-time" font="mono" opacity="80">{{
|
||||
formatDate(post.date, 'MM-dd') }}
|
||||
formatDate(post.date, {
|
||||
template: 'MM-DD',
|
||||
}) }}
|
||||
</time>
|
||||
</div>
|
||||
<h2 class="post-title w-full" inline-flex items-center font="serif black">
|
||||
|
|
|
@ -18,5 +18,7 @@ export function onImgError(e: Event, defaultImg = noneImg) {
|
|||
* @param date
|
||||
*/
|
||||
export function formatTimestamp(date: string | number | Date): string {
|
||||
return formatDate(date, 'YYYY-MM-DD HH:mm:ss')
|
||||
return formatDate(date, {
|
||||
template: 'YYYY-MM-DD HH:mm:ss',
|
||||
})
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ dayjs.tz.setDefault(cnTimezone)
|
|||
* format the date (dayjs)
|
||||
*/
|
||||
export function formatDate(date?: string | number | Date, options: {
|
||||
formatStr?: string
|
||||
template?: string
|
||||
timezone?: string
|
||||
keepLocalTime?: boolean
|
||||
} = {}) {
|
||||
return dayjs(date).tz(options.timezone, options.keepLocalTime).format(options.formatStr || 'YYYY-MM-DD')
|
||||
return dayjs(date).tz(options.timezone, options.keepLocalTime).format(options.template || 'YYYY-MM-DD')
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -73,7 +73,7 @@ export interface SiteConfig {
|
|||
* @zh_CN 时区配置,国内推荐使用 'Asia/Shanghai'
|
||||
* @description:en-US This configuration is used to generate times with timezone when no timezone is set
|
||||
* @description:zh-CN 当时间没有设置时区时,使用该配置生成带时区的时间
|
||||
* @default ''
|
||||
* @default 'Asia/Shanghai'
|
||||
*/
|
||||
timezone: string
|
||||
url: string
|
||||
|
|
Loading…
Reference in New Issue