添加时间处理工具

This commit is contained in:
宫欣海 2025-03-06 18:48:50 +08:00
parent 5e00dd0d25
commit b6551e9bbf
7 changed files with 585 additions and 5 deletions

View File

@ -1,7 +1,6 @@
{
"name": "kunpocc",
"version": "1.0.23",
"type": "module",
"description": "基于creator3.0+的kunpocc库",
"main": "./dist/kunpocc.cjs",
"module": "./dist/kunpocc.mjs",

View File

@ -22,7 +22,17 @@ export default [
plugins: [
typescript({
tsconfig: './tsconfig.json',
importHelpers: false
importHelpers: false,
compilerOptions: {
target: "es6",
module: "es6",
experimentalDecorators: true, // 启用ES装饰器。
strict: true,
strictNullChecks: false,
moduleResolution: "Node",
skipLibCheck: true,
esModuleInterop: true,
}
})
]
},
@ -45,7 +55,17 @@ export default [
plugins: [
typescript({
tsconfig: './tsconfig.json',
importHelpers: false
importHelpers: false,
compilerOptions: {
target: "es6",
module: "es6",
experimentalDecorators: true, // 启用ES装饰器。
strict: true,
strictNullChecks: false,
moduleResolution: "Node",
skipLibCheck: true,
esModuleInterop: true,
}
}),
terser()
]

View File

@ -14,6 +14,7 @@ import { Platform, PlatformType } from "../global/Platform";
import { ECManager } from "../kunpocc";
import { ModuleBase } from "../module/ModuleBase";
import { info } from "../tool/log";
import { Time } from "../tool/Time";
import { PropsHelper } from "../ui/PropsHelper";
import { CocosAdapter } from "./CocosAdapter";
const { property } = _decorator;
@ -109,6 +110,7 @@ export abstract class CocosEntry extends Component {
}
private initTime(): void {
Time._configBoot();
InnerTimer.initTimer();
GlobalTimer.initTimer();
this.schedule(this.tick.bind(this), 0, macro.REPEAT_FOREVER);

View File

@ -10,7 +10,7 @@ export { Binary } from "./tool/Binary";
export * from "./tool/log";
export { MathTool } from "./tool/Math";
export { md5 } from "./tool/MD5";
export { Time } from "./tool/Time";
/** 消息监听 */
export { EventManager } from "./event/EventManager";

View File

@ -4,6 +4,7 @@ const MathFloor = Math.floor;
const MathRandom = Math.random;
export class MathTool {
/** 限制 value 在 min 和 max 之间 */
public static clampf(value: number, min: number, max: number): number {
return MathMin(MathMax(value, min), max);
}
@ -18,14 +19,24 @@ export class MathTool {
return MathRandom() * (max - min) + min;
}
/** 角度转弧度 */
public static rad(angle: number): number {
return (angle * Math.PI) / 180;
}
/** 弧度转角度 */
public static deg(radian: number): number {
return (radian * 180) / Math.PI;
}
/**
*
* @param num1
* @param num2
* @param elapsedTime
* @param responseTime
* @returns
*/
public static smooth(num1: number, num2: number, elapsedTime: number, responseTime: number): number {
let out: number = num1;
if (elapsedTime > 0) {

548
src/tool/Time.ts Normal file
View File

@ -0,0 +1,548 @@
/**
* @Author: Gongxh
* @Date: 2025-03-06
* @Description:
*/
import { game } from "cc";
import { log } from "./log";
/** 时间对象缓存 */
let TimeCache: Date = null;
export class Time {
/** 游戏系统启动时间戳 */
private static _osBootTime: number = 0;
/** 主动设置的网络时间 单位ms */
private static _netTime: number = 0;
/** 本地时间与网路时间的偏移量 单位ms */
private static _netTimeDiff: number = 0;
/** 获取当前毫秒时间戳 */
private static _nowTimestamp: () => number;
/** 获取游戏系统启动时间戳 */
public static get osBootTime(): number { return this._osBootTime; }
/** 获取主动设置的网络时间 单位ms */
public static get netTime(): number { return this._netTime; }
/** 获取本地时间与网路时间的偏移量 单位ms */
public static get netTimeDiff(): number { return this._netTimeDiff; }
/** 获取系统运行时间 */
public static get runTime(): number { return game.totalTime };
public static _configBoot(): void {
this._osBootTime = Date.now();
log("系统启动时间", this._osBootTime);
TimeCache = new Date();
this._nowTimestamp = (): number => {
return this._osBootTime + this.runTime;
}
}
/**
* , ms
* @param netTime
*/
public static setNetTime(netTime: number): void {
if (netTime == 0) {
return;
}
this._netTime = netTime;
const localTime = this._nowTimestamp();
this._netTimeDiff = this.netTime - localTime;
log(`设置网络时间: net(${this.formatTime(this.netTime)}), boot(${this.formatTime(this.osBootTime)}), diff(${Math.abs(this.netTimeDiff / 1000)}秒)`);
}
/**
* ms
*/
public static now(): number {
return this._nowTimestamp() + this.netTimeDiff;
}
/**
*
* @param ms
*/
public static msTos(ms: number): number {
return Math.floor((ms || 0) / 1000);
}
/**
*
*/
public static sToMs(s: number): number {
return (s || 0) * 1000;
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getTime(timestamp?: number): { year: number, month: number, day: number, hour: number, minute: number, second: number } {
TimeCache.setTime(timestamp || this.now());
return {
year: TimeCache.getFullYear(),
month: TimeCache.getMonth() + 1,
day: TimeCache.getDate(),
hour: TimeCache.getHours(),
minute: TimeCache.getMinutes(),
second: TimeCache.getSeconds()
};
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getYear(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
return TimeCache.getFullYear();
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getMonth(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
return TimeCache.getMonth() + 1;
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getDay(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
return TimeCache.getDate();
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getHour(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
return TimeCache.getHours();
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getMinute(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
return TimeCache.getMinutes();
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getSecond(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
return TimeCache.getSeconds();
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getDayStartTime(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
TimeCache.setHours(0, 0, 0, 0);
return TimeCache.getTime();
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getDayEndTime(timestamp?: number): number {
return this.getDayStartTime(timestamp) + 86400000;
}
/**
*
* @param {number} [time] (ms)
* @returns {number}
*/
public static getWeekDay(time?: number): number {
TimeCache.setTime(time || Time.now());
return TimeCache.getDay() || 7;
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getWeekStartTime(timestamp?: number): number {
return this.getDayStartTime(timestamp - this.getWeekDay(timestamp) * 86400000);
}
public static getWeekEndTime(timestamp?: number): number {
return this.getWeekStartTime(timestamp) + 86400000 * 7;
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getMonthStartTime(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
TimeCache.setDate(1);
TimeCache.setHours(0, 0, 0, 0);
return TimeCache.getTime();
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getMonthEndTime(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
TimeCache.setDate(1);
TimeCache.setHours(0, 0, 0, 0);
TimeCache.setMonth(TimeCache.getMonth() + 1);
return TimeCache.getTime();
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getYearStartTime(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
TimeCache.setMonth(0);
TimeCache.setDate(1);
TimeCache.setHours(0, 0, 0, 0);
return TimeCache.getTime();
}
/**
*
* @param timestamp (ms)
* @returns (ms)
*/
public static getYearEndTime(timestamp?: number): number {
TimeCache.setTime(timestamp || this.now());
TimeCache.setMonth(0);
TimeCache.setDate(1);
TimeCache.setHours(0, 0, 0, 0);
TimeCache.setFullYear(TimeCache.getFullYear() + 1);
return TimeCache.getTime();
}
/**
*
* @param timestamp (ms)
* @returns
*/
public static getMonthDays(timestamp?: number): number {
const monthEndTime = this.getMonthEndTime(timestamp);
const monthStartTime = this.getMonthStartTime(timestamp);
return Math.round((monthEndTime - monthStartTime) / 86400000);
}
/**
*
* @param timestamp1 1 (ms)
* @param now 2 (ms)
* @returns
*/
public static isSameDay(timestamp1: number, now?: number): boolean {
now = now || this.now();
if (now - timestamp1 > 86400000) {
return false;
}
return this.getDayStartTime(timestamp1) === this.getDayStartTime(now);
}
/**
*
* @param timestamp1 1 (ms)
* @param now 2 (ms)
* @returns
*/
public static isSameWeek(timestamp1: number, now?: number): boolean {
now = now || this.now();
if (now - timestamp1 > 86400000 * 7) {
return false;
}
return this.getWeekStartTime(timestamp1) === this.getWeekStartTime(now);
}
/**
*
* @param timestamp1 1 (ms)
* @param now 2 (ms)
* @returns
*/
public static isSameMonth(timestamp1: number, now?: number): boolean {
now = now || this.now();
TimeCache.setTime(timestamp1);
const month1 = TimeCache.getMonth();
const year1 = TimeCache.getFullYear();
TimeCache.setTime(now);
const month2 = TimeCache.getMonth();
const year2 = TimeCache.getFullYear();
return month1 === month2 && year1 === year2;
}
/**
*
* @param timestamp1 1 (ms)
* @param now 2 (ms)
* @returns
*/
public static isSameYear(timestamp1: number, now?: number): boolean {
now = now || this.now();
// 直接比较年份,避免使用天数计算可能出现的边界错误
TimeCache.setTime(timestamp1);
const year1 = TimeCache.getFullYear();
TimeCache.setTime(now);
const year2 = TimeCache.getFullYear();
return year1 === year2;
}
/**
* 格式: xxxx-xx-xx HH:MM:SS
* @param timestamp (ms)
*/
public static formatTime(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getFullYear()}-${TimeCache.getMonth() + 1}-${TimeCache.getDate()} ${TimeCache.getHours()}:${TimeCache.getMinutes()}:${TimeCache.getSeconds()}`;
}
/**
* 格式: xxxx年xx月xx日 HH:MM:SS
* @param timestamp (ms)
*/
public static formatTimeChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getFullYear()}${TimeCache.getMonth() + 1}${TimeCache.getDate()}${TimeCache.getHours()}:${TimeCache.getMinutes()}:${TimeCache.getSeconds()}`;
}
/**
* 格式: xxxx-xx-xx hh:mm
* @param timestamp (ms)
*/
public static formatYMDHM(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getFullYear()}-${TimeCache.getMonth() + 1}-${TimeCache.getDate()} ${TimeCache.getHours()}:${TimeCache.getMinutes()}`;
}
/**
* 格式: xxxx年xx月xx日 h时m分
* @param timestamp (ms)
*/
public static formatYMDHMChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getFullYear()}${TimeCache.getMonth() + 1}${TimeCache.getDate()}${TimeCache.getHours()}${TimeCache.getMinutes()}`;
}
/**
* 格式: xxxx-xx-xx
* @param timestamp (ms)
*/
public static formatYMD(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getFullYear()}-${TimeCache.getMonth() + 1}-${TimeCache.getDate()}`;
}
/**
* 格式: xxxx年xx月xx日
* @param timestamp (ms)
*/
public static formatYMDChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getFullYear()}${TimeCache.getMonth() + 1}${TimeCache.getDate()}`;
}
/**
* 格式: xx-xx h:m:s
* @param timestamp (ms)
*/
public static formatMDHMS(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getMonth() + 1}-${TimeCache.getDate()} ${TimeCache.getHours()}:${TimeCache.getMinutes()}:${TimeCache.getSeconds()}`;
}
/**
* 格式: xx月xx日 h时m分s秒
* @param timestamp (ms)
*/
public static formatMDHMSChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getMonth() + 1}${TimeCache.getDate()}${TimeCache.getHours()}${TimeCache.getMinutes()}${TimeCache.getSeconds()}`;
}
/**
* 格式: xx-xx
* @param timestamp (ms)
*/
public static formatMD(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getMonth() + 1}-${TimeCache.getDate()}`;
}
/**
* 格式: xx月xx日
* @param timestamp (ms)
*/
public static formatMDChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getMonth() + 1}${TimeCache.getDate()}`;
}
/**
* 格式: hh:mm
* @param timestamp (ms)
*/
public static formatHM(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getHours()}:${TimeCache.getMinutes()}`;
}
/**
* 格式: h时m分
* @param timestamp (ms)
*/
public static formatHMChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getHours()}${TimeCache.getMinutes()}`;
}
/**
* 格式: hh:mm:ss
* @param timestamp (ms)
*/
public static formatHMS(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getHours()}:${TimeCache.getMinutes()}:${TimeCache.getSeconds()}`;
}
/**
* 格式: h时m分s秒
* @param timestamp (ms)
*/
public static formatHMSChinese(timestamp: number): string {
TimeCache.setTime(timestamp);
return `${TimeCache.getHours()}${TimeCache.getMinutes()}${TimeCache.getSeconds()}`;
}
/**
* >1(x天x小时x分x秒) >1(xx小时x分x秒) 1(xx分x秒) 1(xx秒)
* @param time (s)
*/
public static formatSmart(time: number): string {
const curTime = Math.floor(time < 0 ? 0 : time);
const day = Math.floor(curTime / 86400);
const hour = Math.floor((curTime % 86400) / 3600);
const minute = Math.floor((curTime % 3600) / 60);
const second = curTime % 60;
if (day > 0) {
return `${day}${hour}小时${minute}${second}`;
} else if (hour > 0) {
return `${hour}小时${minute}${second}`;
} else if (minute > 0) {
return `${minute}${second}`;
} else {
return `${second}`;
}
}
/**
* >1(x天x小时) >1(xx小时xx分) 1(xx分xx秒) 1(xx秒)
* @param time (s)
*/
public static formatSmartSimple(time: number): string {
const curTime = Math.floor(time < 0 ? 0 : time);
if (curTime > 86400) {
const day = Math.floor(curTime / 86400);
const hour = Math.ceil((curTime % 86400) / 3600);
return `${day}${hour}小时`;
} else if (curTime > 3600) {
const hour = Math.floor(curTime / 3600);
const minute = Math.ceil((curTime % 3600) / 60);
return `${hour}小时${minute}`;
} else if (curTime > 60) {
const minute = Math.floor(curTime / 60);
const second = Math.ceil(curTime % 60);
return `${minute}${second}`;
} else {
return `${curTime}`;
}
}
/**
* 格式: xx:xx:xx
* @param time (s)
*/
public static formatToHour(time: number): string {
const curTime = time < 0 ? 0 : time;
const timeNum = Math.floor(curTime);
const hour = Math.floor(timeNum / 3600);
const minute = Math.floor((timeNum % 3600) / 60);
const seconds = timeNum % 60;
const hourStr = hour < 10 ? `0${hour}` : `${hour}`;
const minuteStr = minute < 10 ? `0${minute}` : `${minute}`;
const secondsStr = seconds < 10 ? `0${seconds}` : `${seconds}`;
return `${hourStr}:${minuteStr}:${secondsStr}`;
}
/**
* 格式: xx小时xx分xx秒
* @param time (s)
*/
public static formatToHourChinese(time: number): string {
const curTime = time < 0 ? 0 : time;
const timeNum = Math.floor(curTime);
const hour = Math.floor(timeNum / 3600);
const minute = Math.floor((timeNum % 3600) / 60);
const seconds = timeNum % 60;
return `${hour}小时${minute}${seconds}`;
}
/**
* 格式: xx:xx
* @param time (s)
*/
public static formatToMinute(time: number): string {
const curTime = time < 0 ? 0 : time;
const timeNum = Math.floor(curTime);
const minute = Math.floor(timeNum / 60);
const seconds = timeNum % 60;
const minuteStr = minute < 10 ? `0${minute}` : `${minute}`;
const secondsStr = seconds < 10 ? `0${seconds}` : `${seconds}`;
return `${minuteStr}:${secondsStr}`;
}
/**
* 格式: xx分xx秒
* @param time (s)
*/
public static formatToMinuteChinese(time: number): string {
return this.formatToMinute(time).replace(/:/g, "分") + "秒";
}
}

View File

@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es6", //
"module": "es6", //
"module": "commonjs", //
"experimentalDecorators": true, // ES
"strict": true,
"strictNullChecks": false,