“wangzhongjie” 157a292a74 init🎉: 初始化
2024-01-05 11:11:15 +08:00

78 lines
2.7 KiB
TypeScript

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}
let res:any = await timeSynchronization(params)
res = res.timeSynchronizationRsp;
globalThis.timeInfo=res.body
// globalThis.timeInfo.url='http://112.80.35.83:11054/der2/services'
// globalThis.timeInfo.url='http://112.80.35.83:11054/der2/services'
console.info('jiangsong1:timeSynchronization '+ JSON.stringify(res) );
// {"timeSynchronizationRsp":
// {"head":
// {"resultCode":"0","resultMessage":"%E6%97%B6%E9%97%B4%E5%90%8C%E6%AD%A5%E6%8E%A5%E5%8F%A3-%E6%88%90%E5%8A%9F","time":"2023-03-07 11:08:09"},
// "body":
// {"carid":"1003","kdid":"2","ksdd":"6224J252","mode":"3","paraKdid":"122","url":"http://192.168.32.167:8084/der2/services"}
// }
// }
return res;
}
export async function setCurrentTime():Promise<void> {
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<string> {
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}`
}