Repository: node-red/node-red (js)

Issue:
Core Trigger Node does not send epoch
### Current Behavior

Reported on forum, here: https://discourse.nodered.org/t/trigger-node-output-bug-or-pebkac/92335

> One seems to send a legit timestamp, the other is just sending "1". Have they always been like this? I only noticed when one of my flows broke after updating to 4.0.4 and it is still behaving this way on 4.0.5.

Trigger node does not send epoch - it sends "1"

![image](https://github.com/user-attachments/assets/66d8c2a3-870c-417a-ae94-4f65b0a6ace3)



At a guess, this is related to the introduction of additional timestamp options since the trigger node has not had any changes for ~4y.



### Expected Behavior

Should send epoch

### Steps To Reproduce

import the below flow and inject

### Example flow

```
[{"id":"de7b0ee8ecb2e5ee","type":"group","z":"0f909bac596182b4","name":"Doesnt send epoch - GH issue #4914","style":{"stroke":"#2b2b2b","fill":"#181818","label":true,"color":"#ff0000","fill-opacity":"0.22"},"nodes":["36e5f0ca2a0f5cfc","9a9a4e8234f9f6f2","11068274249f5b33","4250ea710b0d6ffb","d24269b36bf9e864"],"x":984,"y":119,"w":532,"h":162},{"id":"36e5f0ca2a0f5cfc","type":"inject","z":"0f909bac596182b4","g":"de7b0ee8ecb2e5ee","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1045,"y":200,"wires":[["9a9a4e8234f9f6f2","4250ea710b0d6ffb"]],"l":false},{"id":"9a9a4e8234f9f6f2","type":"trigger","z":"0f909bac596182b4","g":"de7b0ee8ecb2e5ee","name":"2 Seconds","op1":"","op2":"","op1type":"date","op2type":"nul","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1180,"y":160,"wires":[["11068274249f5b33"]]},{"id":"11068274249f5b33","type":"debug","z":"0f909bac596182b4","g":"de7b0ee8ecb2e5ee","name":"should send epoch","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1380,"y":160,"wires":[]},{"id":"4250ea710b0d6ffb","type":"trigger","z":"0f909bac596182b4","g":"de7b0ee8ecb2e5ee","name":"2 Seconds","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1180,"y":240,"wires":[["d24269b36bf9e864"]]},{"id":"d24269b36bf9e864","type":"debug","z":"0f909bac596182b4","g":"de7b0ee8ecb2e5ee","name":"send the msg","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1370,"y":240,"wires":[]}]
```


### Environment

- Node-RED version: 4.0.3, 4.0.4, 4.0.5
- Node.js version:
- npm version:
- Platform/OS:
- Browser:


Pull request:
Fix trigger node date handling for latest time type input
to fix #4914

<!--
## Before you hit that Submit button....

Please read our [contribution guidelines](https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md)
before submitting a pull-request.

## Types of changes

What types of changes does your code introduce?
Put an `x` in the boxes that apply
-->

- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)

<!--
If you want to raise a pull-request with a new feature, or a refactoring
of existing code, it **may well get rejected** if it hasn't been discussed on
the [forum](https://discourse.nodered.org) or
[slack team](https://nodered.org/slack) first.

-->

## Proposed changes

<!-- Describe the nature of this change. What problem does it address? -->
Fixes trigger node handling of the new time type typed input.

## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I have read the [contribution guidelines](https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md)
- [ ] For non-bugfix PRs, I have discussed this change on the forum/slack team.
- [x] I have run `npm run test` to verify the unit tests pass
- [x] I have added suitable unit tests to cover the new/changed functionality


Patch:
diff --git a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js
index 16a00e99d..8b3625f2d 100644
--- a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js
+++ b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js
@@ -24,6 +24,14 @@ module.exports = function(RED) {
         this.op2 = n.op2 || "0";
         this.op1type = n.op1type || "str";
         this.op2type = n.op2type || "str";
+        // If the op1/2type is 'date', then we need to leave op1/2 alone so that
+        // evaluateNodeProperty works as expected.
+        if (this.op1type === 'date' && this.op1 === '1') {
+            this.op1 = ''
+        }
+        if (this.op2type === 'date' && this.op2 === '0') {
+            this.op2 = ''
+        }
         this.second = (n.outputs == 2) ? true : false;
         this.topic = n.topic || "topic";
 
@@ -193,7 +201,7 @@ module.exports = function(RED) {
                                         if (node.op2type !== "nul") {
                                             var promise = Promise.resolve();
                                             msg2 = RED.util.cloneMessage(msg);
-                                            if (node.op2type === "flow" || node.op2type === "global") {
+                                            if (node.op2type === "flow" || node.op2type === "global" || node.op2type === "date") {
                                                 promise = new Promise((resolve,reject) => {
                                                     RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg,(err,value) => {
                                                         if (err) {
@@ -213,7 +221,6 @@ module.exports = function(RED) {
                                                 }
                                                 else {
                                                     msg2.payload = node.topics[topic].m2;
-                                                    if (node.op2type === "date") { msg2.payload = Date.now(); }
                                                     if (node.second === true) { msgInfo.send([null,msg2]); }
                                                     else { msgInfo.send(msg2); }
                                                 }


Test patch:
diff --git a/test/nodes/core/function/89-trigger_spec.js b/test/nodes/core/function/89-trigger_spec.js
index 33401e540..913298901 100644
--- a/test/nodes/core/function/89-trigger_spec.js
+++ b/test/nodes/core/function/89-trigger_spec.js
@@ -111,7 +111,15 @@ describe('trigger node', function() {
                     try {
                         if (rval) {
                             msg.should.have.property("payload");
-                            should.deepEqual(msg.payload, rval);
+                            if (type == "date" && val == "1") {
+                                should.deepEqual(Math.round(msg.payload/1000000), Math.round(Date.now()/1000000));
+                            }
+                            else if (type == "date" && val == "iso") {
+                                should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11));
+                            }
+                            else {
+                                should.deepEqual(msg.payload, rval);
+                            }
                         }
                         else {
                             msg.should.have.property("payload", val);
@@ -126,6 +134,7 @@ describe('trigger node', function() {
         });
 
         it('should output 2st value when triggered ('+type+')', function(done) {
+            if (type == "date" && val == "1") { val = "0"; }
             var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1:"foo", op1type:"str", op2:val, op2type:type, duration:"20", wires:[["n2"]] },
                 {id:"n2", type:"helper"} ];
             process.env[val] = rval;
@@ -142,7 +151,15 @@ describe('trigger node', function() {
                         else {
                             if (rval) {
                                 msg.should.have.property("payload");
-                                should.deepEqual(msg.payload, rval);
+                                if (type == "date" && val == "0") {
+                                    ;(Math.round(msg.payload/1000000)).should.be.approximately(parseInt(Date.now()/1000000), 1);
+                                }
+                                else if (type == "date" && val == "iso") {
+                                    should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11));
+                                }
+                                else {
+                                    should.deepEqual(msg.payload, rval);
+                                }
                             }
                             else {
                                 msg.should.have.property("payload", val);
@@ -166,6 +183,9 @@ describe('trigger node', function() {
     var val_buf = "[1,2,3,4,5]";
     basicTest("bin", val_buf, Buffer.from(JSON.parse(val_buf)));
     basicTest("env", "NR-TEST", "env-val");
+    basicTest("date", "1", Date.now());
+    basicTest("date", "iso", (new Date()).toISOString());
+    // basicTest("date", "object", Date.now());
 
     it('should output 1 then 0 when triggered (default)', function(done) {
         var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", duration:"20", wires:[["n2"]] },