使用nodejs 实现阿里云时间表达式解析时间

​​​​​​SQL时间窗口相关的时间表达式语法_日志服务-阿里云帮助中心

按照一下规则实现一个输入表达式返回时间

/**
 * sls 时间表达式解析
 */
const moment = require('moment')
class SlsScheduledParse {
    constructor() {
    }

    /**
     * 解析时间表达式字符串
     * @param param
     * @returns {[]} 返回一个数组
     */
    getParse(param){
       let res = []
       let regs = /^[1-9mh\@\+\-]*$/
       if(param && regs.test(param)){
           //先判断字符串中是否有别的字符串
           for(let i=0;i<param.length;i++){
               if(param[i]=='m' || param[i]=='h'){
                   if(res.length==0){
                       res.push(param.substr(res.length,i+1))
                   }else {
                       res.push(param.substr(res[res.length-1].length,i+1))
                   }
               }
           }
           return res;
       }else {
           return res
       }
    }

    /**
     * 根据时间表达是获取到时间
     * @param param
     * @returns {string|null}
     */
    getTime(param){
        let arr = this.getParse(param)
        if(arr.length==0){
            return null
        }
        let now = moment()
        for (let it of arr){
            if(it.includes('m')){
                if(it.includes('-')){
                    //减时间
                    let diff = it.substring(it.indexOf('-'),it.indexOf('m'))
                    if(diff.length==1){
                        diff = -1
                    }
                    now = now.add(diff,'minutes')
                }else {
                    //加时间
                    let temp_str = it
                    if(it.includes('@')){
                        temp_str= it.replace('@','')
                    }
                    let diff = 1
                    if(temp_str.includes('+')){
                        diff = temp_str.substring(temp_str.indexOf('+'),temp_str.indexOf('m'))
                    }else {
                        diff = temp_str.substring(0,temp_str.indexOf('m'))
                    }
                    now = now.add(diff,'minutes')
                }
                if(it.includes('@')){
                    //分钟取整
                    now = now.startOf("minute")
                }
            }
            if(it.includes('h')){
                if(it.includes('-')){
                    //减时间
                    let diff = it.substring(it.indexOf('-'),it.indexOf('h'))
                    if(diff.length==1){
                        diff = -1
                    }
                    now = now.add(diff,'hours')
                }else {
                    //加时间
                    let temp_str = it
                    if(it.includes('@')){
                        temp_str= it.replace('@','')
                    }
                    let diff = 1
                    if(temp_str.includes('+')){
                        diff = temp_str.substring(temp_str.indexOf('+'),temp_str.indexOf('h'))
                    }else {
                        diff = temp_str.substring(0,temp_str.indexOf('h'))
                    }
                    now = now.add(diff,'hours')
                }
                if(it.includes('@')){
                    //小时取整
                    now = now.startOf("hours")
                }
            }
        }
        return now.format('YYYY-MM-DD HH:mm:ss');
    }

}
module.exports = SlsScheduledParse

let sls = new SlsScheduledParse();
let time = sls.getTime("24h+5m")
console.log(time)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古月_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值