2022-06-22 14:57:09 +08:00
|
|
|
import type * as React from 'react';
|
2019-01-09 20:38:09 +08:00
|
|
|
|
|
|
|
export type valueType = number | string;
|
2023-01-05 14:07:33 +08:00
|
|
|
// countdownValueType is deprecated but still support
|
|
|
|
export type countdownValueType = number | string;
|
2019-01-09 20:38:09 +08:00
|
|
|
|
|
|
|
export type Formatter =
|
|
|
|
| false
|
|
|
|
| 'number'
|
|
|
|
| 'countdown'
|
|
|
|
| ((value: valueType, config?: FormatConfig) => React.ReactNode);
|
|
|
|
|
|
|
|
export interface FormatConfig {
|
|
|
|
formatter?: Formatter;
|
|
|
|
decimalSeparator?: string;
|
2019-01-10 17:04:14 +08:00
|
|
|
groupSeparator?: string;
|
2019-01-09 20:38:09 +08:00
|
|
|
precision?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CountdownFormatConfig extends FormatConfig {
|
|
|
|
format?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Countdown
|
|
|
|
const timeUnits: [string, number][] = [
|
|
|
|
['Y', 1000 * 60 * 60 * 24 * 365], // years
|
|
|
|
['M', 1000 * 60 * 60 * 24 * 30], // months
|
|
|
|
['D', 1000 * 60 * 60 * 24], // days
|
|
|
|
['H', 1000 * 60 * 60], // hours
|
|
|
|
['m', 1000 * 60], // minutes
|
|
|
|
['s', 1000], // seconds
|
|
|
|
['S', 1], // million seconds
|
|
|
|
];
|
|
|
|
|
2019-07-24 15:07:10 +08:00
|
|
|
export function formatTimeStr(duration: number, format: string) {
|
2019-01-09 20:38:09 +08:00
|
|
|
let leftDuration: number = duration;
|
|
|
|
|
2020-04-03 15:02:11 +08:00
|
|
|
const escapeRegex = /\[[^\]]*]/g;
|
2022-11-19 13:47:33 +08:00
|
|
|
const keepList: string[] = (format.match(escapeRegex) || []).map((str) => str.slice(1, -1));
|
2019-07-24 15:07:10 +08:00
|
|
|
const templateText = format.replace(escapeRegex, '[]');
|
|
|
|
|
|
|
|
const replacedText = timeUnits.reduce((current, [name, unit]) => {
|
2022-10-21 11:45:55 +08:00
|
|
|
if (current.includes(name)) {
|
2019-01-09 20:38:09 +08:00
|
|
|
const value = Math.floor(leftDuration / unit);
|
|
|
|
leftDuration -= value * unit;
|
|
|
|
return current.replace(new RegExp(`${name}+`, 'g'), (match: string) => {
|
|
|
|
const len = match.length;
|
2022-11-28 00:20:50 +08:00
|
|
|
return value.toString().padStart(len, '0');
|
2019-01-09 20:38:09 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return current;
|
2019-07-24 15:07:10 +08:00
|
|
|
}, templateText);
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
return replacedText.replace(escapeRegex, () => {
|
|
|
|
const match = keepList[index];
|
|
|
|
index += 1;
|
|
|
|
return match;
|
|
|
|
});
|
2019-01-09 20:38:09 +08:00
|
|
|
}
|
|
|
|
|
2023-01-05 14:07:33 +08:00
|
|
|
export function formatCountdown(value: valueType, config: CountdownFormatConfig) {
|
2019-01-09 20:38:09 +08:00
|
|
|
const { format = '' } = config;
|
2020-01-27 10:08:02 +08:00
|
|
|
const target = new Date(value).getTime();
|
|
|
|
const current = Date.now();
|
2019-01-09 20:38:09 +08:00
|
|
|
const diff = Math.max(target - current, 0);
|
|
|
|
|
|
|
|
return formatTimeStr(diff, format);
|
|
|
|
}
|