Convert TimeZone to Universal Time Coordinated?

Hi,

I'm looking to convert TimeZone to Universal Time Coordinated (UTC) for one of my projects.
Initially, the data is an IP address, I'm using the node "node-red-contrib-ip-location-lite" to get a timezone. I'm looking to do this type of conversion:

* 'Europe/Paris' --------> 'GMT+1'
* 'America/Los_Angeles' -> 'PST'
* 'Pacific/Auckland' ----> 'GMT+13'
* 'America/Toronto ------> 'EST'

For now, I'm using many "if" conditions, but I'm sure it exists a better way.
I have the date and time for managing daylight saving (optional).

Thanks in advance for any subjection.

You can either use the moment node or the MomentJS library built into JSONata.

Alternatively, you can use Node.js's native INTL library in a function node if you either allow modules to be loaded in function nodes or add a require to it in the globals section of your settings.js.

1 Like

Hi @TotallyInformation ,

I tried to use the moment node but I didn't find a way to my conversion.
It looks more like for converting date/time than for my conversion. I may miss it.

Regarding your alternative, it's unfortunately outside of my "small" knowledge :frowning:

Hopping it exists as a simple way to get the TZ name to UTC offset, like this list:

Thanks

Have you tried .utc()
e.g $moment($$.payload).utc().format() in a change node and selecting JSONata J:
~
Scrub that after rereading.

If you provide an example flow of what you are doing we could probably show a simpler way/

1 Like

Been a while since I wrote or used it but I think you can specify an input tz and an output tz and it will convert automatically? Isn't that what you want?

Ah, re-reading, I see you want to work out what TZ the timestamp is coming from! My bad.

In that case, you need to convert the incoming IP to the 3 letter country code, then you can use that to steer the MomentJS conversion.

Something like this should do it: geoip-lite/node-geoip: Native NodeJS implementation of MaxMind's GeoIP API -- works in node 0.6.3 and above, ask me about other versions (github.com) - however, you will need to add a reference to it in Node-RED and then use a function node.

1 Like

Do you mean that you have a string that, for example, contains "Europe/Paris", and you want to convert that to the string "GMT+1"?

it would be easier if you send us a sample msg from that node to see its structure but luckily there is an example of the node's page

image

the expression $moment().tz($$.location.timezone).format("z Z") in a Change node
will produce this format CST -06:00

test Flow:

[{"id":"3bbd3677c8b9707f","type":"inject","z":"a479605cda9a7cfd","name":"ip node","props":[{"p":"payload"},{"p":"location.timezone","v":"America/Chicago","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"123.123.123.123\"","payloadType":"str","x":250,"y":900,"wires":[["0b7720798a9f4ec7","4c6ca52050b3b727"]]},{"id":"0b7720798a9f4ec7","type":"change","z":"a479605cda9a7cfd","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz($$.location.timezone).format(\"z Z\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":900,"wires":[["1b814eaabdf51194"]]},{"id":"1b814eaabdf51194","type":"debug","z":"a479605cda9a7cfd","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":900,"wires":[]},{"id":"4c6ca52050b3b727","type":"debug","z":"a479605cda9a7cfd","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":420,"y":820,"wires":[]}]
1 Like

@UnborN, your code matches perfectly my needs!!!

Sorry, next time I will provide a code example of my issue.
Thanks a lot, everyone for your subjections!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.