Procházet zdrojové kódy

feat: 新增有两种展示方式的时区函数

zhouyuhao před 1 rokem
rodič
revize
cfc9779ca0

+ 2 - 1
src/auto-imports.d.ts

@@ -3,6 +3,7 @@
 // @ts-nocheck
 // noinspection JSUnusedGlobalSymbols
 // Generated by unplugin-auto-import
+// biome-ignore lint: disable
 export {}
 declare global {
   const $api: typeof import('@/api/index')['default']
@@ -68,6 +69,6 @@ declare global {
 // for type re-export
 declare global {
   // @ts-ignore
-  export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
+  export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue'
   import('vue')
 }

+ 16 - 0
src/utils/tools.ts

@@ -1,6 +1,22 @@
 import moment from 'moment-timezone'
 
 export const formatTimezone = (time: string, timezone: string) => {
+  if (!time) return '--'
+  let formattedTime = ''
+  if (time.length > 12) {
+    formattedTime = moment(time).format('MMM-DD-YYYY hh:mm A')
+    let gmtOffset = ''
+    const timeZoneOffset = moment().tz(timezone).format('Z')
+    // 替换 "+07:00" 为 "GMT+07"
+    gmtOffset = `(GMT${timeZoneOffset.slice(0, 3)})`
+    return `${formattedTime} ${gmtOffset}`
+  } else {
+    formattedTime = moment(time).format('MMM-DD-YYYY')
+    return formattedTime
+  }
+}
+
+export const formatTimezoneByUTCorGMT = (time: string, timezone: string) => {
   if (!time) return '--'
   let formattedTime = ''
   if (time.length > 12) {

+ 3 - 3
src/views/Dashboard/src/components/RecentStatus.vue

@@ -1,7 +1,7 @@
 <script lang="ts" setup>
 import { useRouter } from 'vue-router'
 import dayjs from 'dayjs'
-import { formatTimezone } from '@/utils/tools'
+import { formatTimezoneByUTCorGMT } from '@/utils/tools'
 
 const router = useRouter()
 interface RecentItem {
@@ -124,7 +124,7 @@ const RouteToDetail = (val: any) => {
         <VTag :type="item.type" style="margin-bottom: 4px">{{ item.type }}</VTag>
         <div class="right_text">{{ item.Arrived }}</div>
         <div class="startStation_time" v-if="item.Time != null">
-          {{ formatTimezone(item.Time, item.timezone) }}&nbsp;&nbsp;{{ item.timezone }}
+          {{ formatTimezoneByUTCorGMT(item.Time, item.timezone) }}&nbsp;&nbsp;{{ item.timezone }}
         </div>
       </div>
     </div>
@@ -255,4 +255,4 @@ const RouteToDetail = (val: any) => {
 .right_text {
   font-size: var(--font-size-2);
 }
-</style>
+</style>