I am having some trouble with the timestamps and dates in general.
My time zone is UTC (Europe/Lisbon) but when DST kicks in the time changes to UTC+1
The problem is that node-red does not take the DST in to account. So now whenever I create a date its created with the UTC time.
I also used the moment.js library, but get the same problem. This is where things get wierder.
I set up a timestamp inject that runs a function node with the following code:
var moment=global.get('moment')
msg.nowlocal=moment().local() //current datetime in local
msg.now=moment() //current datetime
msg.utc=moment().utc() //current datetime in utc
msg.nowlocaloffset=moment().local().utcOffset() //difference from current datetime local to UTC
msg.nowoffset=moment().utcOffset() //difference from current datetime to UTC
msg.utcoffset=moment().utc().utcOffset() //difference from UTC datetime to UTC - has to be 0
return msg
Using the local() should get me my timezone date, and the utc() should get me the UTC. The utcOffset() gives me the difference between the date generated and UTC time.
The response is the following:
{"_msgid":"56f0ffb6.40749",
"topic":"","payload":1554965953702,
"nowlocal":"2019-04-11T06:59:13.702Z",
"now":"2019-04-11T06:59:13.702Z",
"utc":"2019-04-11T06:59:13.702Z"
,"nowlocaloffset":60,
"nowoffset":60,
"utcoffset":0}
The current time is 2019-04-11T07:59:13.702Z. How can the nowlocal and now dates have the same timestamp as the utc one and still report a +60minute shift from it?
Check to see what the timezone is set to on the machine you are running Node-RED server on. As Node-RED picks up the timezone from there,
If you are doing any database work it usually makes sense to leave the timezone as UTC so you don't get double entries / no entries when the timezone changes.
Current default time zone: 'Europe/Lisbon'
Local time is now: Thu Apr 11 09:04:15 WEST 2019.
Universal Time is now: Thu Apr 11 08:04:15 UTC 2019.
Even if it was wrong, that would not explain how two dates can have the same datetime value and have different utcoffsets - nowlocal and now vs utc in my example.
Since this problem appeared after Daylight Saving Time started, the issue must have something to do with it.
Notice the wording of that
"Sets a flag on the original moment to use local time to display a moment instead of the original moment's time".
It does not change the time, it just says that if, for example, you use moment's methods to get the number of hours then it will perform that conversion using the timezone data. Since you have not performed that sort of operation on it then there is no difference between them. If you were to look at msg.nowlocal.hours() and msg.utc.hours() then I think they should be different.
When you displayed the response, which I guess was via a debug node, then it has only seen the underlying Date object rather than the moment object that wraps the date, since a debug node does not know about moment objects, so when it converted the different flavours to strings for display it did not even see the flag, so it displayed them all the same.
Thank you, but this flow in particular is getting too cluttered. Since this is something that can be done easily in a function node along with other actions, I believe using the library is better suited.