mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Merge pull request #4468 from node-red/timestamp-formatting
Timestamp formatting
This commit is contained in:
commit
22b4ab6bb2
@ -925,6 +925,12 @@
|
|||||||
"jsonata": "expression",
|
"jsonata": "expression",
|
||||||
"env": "env variable",
|
"env": "env variable",
|
||||||
"cred": "credential"
|
"cred": "credential"
|
||||||
|
},
|
||||||
|
"date": {
|
||||||
|
"format": {
|
||||||
|
"timestamp": "milliseconds since epoch",
|
||||||
|
"object": "JavaScript Date Object"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"editableList": {
|
"editableList": {
|
||||||
|
@ -408,7 +408,25 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.svg"},
|
re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.svg"},
|
||||||
date: {value:"date",label:"timestamp",icon:"fa fa-clock-o",hasValue:false},
|
date: {
|
||||||
|
value:"date",
|
||||||
|
label:"timestamp",
|
||||||
|
icon:"fa fa-clock-o",
|
||||||
|
options:[
|
||||||
|
{
|
||||||
|
label: 'milliseconds since epoch',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'YYYY-MM-DDTHH:mm:ss.sssZ',
|
||||||
|
value: 'iso'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'JavaScript Date Object',
|
||||||
|
value: 'object'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
jsonata: {
|
jsonata: {
|
||||||
value: "jsonata",
|
value: "jsonata",
|
||||||
label: "expression",
|
label: "expression",
|
||||||
@ -652,6 +670,10 @@
|
|||||||
allOptions.flow.options = contextStoreOptions;
|
allOptions.flow.options = contextStoreOptions;
|
||||||
allOptions.global.options = contextStoreOptions;
|
allOptions.global.options = contextStoreOptions;
|
||||||
}
|
}
|
||||||
|
// Translate timestamp options
|
||||||
|
allOptions.date.options.forEach(opt => {
|
||||||
|
opt.label = RED._("typedInput.date.format." + (opt.value || 'timestamp'), {defaultValue: opt.label})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
nlsd = true;
|
nlsd = true;
|
||||||
var that = this;
|
var that = this;
|
||||||
|
@ -117,7 +117,7 @@ module.exports = function(RED) {
|
|||||||
});
|
});
|
||||||
return
|
return
|
||||||
} else if (rule.tot === 'date') {
|
} else if (rule.tot === 'date') {
|
||||||
value = Date.now();
|
value = RED.util.evaluateNodeProperty(rule.to, rule.tot, node)
|
||||||
} else if (rule.tot === 'jsonata') {
|
} else if (rule.tot === 'jsonata') {
|
||||||
RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => {
|
RED.util.evaluateJSONataExpression(rule.to,msg, (err, value) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
10
packages/node_modules/@node-red/util/lib/util.js
vendored
10
packages/node_modules/@node-red/util/lib/util.js
vendored
@ -636,7 +636,15 @@ function evaluateNodeProperty(value, type, node, msg, callback) {
|
|||||||
} else if (type === 're') {
|
} else if (type === 're') {
|
||||||
result = new RegExp(value);
|
result = new RegExp(value);
|
||||||
} else if (type === 'date') {
|
} else if (type === 'date') {
|
||||||
result = Date.now();
|
if (!value) {
|
||||||
|
result = Date.now();
|
||||||
|
} else if (value === 'object') {
|
||||||
|
result = new Date()
|
||||||
|
} else if (value === 'iso') {
|
||||||
|
result = (new Date()).toISOString()
|
||||||
|
} else {
|
||||||
|
result = moment().format(value)
|
||||||
|
}
|
||||||
} else if (type === 'bin') {
|
} else if (type === 'bin') {
|
||||||
var data = JSON.parse(value);
|
var data = JSON.parse(value);
|
||||||
if (Array.isArray(data) || (typeof(data) === "string")) {
|
if (Array.isArray(data) || (typeof(data) === "string")) {
|
||||||
|
@ -379,10 +379,17 @@ describe("@node-red/util/util", function() {
|
|||||||
result = util.evaluateNodeProperty('','bool');
|
result = util.evaluateNodeProperty('','bool');
|
||||||
result.should.be.false();
|
result.should.be.false();
|
||||||
});
|
});
|
||||||
it('returns date',function() {
|
it('returns date - default format',function() {
|
||||||
var result = util.evaluateNodeProperty('','date');
|
var result = util.evaluateNodeProperty('','date');
|
||||||
(Date.now() - result).should.be.approximately(0,50);
|
(Date.now() - result).should.be.approximately(0,50);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns date - iso format',function() {
|
||||||
|
var result = util.evaluateNodeProperty('iso','date');
|
||||||
|
// 2023-12-04T16:51:04.429Z
|
||||||
|
/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d+Z$/.test(result).should.be.true()
|
||||||
|
});
|
||||||
|
|
||||||
it('returns bin', function () {
|
it('returns bin', function () {
|
||||||
var result = util.evaluateNodeProperty('[1, 2]','bin');
|
var result = util.evaluateNodeProperty('[1, 2]','bin');
|
||||||
result[0].should.eql(1);
|
result[0].should.eql(1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user