@peng_kai/kit 0.2.35 → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,9 +11,11 @@ const props = withDefaults(
11
11
  defineProps<{
12
12
  timestamp?: number | string
13
13
  template?: string
14
+ utc?: boolean
14
15
  }>(),
15
16
  {
16
17
  template: 'MM-DD HH:mm:ss',
18
+ utc: false,
17
19
  },
18
20
  );
19
21
  const timestamp = computed(() => {
@@ -28,15 +30,24 @@ const timestamp = computed(() => {
28
30
 
29
31
  return Number.isNaN(tsNum) ? undefined : tsNum;
30
32
  });
33
+ const text = computed(() => {
34
+ if (!timestamp.value)
35
+ return '-';
36
+
37
+ const ts = dayjs(timestamp.value);
38
+
39
+ return props.utc ? ts.utc().format(props.template) : ts.format(props.template);
40
+ });
31
41
  </script>
32
42
 
33
43
  <template>
34
44
  <ATooltip destroyTooltipOnHide>
35
45
  <template v-if="timestamp" #title>
36
46
  <div>{{ dayjs(timestamp).fromNow?.() }}</div>
37
- <div>{{ dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss') }}</div>
47
+ <div><span class="inline-block w-2em">本地</span> {{ dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss') }}</div>
48
+ <div><span class="inline-block w-2em">UTC</span> {{ dayjs(timestamp).utc().format('YYYY-MM-DD HH:mm:ss') }}</div>
38
49
  </template>
39
- <span v-bind="$attrs" class="text">{{ timestamp ? dayjs(timestamp).format(props.template) : '-' }}</span>
50
+ <span v-bind="$attrs" class="text">{{ text }}</span>
40
51
  </ATooltip>
41
52
  </template>
42
53
 
package/libs/dayjs.ts CHANGED
@@ -3,6 +3,8 @@ import dayjs from 'dayjs/esm';
3
3
  import relativeTime from 'dayjs/esm/plugin/relativeTime';
4
4
  import weekday from 'dayjs/esm/plugin/weekday';
5
5
  import localeData from 'dayjs/esm/plugin/localeData';
6
+ import utc from 'dayjs/esm/plugin/utc';
7
+ import tz from 'dayjs/esm/plugin/timezone';
6
8
  import 'dayjs/esm/locale/zh';
7
9
  import 'dayjs/esm/locale/en';
8
10
 
@@ -13,3 +15,5 @@ dayjs.locale('zh');
13
15
  dayjs.extend(relativeTime);
14
16
  dayjs.extend(weekday);
15
17
  dayjs.extend(localeData);
18
+ dayjs.extend(utc);
19
+ dayjs.extend(tz);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.2.35",
4
+ "version": "0.2.36",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",
package/utils/date.ts CHANGED
@@ -20,6 +20,29 @@ export function endOfDay(day: dayjs.ConfigType) {
20
20
  return _day.isValid() ? _day.endOf('day').valueOf() : undefined;
21
21
  }
22
22
 
23
+ /**
24
+ * 将时间范围转换为各自当天时间戳范围。
25
+ * @param range - 要转换的时间范围。
26
+ * @param fields - 转换后的时间戳范围的字段名称。
27
+ * @param unit - 要使用的时间单位。
28
+ * @param utc - 是否使用 UTC 时间。
29
+ */
30
+ export function rangeOf<F extends string>(
31
+ range: [dayjs.ConfigType, dayjs.ConfigType],
32
+ fields: [F, F],
33
+ unit: dayjs.OpUnitType = 'day',
34
+ utc = true,
35
+ ) {
36
+ const start = range[0] && dayjs(range[0]);
37
+ const end = range[1] && dayjs(range[1]);
38
+ const region = utc ? 'utc' : 'local';
39
+
40
+ return {
41
+ [fields[0]]: (start && start.isValid()) ? start[region]().startOf(unit).valueOf() : undefined,
42
+ [fields[1]]: (end && end.isValid()) ? end[region]().endOf(unit).valueOf() : undefined,
43
+ } as Record<F, number | undefined>;
44
+ }
45
+
23
46
  /**
24
47
  * 将一个值转换为 Day.js 实例。
25
48
  * @param day - 要转换的值。