This commit is contained in:
lixiao 2025-09-25 08:53:25 +08:00
parent 89f2b20df5
commit 1baa3dd861
7 changed files with 58 additions and 353 deletions

View File

@ -1,6 +1,6 @@
import dayTs from '../../../utils/Date'
import { CusCalendar } from './Calendar'
import { promptAction } from '@kit.ArkUI'
import { dateFormat } from '../../../utils/Utils'
@CustomDialog
@Component
@ -112,7 +112,7 @@ export struct DateRangePicker {
setup() {
if (this.start && this.end) {
this.value = [dayTs(this.start).format(this.format), dayTs(this.end).format(this.format)]
this.value = [dateFormat(this.start, this.format), dateFormat(this.end, this.format)]
}
}

View File

@ -13,7 +13,7 @@ export struct CusInput {
bottom: 4,
right: 8
})
.backgroundColor(0xeeeeee)
.backgroundColor(0xffffff)
.placeholderColor(0xd2d2d2)
.placeholderFont({
family: "Alimama"

View File

@ -1,8 +1,8 @@
import display from '@ohos.display'
import window from '@ohos.window'
import dayTs from '../../../utils/Date'
import common from '@ohos.app.ability.common'
import { MediaPlayer, PlayerSourceType, PlayStatus } from '../../../utils/MediaPlayer'
import { len2time } from '../../../utils/Utils'
interface VideoPlayerAttribute {
type?: PlayerSourceType
@ -429,9 +429,9 @@ export default struct VideoPlayer {
buildCenterButton() {
Row() {
if (this.touchMode === TouchMode.Seek) {
Text(dayTs().len2time(this.current + this.seekLength)).fontSize(10).fontColor(0xffffff)
Text(len2time(this.current + this.seekLength)).fontSize(10).fontColor(0xffffff)
Text("/").fontSize(10).fontColor(0xffffff).margin({ left: 2, right: 2 })
Text(dayTs().len2time(this.duration)).fontSize(10).fontColor(0xffffff)
Text(len2time(this.duration)).fontSize(10).fontColor(0xffffff)
} else if (this.status === PlayStatus.Playing) {
Image($rawfile("images/player/pause.png")).width(24).height(24)
} else if (this.canPlay()) {
@ -544,9 +544,9 @@ export default struct VideoPlayer {
})
Row() {
Text(dayTs().len2time(this.current)).fontSize(8).fontColor(0xffffff)
Text(len2time(this.current)).fontSize(8).fontColor(0xffffff)
Text("/").fontSize(8).fontColor(0xffffff).margin({ left: 2, right: 2 })
Text(dayTs().len2time(this.duration)).fontSize(8).fontColor(0xffffff)
Text(len2time(this.duration)).fontSize(8).fontColor(0xffffff)
}.margin({ right: 8 })
Stack() {

View File

@ -1,313 +0,0 @@
/*
* @Author: wangzhongjie
* @Date: 2024-02-21 17:12:56
* @LastEditors: wangzhongjie
* @LastEditTime: 2024-02-21 21:29:49
* @Description: 时间工具
* @Email: shutdown0630@163.com
*/
interface DateData {
getFullYear: () => number;
getMonth: () => number;
getDate: () => number;
getHours: () => number;
getMinutes: () => number;
getSeconds: () => number;
getDay: () => number;
}
// 时间处理相关函数集合dayTs("2024-02-21 17:13:00").format("YYYY-MM-DD HH:mm:ss")
class DayTs {
dateData: Date
constructor(date: number | string | Date, offset?: number) {
this.dateData = new Date(date)
if (offset) {
this.dateData = new Date(Number(this.dateData) + offset)
}
}
private static _paddingZero(num: number): string {
return `${num}`.padStart(2, '0')
}
// 格式化时间
public format(formatter: string = 'yyyy/MM/dd HH:mm:ss') {
const day = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
const obj: Record<string, string | number | ((dateData: DateData) => string | number)> = {
'Y{4}': (dateData: DateData) => dateData.getFullYear(),
'y+': (dateData: DateData) => dateData.getFullYear(),
'M{2}': (dateData: DateData) => DayTs._paddingZero(dateData.getMonth() + 1),
"D{2}": (dateData: DateData) => DayTs._paddingZero(dateData.getDate()),
'd{2}': (dateData: DateData) => DayTs._paddingZero(dateData.getDate()),
'H{2}': (dateData: DateData) => DayTs._paddingZero(dateData.getHours()),
'h{2}': (dateData: DateData) => DayTs._paddingZero(dateData.getHours() % 12),
'm{2}': (dateData: DateData) => DayTs._paddingZero(dateData.getMinutes()),
's{2}': (dateData: DateData) => DayTs._paddingZero(dateData.getSeconds()),
'M': (dateData: DateData) => dateData.getMonth() + 1,
'd': (dateData: DateData) => dateData.getDate(),
'H': (dateData: DateData) => dateData.getHours(),
'h': (dateData: DateData) => dateData.getHours() % 12,
'm': (dateData: DateData) => dateData.getMinutes(),
's': (dateData: DateData) => dateData.getSeconds(),
'CW': (dateData: DateData) => day[dateData.getDay()],
'W': (dateData: DateData) => dateData.getDay()
};
const objEntries = Object.entries(obj);
for (let i = 0; i < objEntries.length; i++) {
const key = objEntries[i][0];
const value = objEntries[i][1];
const regexp = new RegExp(`(${key})([^a-zA-Z])?`);
const match = regexp.exec(formatter);
if (match) {
formatter = formatter.replace(match[1], `${(value as Function)(this.dateData)}`);
}
}
return formatter
}
// 获取时间戳
valueOf() {
return this.dateData.getTime()
}
// 获取年份
year() {
//
return this.dateData.getFullYear()
}
// 获取月份
month() {
return this.dateData.getMonth() + 1
}
// 获取日期
date() {
return this.dateData.getDate()
}
// 获取小时
hour() {
return this.dateData.getHours()
}
// 获取分钟
minute() {
return this.dateData.getMinutes()
}
// 获取秒
second() {
return this.dateData.getSeconds()
}
// 获取毫秒
millisecond() {
return this.dateData.getMilliseconds()
}
// 获取星期几
day() {
return this.dateData.getDay()
}
// 获取一年中的第几天
dayOfYear() {
return 21
}
// 获取一月中的第几天
dayOfMonth() {
return 21
}
// 获取一周中的第几天
dayOfWeek(format?: boolean) {
const weeks = ["日", "一", "二", "三", "四", "五", "六",]
return format ? weeks[this.dateData.getDay()] : this.dateData.getDay() + ""
}
// 获取两个时间差值
diff(date: Date | string, unit?: Unit, isFloat?: boolean) {
// 计算this.dateData和date的时间差
const date1 = new Date(this.dateData)
const date2 = new Date(date)
// 计算时间差
const diffValue = date1.getTime() - date2.getTime()
if (unit) {
if (unit === "ms" || unit === "millisecond") {
return diffValue
}
if (unit === "s" || unit === "second") {
return Math.floor(diffValue / 1000)
}
if (unit === "m" || unit === "minute") {
return Math.floor(diffValue / 1000 / 60)
}
if (unit === "h" || unit === "hour") {
return Math.floor(diffValue / 1000 / 60 / 60)
}
if (unit === "d" || unit === "day") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24)
}
if (unit === "w" || unit === "week") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 7)
}
if (unit === "M" || unit === "month") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 30)
}
if (unit === "Q" || unit === "quarter") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 30 / 3)
}
if (unit === "y" || unit === "year") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 365)
}
}
return diffValue
}
// 获取两个时间差值-传入时间戳
diffByDateTime(startTime: number, endTime: number, unit?: Unit) {
// 计算时间差
const diffValue = endTime - startTime
if (unit) {
if (unit === "ms" || unit === "millisecond") {
return diffValue
}
if (unit === "s" || unit === "second") {
return Math.floor(diffValue / 1000)
}
if (unit === "m" || unit === "minute") {
return Math.floor(diffValue / 1000 / 60)
}
if (unit === "h" || unit === "hour") {
return Math.floor(diffValue / 1000 / 60 / 60)
}
if (unit === "d" || unit === "day") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24)
}
if (unit === "w" || unit === "week") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 7)
}
if (unit === "M" || unit === "month") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 30)
}
if (unit === "Q" || unit === "quarter") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 30 / 3)
}
if (unit === "y" || unit === "year") {
return Math.floor(diffValue / 1000 / 60 / 60 / 24 / 365)
}
}
return diffValue
}
// 增加时间
add(value: number, unit: Unit) {
let newDate: number = 0
// 从秒开始
if (unit === "s" || unit === "second") {
newDate = this.dateData.setSeconds(this.dateData.getSeconds() + value)
}
// 从分开始
if (unit === "m" || unit === "minute") {
newDate = this.dateData.setMinutes(this.dateData.getMinutes() + value)
}
// 从小时开始
if (unit === "h" || unit === "hour") {
newDate = this.dateData.setHours(this.dateData.getHours() + value)
}
// 从天开始
if (unit === "d" || unit === "day") {
newDate = this.dateData.setDate(this.dateData.getDate() + value)
}
return new DayTs(newDate)
}
// 减少时间
subtract(value: number, unit: Unit) {
console.log(value.toString(), unit)
return 123456
}
// 时间开始
startOf(unit: StartEndUnit) {
console.log(unit)
return 123456
}
// 时间结束
endOf(unit: StartEndUnit) {
console.log(unit)
return 123456
}
// 是否同一天
compare(date: Date | number | string) {
let tmp = dayTs(date)
let isDay = this.year() === tmp.year() && this.month() === tmp.month() && this.date() === tmp.date()
return isDay
}
/**
* 秒转标准时间格式
* @param time 时间长度 单位秒
* @param showHour
* @returns
*/
len2time(time: string | number, showHour: boolean = false) {
let duration = +Number(time)
let hour = Math.floor(duration / (60 * 60))
let min = Math.floor((duration % (60 * 60)) / 60)
let seconds = Math.floor(duration % 60)
let timestamp = ""
if (hour > 0 || showHour) {
timestamp += hexTime(hour) + ":"
}
return timestamp + hexTime(min) + ":" + hexTime(seconds)
}
}
const hexTime = (num: number) => {
return num < 10 ? `0${num}` : num + ''
}
type BaseUnit =
"day" |
"d" |
"month" |
"M" |
"quarter" |
"Q" |
"year" |
"y" |
"week" |
"w" |
"hour" |
"h" |
"minute" |
"m" |
"second" |
"s";
type Unit = BaseUnit | "millisecond" | "ms";
type StartEndUnit = BaseUnit | "isoWeek" | "date" | "D";
// const proto = DayTs.prototype
const dayTs = (date?: number | string | Date, offset?: number) => {
if (!date) {
date = new Date()
}
return new DayTs(date, offset)
}
// dayTs.prototype = proto;
export default dayTs

View File

@ -1,5 +1,6 @@
import fs from '@ohos.file.fs'
import hilog from '@ohos.hilog'
import { dateFormat } from './Utils'
interface LoggerOption {
mode?: LoggerMode
@ -22,33 +23,7 @@ export enum LoggerMode {
const MB = 1024 * 1024
const Tag = "Logger"
function formatDate(date: Date, fmt = 'yyyy-MM-dd') {
date = date instanceof Date ? date : new Date(date);
const pad = (n: number) => (n < 10 ? `0${n}` : `${n}`);
const pad3 = (n: number) => (n < 10 ? `00${n}` : n < 100 ? `0${n}` : `${n}`);
return fmt
.replace(new RegExp("yyyy"), date.getFullYear().toString())
.replace(new RegExp("yy"), `${date.getFullYear()}`.slice(-2))
.replace(new RegExp("MM"), pad(date.getMonth() + 1))
.replace(new RegExp("M"), (date.getMonth() + 1).toString())
.replace(new RegExp("dd"), pad(date.getDate()))
.replace(new RegExp("d"), (date.getDate()).toString())
.replace(new RegExp("HH"), pad(date.getHours()))
.replace(new RegExp("H"), (date.getHours()).toString())
.replace(new RegExp("hh"), pad(date.getHours() % 12 || 12))
.replace(new RegExp("h"), (date.getHours() % 12 || 12).toString())
.replace(new RegExp("mm"), pad(date.getMinutes()))
.replace(new RegExp("m"), date.getMinutes().toString())
.replace(new RegExp("ss"), pad(date.getSeconds()))
.replace(new RegExp("s"), date.getSeconds().toString())
.replace(new RegExp("SSS"), pad3(date.getMilliseconds()))
.replace(new RegExp("S"), date.getMilliseconds().toString())
.replace(new RegExp("a"), date.getHours() < 12 ? '上午' : '下午')
.replace(new RegExp("A"), date.getHours() < 12 ? 'AM' : 'PM')
.replace(new RegExp("q"), Math.floor((date.getMonth() + 3) / 3).toString());
}
function pathJoin(...path: string[]) {
return path.join("/")
@ -58,8 +33,8 @@ export default class Logger {
private static mode: LoggerMode = LoggerMode.Development
private static level: LoggerLevel = LoggerLevel.Info
private static size: number = MB * 10
private static date: string = formatDate(new Date(), "yyyy_MM_dd")
private static time: string = formatDate(new Date(), "HH_mm_ss.SSS")
private static date: string = dateFormat(new Date(), "yyyy_MM_dd")
private static time: string = dateFormat(new Date(), "HH_mm_ss.SSS")
private static fd: number = 0
private static dir: string = ""
@ -79,8 +54,8 @@ export default class Logger {
private static createNewFile() {
try {
Logger.date = formatDate(new Date(), "yyyy_MM_dd")
Logger.time = formatDate(new Date(), "HH_mm_ss.SSS")
Logger.date = dateFormat(new Date(), "yyyy_MM_dd")
Logger.time = dateFormat(new Date(), "HH_mm_ss.SSS")
let path = pathJoin(Logger.dir, Logger.date, Logger.time) + ".log"
if (!fs.accessSync(path)) {
let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.APPEND | fs.OpenMode.WRITE_ONLY)
@ -119,7 +94,7 @@ export default class Logger {
Logger.createNewFile()
}
fs.writeSync(Logger.fd,
`[${formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")}]-[${map[type]}]: ${resultMessage}\n`)
`[${dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")}]-[${map[type]}]: ${resultMessage}\n`)
} catch (e) {
hilog.error(0x0000, Tag, JSON.stringify(e))
}

View File

@ -15,7 +15,7 @@ export class TaskPool<T> {
private max: number = 10
constructor(max?: number) {
this.max = this.max
this.max = max || 10
}
private async handleTask(task: Task<T>) {

View File

@ -0,0 +1,43 @@
export function dateFormat(date: Date, fmt = 'yyyy-MM-dd') {
date = date instanceof Date ? date : new Date(date);
const pad = (n: number) => (n < 10 ? `0${n}` : `${n}`);
const pad3 = (n: number) => (n < 10 ? `00${n}` : n < 100 ? `0${n}` : `${n}`);
return fmt
.replace(new RegExp("yyyy"), date.getFullYear().toString())
.replace(new RegExp("yy"), `${date.getFullYear()}`.slice(-2))
.replace(new RegExp("MM"), pad(date.getMonth() + 1))
.replace(new RegExp("M"), (date.getMonth() + 1).toString())
.replace(new RegExp("dd"), pad(date.getDate()))
.replace(new RegExp("d"), (date.getDate()).toString())
.replace(new RegExp("HH"), pad(date.getHours()))
.replace(new RegExp("H"), (date.getHours()).toString())
.replace(new RegExp("hh"), pad(date.getHours() % 12 || 12))
.replace(new RegExp("h"), (date.getHours() % 12 || 12).toString())
.replace(new RegExp("mm"), pad(date.getMinutes()))
.replace(new RegExp("m"), date.getMinutes().toString())
.replace(new RegExp("ss"), pad(date.getSeconds()))
.replace(new RegExp("s"), date.getSeconds().toString())
.replace(new RegExp("SSS"), pad3(date.getMilliseconds()))
.replace(new RegExp("S"), date.getMilliseconds().toString())
.replace(new RegExp("a"), date.getHours() < 12 ? '上午' : '下午')
.replace(new RegExp("A"), date.getHours() < 12 ? 'AM' : 'PM')
.replace(new RegExp("q"), Math.floor((date.getMonth() + 3) / 3).toString());
}
export function len2time(time: string | number, showHour: boolean = false) {
const hexTime = (num: number) => {
return num < 10 ? `0${num}` : num + ''
}
let duration = +Number(time)
let hour = Math.floor(duration / (60 * 60))
let min = Math.floor((duration % (60 * 60)) / 60)
let seconds = Math.floor(duration % 60)
let timestamp = ""
if (hour > 0 || showHour) {
timestamp += hexTime(hour) + ":"
}
return timestamp + hexTime(min) + ":" + hexTime(seconds)
}