node-red时区问题解决方案对比

一、问题描述

  在实际操作中,我们从前端传递时,还是中国时间,但是到了node-red中,就少了8小时,出现时区不对应的情况。

  • 确认node-red时区:在node-red中输出当前时间,发现Node-red的容器内时间正常,Node-red输出的时间戳也是当前的时间。排除node-red本身时区问题。

  Node-RED默认使用UTC时区,如果想更改时区,你可以在运行Node-RED时通过TZ变量来传入你期望的时区。

  • 在数据库端查询,查看查询输出结果,结果正常,排除数据库本身时区设置问题。
  • 通过node-red使用http://rest查询tdengine数据库,返回的时间格式与实际时间少了8小时。需要修改时区。

回到目录

二、node-red修改时区的两种方法

  node-red修改时区有两种方法,一种是通过moment节点修改,一种是通过date函数修改。

1. 方法对比如下:

  • 第一种方式更通用,能支持多种格式的互相转换,但是转换速度慢,对于大数据量数组的转换需要消耗较长时间和资源。(批量转换10万条数据时,耗时0.5秒)
  • 第二种方式适应性较差,需要指定固定的时区进行转换,但是转换速度快,效率高。(批量转换1万条数据时,耗时1.4秒)

2. 总结:

  • 对于大数据量的数组循环批量时,推荐第二种
  • 对于单次或少量的转换时,推荐第一种

方法详情如下:

2.1 通过安装moment节点修改 - 少量时选择

  1. 安装node-red-contrib-moment节点
  2. 使用moment节点,将时间字符串进行转换,格式示例如下:
    1

moment节点详细操作参考链接:读取时间的节点 node-red-contrib-moment

  1. js代码详情:
[
    {
        "id": "48a7566649d1f1b7",
        "type": "inject",
        "z": "411470ea6ab023d5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 480,
        "wires": [
            [
                "9a41e67fee63eab8"
            ]
        ]
    },
    {
        "id": "9a41e67fee63eab8",
        "type": "function",
        "z": "411470ea6ab023d5",
        "name": "初始化",
        "func": "msg.data = [[\"2023-07-26T07:53:50.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:48.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:46.000Z\", 449, 0, 0, 0, 1]];\nfor(let i = 0;i <10000; i++){\n    msg.data.push([\"2023-07-26T07:53:50.000Z\", 4+i, 0, 0, 0, 1]);\n}\nmsg.t = new Date().getTime();\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 480,
        "wires": [
            [
                "b8d4ba7dcd1ea9cd",
                "48cc9a3e33f41101"
            ]
        ]
    },
    {
        "id": "b8d4ba7dcd1ea9cd",
        "type": "debug",
        "z": "411470ea6ab023d5",
        "name": "debug 35",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "data",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 380,
        "y": 660,
        "wires": []
    },
    {
        "id": "48cc9a3e33f41101",
        "type": "array-loop",
        "z": "411470ea6ab023d5",
        "name": "",
        "key": "id",
        "keyType": "msg",
        "reset": true,
        "resetValue": "value-undefined",
        "array": "data",
        "arrayType": "msg",
        "x": 460,
        "y": 520,
        "wires": [
            [
                "ebfe46536b1e2467"
            ],
            [
                "3a2d7148ba39a821"
            ]
        ]
    },
    {
        "id": "5cfd0bcf779ca93b",
        "type": "moment",
        "z": "411470ea6ab023d5",
        "name": "时区转换",
        "topic": "",
        "input": "dateStr",
        "format": "YYYY-MM-DD HH:mm:ss.SSS",
        "locale": "en-US",
        "output": "dateStr",
        "x": 560,
        "y": 600,
        "wires": [
            [
                "6e8f4da78a919e81"
            ]
        ]
    },
    {
        "id": "ebfe46536b1e2467",
        "type": "function",
        "z": "411470ea6ab023d5",
        "name": "function 7",
        "func": "msg.t = \"耗时:\" + ((new Date().getTime() - msg.t) / 1000) + \"秒\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 520,
        "wires": [
            [
                "f46f8436d1d35a36"
            ]
        ]
    },
    {
        "id": "f46f8436d1d35a36",
        "type": "debug",
        "z": "411470ea6ab023d5",
        "name": "debug 37",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 780,
        "y": 520,
        "wires": []
    },
    {
        "id": "3a2d7148ba39a821",
        "type": "function",
        "z": "411470ea6ab023d5",
        "name": "function 8",
        "func": "msg.dateStr = msg.data[msg.id][0].toUpperCase();\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 600,
        "wires": [
            [
                "5cfd0bcf779ca93b"
            ]
        ]
    },
    {
        "id": "6e8f4da78a919e81",
        "type": "function",
        "z": "411470ea6ab023d5",
        "name": "function 9",
        "func": "msg.data[msg.id][0] = msg.dateStr;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 600,
        "wires": [
            [
                "48cc9a3e33f41101"
            ]
        ]
    }
]

回到目录

2.2 通过date函数修改 - 批量时选择

var d = new Date(dateStr)
d.setHours(d.getHours() + 8);//将时加8
let s = d.toISOString().replace("T", " ").replace("Z", "");//去掉S T的时间格式

示例js代码如下:
1

[
    {
        "id": "b8d4ba7dcd1ea9cd",
        "type": "debug",
        "z": "411470ea6ab023d5",
        "name": "debug 35",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "data",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 480,
        "y": 460,
        "wires": []
    },
    {
        "id": "b8a9fec4858d0409",
        "type": "function",
        "z": "411470ea6ab023d5",
        "name": "转换",
        "func": "msg.data.forEach((item) => {\n    if (item[0].includes(\"T\")){\n        let d = new Date(item[0]);\n        d.setHours(d.getHours() + 8);\n        item[0] = d.toISOString().replace(\"T\", \" \").replace(\"Z\", \"\");\n    }\n});\nmsg.t = \"耗时:\" + ((new Date().getTime() - msg.t) / 1000)+\"秒\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 400,
        "wires": [
            [
                "e18227c520476fe0"
            ]
        ]
    },
    {
        "id": "e18227c520476fe0",
        "type": "debug",
        "z": "411470ea6ab023d5",
        "name": "debug 36",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 600,
        "y": 400,
        "wires": []
    },
    {
        "id": "9a41e67fee63eab8",
        "type": "function",
        "z": "411470ea6ab023d5",
        "name": "初始化",
        "func": "msg.data = [[\"2023-07-26T07:53:50.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:48.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:46.000Z\", 449, 0, 0, 0, 1]];\nfor(let i = 0;i <10000; i++){\n    msg.data.push([\"2023-07-26T07:53:50.000Z\", 4+i, 0, 0, 0, 1]);\n}\nmsg.t = new Date().getTime();\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 480,
        "wires": [
            [
                "b8d4ba7dcd1ea9cd",
                "b8a9fec4858d0409"
            ]
        ]
    },
    {
        "id": "48a7566649d1f1b7",
        "type": "inject",
        "z": "411470ea6ab023d5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 480,
        "wires": [
            [
                "9a41e67fee63eab8"
            ]
        ]
    }
]

回到目录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值