import systemTime from '@ohos.systemTime'; import { timeSynchronization } from '../../api/index' import { dateFormat,dateVersionFormat } from '../utils/tools'; //同步时时间 export async function timeSynchronize() { let date = new Date(); console.info('jiangsong1:timeSynchronization begin ' ); let params = { time: dateFormat(date), deviceNo: globalThis.deviceNo,version:globalThis.version,judgeVersion:globalThis.judgeVersion} let res:any = await timeSynchronization(params) res = res.timeSynchronizationRsp; globalThis.timeInfo=res.body console.log('timeInfo',JSON.stringify(res.body)) return res; } export async function setCurrentTime():Promise { let res = await timeSynchronize(); let currentTime = res.head.time; let times = new Date(currentTime).getTime(); console.log('jiangsong:times==' + times); try { systemTime.setTime(times).then(() => { console.info(`Succeeded in setting time.`); }).catch((error) => { console.info(`Failed to set time. message: ${error.message}, code: ${error.code}`); }); } catch(e) { console.info(`Failed to set time. message: ${e.message}, code: ${e.code}`); } } //获取当前时间并转化 export async function getCurrentTime():Promise { const date = await systemTime.getDate(); const year = date.getFullYear(); let month = date.getMonth() + 1; //@ts-ignore month = month < 10 ? '0' + month : month; let dates = date.getDate(); //@ts-ignore dates = dates < 10 ? '0' + dates : dates; let h = date.getHours(); //@ts-ignore h = h < 10 ? '0' + h : h; let m = date.getMinutes(); //@ts-ignore m = m < 10 ? '0' + m : m; let s = date.getSeconds(); //@ts-ignore s = s < 10 ? '0' + s : s; return `${year}-${month}-${dates} ${h}:${m}:${s}` } //时间戳转日期 export function formatTime(time:number):string { //@ts-ignore const h = parseInt(time / 3600) //@ts-ignore const minute = parseInt(time / 60 % 60) const second = Math.ceil(time % 60) const hours = h < 10 ? '0' + h : h const formatSecond = second > 59 ? 59 : second return `${hours > 0 ? `${hours}:` : `${hours}:`}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}` }