| | |
| | | /* 时间函数 */ |
| | | |
| | | import dayjs from 'dayjs' |
| | | import lineChart from '@/utils/chartFunction/lineChart.js' |
| | | import customParseFormat from 'dayjs/plugin/customParseFormat' |
| | | import dayjs from 'dayjs' |
| | | dayjs.extend(customParseFormat) |
| | | export default { |
| | | // 判断已选的月份是否大于当前月份 |
| | | judgeDateValid(date) { |
| | |
| | | |
| | | return time |
| | | }, |
| | | /** |
| | | * 返回有效的时间点 |
| | | * @param:扬尘数据 |
| | | * @returns:升序的时间点 |
| | | */ |
| | | validTime(dustData) { |
| | | // 参数为空则退出 |
| | | if (!dustData.length) { |
| | | return [] |
| | | } |
| | | |
| | | let time = [] |
| | | dustData.forEach((item) => { |
| | | if (item.flag != 'A') { |
| | | time.push(item.lst) |
| | | } |
| | | }) |
| | | |
| | | return time |
| | | }, |
| | | /** |
| | | * 取指定间隔的时间为连续时间,放在数组中。 孤立的时间点与自身算一个连续 |
| | | * 目的是构造有效率缺失的颜色背景区间 |
| | |
| | | result.push(temp) |
| | | } |
| | | return result |
| | | }, |
| | | |
| | | /** |
| | | *判断是否是有意义的日期 |
| | | * @param: |
| | | * @returns: |
| | | */ |
| | | judgeTimeValid(time) { |
| | | let r1 = dayjs(time, 'YYYY-MM-DD HH:mm:ss', true).isValid() |
| | | let r2 = dayjs(time, 'YYYY-MM-DD', true).isValid() |
| | | // 两种日期格式都无效时 |
| | | if (!r1 && !r2) { |
| | | return false |
| | | } |
| | | // 符合任意一种格式 |
| | | return true |
| | | }, |
| | | /** |
| | | * 'YYYY-MM-DD HH:mm:ss'返回true |
| | | * @param: |
| | | * @returns: |
| | | */ |
| | | judgeTimeFormat(time) { |
| | | // 'YYYY-MM-DD HH:mm:ss' |
| | | const dateTimeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/ |
| | | // 'YYYY-MM-DD' |
| | | const dateRegex = /^\d{4}-\d{2}-\d{2}$/ |
| | | |
| | | if (dateTimeRegex.test(time)) { |
| | | return true |
| | | } else if (dateRegex.test(time)) { |
| | | return false |
| | | } |
| | | }, |
| | | |
| | | /** |
| | | * 找到在给定的开始时间bt和结束时间et之间缺失的时间区间 |
| | | * @param:开始时间,结束时间,在这个时间范围内已有的时间(时间字符串数组),数组中时间的间隔 |
| | | * @returns: |
| | | */ |
| | | getMissingDays(bt, et, timeArr, minutesNum = 1440) { |
| | | // 判断日期是否有意义 |
| | | if (!this.judgeTimeValid(bt) || !this.judgeTimeValid(et)) { |
| | | return false |
| | | } |
| | | // 存储缺失的时间区间 |
| | | const r = [] |
| | | const begin = dayjs(bt) |
| | | const end = dayjs(et) |
| | | // 开始时间 |
| | | let current = begin |
| | | |
| | | for (const time of timeArr) { |
| | | const currentTime = dayjs(time) |
| | | if (currentTime.isBefore(current)) { |
| | | continue |
| | | } |
| | | |
| | | if (currentTime.isAfter(current)) { |
| | | r.push([current, currentTime]) |
| | | } |
| | | |
| | | current = currentTime.add(minutesNum, 'minute') |
| | | } |
| | | |
| | | if (end.isAfter(current)) { |
| | | r.push([current, end]) |
| | | } |
| | | |
| | | let f = this.judgeTimeFormat(bt) ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD' |
| | | for (let i = 0; i < r.length; i++) { |
| | | for (let j = 0; j < r[i].length; j++) { |
| | | r[i][j] = r[i][j].format(f) |
| | | } |
| | | } |
| | | return r |
| | | }, |
| | | /** |
| | | * 根据时间字符串返回UTC时间 |
| | | * @param: |
| | | * @returns: |
| | | */ |
| | | utcTime(timeStr){ |
| | | return dayjs(timeStr).format() |
| | | }, |
| | | /** |
| | | * 将utc时间转为时间字符串 |
| | | * @param: |
| | | * @returns: |
| | | */ |
| | | utcToStr(dateTime){ |
| | | const r = dateTime ? dayjs(dateTime).format('YYYY-MM-DD HH:mm:ss') :false |
| | | return r |
| | | } |
| | | } |