node-red-contrib-moment 中文文档教程

发布于 4年前 浏览 369 项目主页 更新于 3年前

node-red-contrib-moment

Node-RED 节点 moment 使用 Moment.JS 生成格式良好的日期/时间字符串图书馆。 该节点完全了解时区/DST/区域设置。

节点人性化将持续时间(时间跨度)转换为文本描述(例如2分钟)。

两个节点都了解输出字符串的语言。

node-appearance
图。 1:节点外观

“平台”NPM 版本NPM 总下载量每月 NPM 下载量GitHub 最后一次提交GitHub 星星GitHub 观察者GitHub 许可证最小节点版本包装质量依赖项未决问题Closed Issues

基于 Node-RED Google 群组中的对话。 在 Jaques44 的初始工作之后更新了时区/区域设置功能。 Google 网上论坛的另一次转化后进行了 +/- 调整。

Installation

In Node-RED (preferred)

  • Via Manage Palette -> Search for "node-red-contrib-moment"

In a shell

基本安装:

  • go to the Node-RED "userDir" folder, typically ~/.node-red
  • run npm install node-red-contrib-moment

要获取最新的开发版本,请安装:

  • run npm install TotallyInformation/node-red-contrib-moment

Usage of node moment

节点最简单的用法是向其提供时间戳注入:

moment-basic-usage

图 1 2:基本使用节点时刻

Updates

请参见CHANGELOG文档。

Usage

Moment

节点通常期望来自传入消息的输入。 默认情况下,这是 msg.payload。 如果它是可识别的日期/时间,它将应用一种格式并相应地输出结果字符串或对象。

输入和输出时区可与输出区域设置一样设置。 所有这些都默认为主机系统 tz/locale。

这允许使用节点从一个时区转换到另一个时区。 它还将考虑夏令时 (DST)。

您还可以通过添加或减去金额来对日期/时间进行调整。

Configuration of node moment

moment-node-settings

1 3:节点moment的属性

Input from and Output to

这两个配置属性定义了msg属性,其中输入和输出数据分别从resp读取。 写给. 默认为 msg.payload

Input Timezone and Output Timezone

Input Timezone

此属性定义通过输入 msg 馈送的时间的时区。 输入时间在内部被转换为 UTC 以便进一步处理。
输入时区的格式为地区/位置,例如欧洲/伦敦。 另请参阅时区列表,例如内置于 moment-timezone 或在 维基百科

注意:拼写未经过验证,如果它似乎不起作用,请使用这些时区列表检查地区/位置的有效性。

以下行为有效:

  • If the input data contains a Node-RED timestamp this property is ommitted
  • If the host system has a local timezone set (e.g. dpkg-reconfigure tzdata on Linux), the input timestamp is related to this local timezone.
  • If the host system has no local timezone set, the input timestamp is related to UTC.
  • If the input data contains an interpretable string, this property is used (to convert internally to UTC).

如果在设置中留空,则可以从传入的 msg.inTz 属性中设置此字段。

Output Timezone

此属性定义通过输出 msg 发出的时间的时区。 输出时区的格式如上所述(请参阅输入时区)。

以下行为有效:

  • 如果输出格式留空,则输出格式为“Zulu”格式,与附加属性输出时区的内容无关>区域设置

    祖鲁语格式请参阅:https://momentjs.com/docs/#/displaying/as-iso-string/
    (示例:2013-02-04T22:44:30.652Z)

如果在设置中留空,则可以从传入的 msg.outTz 属性中设置此字段。

Adjustment

使用此属性,可以通过手动给定值来调整时间。 调整可以是正的或负的,并且可以以毫秒、秒、分钟、小时、天、周、月、季度、年为单位给出。

Output Format and Locale

这两个属性的组合定义了输出 msg 中发出的输出格式。

Output Format

Output Format 属性定义格式,并在 moment.js 显示格式部分中进行了描述一个>。

它可以是 moment.js 定义的任何格式字符串。 还允许添加来自 moment-timezone 的格式。 此外,还允许使用其他格式字符串(不区分大小写,括号中的替代项)。

请注意,除了 ISO8601 之外,其他格式均采用指定的时区和时间。 夏令时。 如果未指定,输出时区/DST 与输入相同。

使用 UTC 的输出时区强制输出到该时区。

Format string defined by moment.js

moment.js 定义的格式字符串基本上有两个选项:

  • 手动给定格式字符串: 这是一个其中时间/日期部分由字符表示的字符串。 文本部分也是允许的。 示例:

  • “DD.MM.YYYY HH:mm”给出20.09.2020 08:30

  • “dddd, MMMM Do YYYY, h:mm:ss a”给出Sunday, February 14th 2010, 3:25:50 pm

  • “[Today is] dddd”给出 Today is Sunday

  • “[Date: ]YYYY-MM-DD [Time:]HH:mm:ss”给出 < em>日期:2020-09-20 时间:08:31:45

  • "fromNow"给出一个月内

  • “日历”给出上周一< /em>

  • 预定义本地化字符串: 这是定义本地化格式的字符串。 示例:

  • “LLL”给出 October 20, 2020 8:33 AM

  • “LTS”给出 8:30:25 PM

  • “llll”给出 Thu, Sep 4, 1986 8:30 PM

有关更多选项,请参阅 https://momentjs.com/docs/#/displaying/format/。

Format string is left blank resp. is "ISO8601" or "ISO"

在这种情况下,输出采用 ISO 8602 格式,例如“2015-01-28T16:24:48.123Z”。

请注意,ISO8601 格式的输出始终采用 UTC(“Z”,祖鲁时间),而不是本地时间,无论您指定哪个输出时区。

另请参阅 moment().toISOString()

Format string is "date" resp. "jsDate"

这是一个 Javascript Date 对象,格式为 {years:nnnn, Months:n, date:n, hours:n, 分钟:n, Seconds:n, milliseconds:n}.

它可用于手动(固定)数据/时间值。

警告:moment.js 有一种奇怪的对象格式,其中月份是从零开始的 (0-11),而不是像所有其他元素一样从 1 开始 (1-12)。 我目前不知道为什么,我提出了一个上游问题,但这似乎是出于某种奇怪的原因而故意做出的决定。

另请参阅 moment().toObject()

Format string is "fromNow" resp. "timeAgo"

这是人类可读的输出,例如 30 分钟前一个月内(此输出格式类型仅给出粗略的时间跨度,另请参阅人性化< /em> 下面的例子)。 时间跨度是根据实际时间和输入节点的时间得出的。

另请参阅 moment().fromNow()

Format string is "calendar" resp. "aroundNow"

这是人类可读的替代方案,例如上周一明天下午2:30。 请注意,一周后的日期将输出为 yyyy-mm-dd。

另请参阅 moment().calendar()

Format string is "date" resp. "jsDate"

此输出格式类型实际上不起作用(请参阅问题#37)。

Locale

如果是文本输出字符串内容,Locale 属性定义文本部分的语言(例如“October”、“Oktober”、“ottobre”、“lokakuuta”)。

如果输出以错误的格式显示,例如美国 mm/dd/yy 格式的日期,请更改输出区域设置。 例如,使用 en_gb 将强制短日期以 dd/mm/yy 格式输出。 默认值是 en,假定表示美国 :-(

另请参阅 区域设置帮助程序 (注意:并非支持给出的每个区域设置).

Topic (additional topic)

使用此属性,您可以向输出 msg.topic 添加附加主题。 生成的 msg 可能是(值“myTopicString”):

{"_msgid":"b16b00b5.bada55","payload":"2020-09-20T12:47:55.143Z","topic":"myTopicString"}

Input of node moment

对象中的输入值输入自可以是以下类型:

  • 时间戳: 当前日期/时间用作输入。

  • msg全局以及给定的属性为空或不存在:当前日期/时间用作输入。

  • JSON 日期时间对象: 该数据时间对象可能包含以下元素:小时分钟毫秒

    示例:{“年”:2020,“月”:1,“日期”:11,“小时”:5,“分钟”:6}

    如果未给出元素(例如对象中缺少),则使用实际时间值。

  • 包含可识别日期/时间字符串的属性: 该值将被解释和处理。

    示例:2020-02-11T05:06

  • 包含数值的属性: 该值将被假定为 UNIX 时间值(自 1970 年 1 月 1 日以来的毫秒数)。 备注:这是节点Inject在选项时间戳处发出的格式。

  • 包含不可识别日期/时间字符串的属性(包括null):然后不会发生任何转换,输出将是空字符串加上调试警告。

请注意,解析日期/时间字符串是一个难题。 moment.parseFormat 有帮助,但它并不神奇。 我们假设不明确的输入日期(例如 3/5/05)采用 EU/UK 格式 dd/mm/yy,除非输入时区位于美国或区域设置设置为 en_US。

Outputs of node moment

如果 output 属性不是 msg.payload,则输入 msg.payload 将保留在输出中。

如果配置属性输出格式不是日期,则日期/时间输出为格式化字符串。 jsDateobject 在这种情况下,输出是 Javascript 日期对象或分别如下所述的对象。

输出字符串格式由区域设置输出格式设置控制。 请注意,ISO8601 输出(默认)会忽略输出时区,此类输出始终采用 UTC。 对于其他格式,输出将采用指定的时区,默认为您的主机时区。

指定不同的输入和输出时区允许您在它们之间进行转换。

输出 msg 将传递输入 msg.topic,除非它被 Topic 配置属性覆盖。 如果输出到字段从默认的msg.payload更改,则输入msg.payload也将是通过传递。

Usage of node humanizer

该节点将输入时间跨度转换为人性化文本字符串并输出 msg.payload. humanized。 输出字符串的语言源自系统的语言环境,即它是不可更改的(如 moment 节点的 Locale 属性)。

另请参阅 moment.duration(). humanize()

(由 Laro88 贡献)

Configuration of node humanizer

 humanizer-node-settings
图。 4: 节点 humanizer 的属性

'Input variable'

该属性定义了用于转换的输入msg.payload 属性。 如果留空,则使用 msg.payload

Input of node humanizer

输入是定义时间跨度(以秒为单位)的数字。

Outputs of node humanizer

输出是 msg.payload. humanized 中的字符串对象。
时间跨度按时间间隔进行评估,有关详细信息,请参阅 humanizer 示例。

Examples


备注:示例流程位于示例子目录中。 在 Node-RED 中,可以通过导入功能导入它们,然后在垂直选项卡菜单中选择示例
所有示例流程也可以在此包的示例文件夹中找到。


Usage of the moment node

基本用法如图2所示。以下示例将概述如何使用丰富的配置属性。

Usage of configuration properties Output Timezone, Output Format and Adjustment

示例流程为:

Alt text
输出时区格式调整流程
图。 5:示例流程显示了输出时区输出格式调整

Usage of configuration property Input Timezone

用法示例流程为:

替代文本
输入时区流程
图。 6:示例流程显示输入时区的用法

Usage of configuration properties Output Format and Locale

示例流程为:

Alt text
输出格式区域设置流程
图。 7:示例流程显示输出格式区域设置的使用

Usage of the humanizer node

示例流程为:

替代文本
人性化流程
图。 8:示例流程显示了 humanizer 节点的用法

Depends on

To Do

我想对 moment 节点执行的操作摘要(不一定立即执行):

  • [ ] 添加一些用于执行日期操作的附加节点/时间计算 - 部分完成,可以从主节点进行简单的加/减

  • [ ] 添加附加节点以进行持续时间计算

  • [ ] 将组合框添加到格式字段,并预先填充常用格式

  • [x] 改进 Moment 时的错误消息.JS 无法解释输入(说出原因)

  • [x] 允许更多输入日期/时间格式 - 事实证明 Moment.JS 在这里并没有真正的帮助。 目前,我看到太多美国/英国日期格式等的输入失败。 如果我能够解析“人类”输入,例如“明天”和“从现在起 2 分钟后”,那就太好了。 我们现在可以输出它们,但不能输入它们。 从 v1.0.5 开始,支持本地化参数。

    ~~部分完成:添加了 parseFormat 插件。 失败了,详情见代码。~~现在完成了。

License

此代码是 Apache 2 许可证下的开源代码。 有关详细信息,请参阅 apache2-license.txt 文件

除非遵守许可证,否则您不得使用此代码。 您可以在以下位置获取许可证的原始副本:

http://www.apache.org/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则根据许可证分发的软件将在 “按原样”基础,不带任何明示或暗示的保证或条件。 请参阅 管理许可证下的权限和限制的特定语言的许可证。

Author

朱利安·奈特全部信息 ),https://github.com/totallyinformation

Contributors/Credits

非常感谢您的贡献。

Feedback and Support

请通过此存储库的 Github 问题列表报告任何问题或建议。

有关更多信息、反馈或社区支持,请参阅 Node-RED Google 群组论坛:https://groups.google.com/forum/#!forum/node-red

node-red-contrib-moment

Node-RED node moment produces a nicely formatted Date/Time string using the Moment.JS library. The node is fully time zone/DST/locale aware.

Node humanizer converts time durations (time spans) into textual descriptions (e.g. 2 minutes).

Both nodes are locale aware regarding the language of the output strings.

node-appearance
Fig. 1: Node appearance

PlatformNPM VersionNPM Total DownloadsNPM Downloads per monthGitHub last commitGitHub starsGitHub watchersGitHub licenseMin Node VersionPackage QualityDependenciesOpen IssuesClosed Issues

Based on thoughts from a conversation in the Node-RED Google Group. Updated with timezone/locale capabilities after Jaques44's initial work. Updated with +/- adjustments after another conversion in the Google Group.

Installation

In Node-RED (preferred)

  • Via Manage Palette -> Search for "node-red-contrib-moment"

In a shell

Basic installation:

  • go to the Node-RED "userDir" folder, typically ~/.node-red
  • run npm install node-red-contrib-moment

To get the latest development version, install with:

  • run npm install TotallyInformation/node-red-contrib-moment

Usage of node moment

The easiest usage of the node is feeding it with an timestamp inject:

moment-basic-usage

Fig. 2: Basic usage node moment

Updates

Please see the CHANGELOG document.

Usage

Moment

The node generally expects an input from the incoming msg. By default, this is msg.payload. If it is a recognisable date/time, it will apply a format and output the resulting string or object accordingly.

Input and output time zones are settable as is the output locale. All of which default to the host systems tz/locale.

This allows the node to be used to translate from one time zone to another. It also will take into account daylight savings time (DST).

You can also apply an adjustment to the date/time by adding or subtracting an amount.

Configuration of node moment

moment-node-settings

Fig. 3: Properties of node moment

Input from and Output to

These two configuration properties define the msg properties in which the input and output data are read from resp. written to. Default is msg.payload.

Input Timezone and Output Timezone

Input Timezone

This property defines the timezone of the time fed via the input msg. Internally the input time is converted into UTC for further processing.
The format of Input Timezone is in the format region/location, e.g. Europe/London. See also timezone lists e.g. built in to moment-timezone or given in wikipedia.

Note: Spellings are not validated, if it doesn't seem to work, check the validity of region/location with these timezone lists.

The following behaviour is valid:

  • If the input data contains a Node-RED timestamp this property is ommitted
  • If the host system has a local timezone set (e.g. dpkg-reconfigure tzdata on Linux), the input timestamp is related to this local timezone.
  • If the host system has no local timezone set, the input timestamp is related to UTC.
  • If the input data contains an interpretable string, this property is used (to convert internally to UTC).

If left blank in settings, this field may be set from the incoming msg.inTz property.

Output Timezone

This property defines the timezone of the time emitted via the output msg. The format of Output Timezone is described above (see Input Timezone).

The following behaviour is valid:

  • If Output Format is left blank, the output format is in 'Zulu' format, independent of the contents of the additional properties Output Timezone and Locale.

    Zulu format see: https://momentjs.com/docs/#/displaying/as-iso-string/
    (Example: 2013-02-04T22:44:30.652Z)

If left blank in settings, this field may be set from the incoming msg.outTz property.

Adjustment

Using this property, the time can be adjusted by a manually given value. Adjustments can be positive or negative and can be given in milliseconds, seconds, minutes, hours, days, weeks, months, quarters, years.

Output Format and Locale

These two properties in combination define the output format emitted in the output msg.

Output Format

The Output Format property defines the format and is described in the moment.js displaying format section.

It may be any format string defined by moment.js. The formatting additions from moment-timezone are also allowed. In addition, further (not case sensitive, alternatives in brackets) format strings are also allowed.

Note that with the exception of ISO8601, other formats are in the specified timezone & DST. If not specified, the output timezone/DST is the same as the input.

Use an output timezone of UTC to force output to that.

Format string defined by moment.js

The format string defined by moment.js basically has two options:

  • Manual given format string: This is a string where the time/date parts are represented by characters. Also text parts are allowed. Examples:

  • "DD.MM.YYYY HH:mm" gives 20.09.2020 08:30

  • "dddd, MMMM Do YYYY, h:mm:ss a" gives Sunday, February 14th 2010, 3:25:50 pm

  • "[Today is] dddd" gives Today is Sunday

  • "[Date: ]YYYY-MM-DD [Time: ]HH:mm:ss" gives Date: 2020-09-20 Time: 08:31:45

  • "fromNow" gives in a month

  • "calendar" gives Last Monday

  • Predefined localized string: This is a string which defines a localized format. Examples:

  • "LLL" gives October 20, 2020 8:33 AM

  • "LTS" gives 8:30:25 PM

  • "llll" gives Thu, Sep 4, 1986 8:30 PM

For more options see https://momentjs.com/docs/#/displaying/format/.

Format string is left blank resp. is "ISO8601" or "ISO"

In this case the output is in ISO 8602 format, e.g. "2015-01-28T16:24:48.123Z".

Note that ISO8601 formatted output is ALWAYS in UTC ('Z', Zulu time) not local, no matter what output timezone you may specify.

See also moment().toISOString().

Format string is "date" resp. "jsDate"

This is a Javascript Date object in the form {years:nnnn, months:n, date:n, hours:n, minutes:n, seconds:n, milliseconds:n}.

It may be used for manual (fixed) data/time values.

WARNING: moment.js has a bizarre object format where the month is zero-based (0-11) instead of 1-based (1-12) like all the other elements are. I don't currently know why, I've raised an upstream issue but this appears to be a deliberate decision for some strange reason.

See also moment().toObject().

Format string is "fromNow" resp. "timeAgo"

This is a human readable output, e.g. 30 minutes ago or in a month (only rough time spans are given in this output format type, see also the humanizer example below). The time span is derived from the actual time and the time fed into the node.

See also moment().fromNow().

Format string is "calendar" resp. "aroundNow"

This is a human readable alternative, e.g. Last Monday or Tomorrow 2:30pm. Note that dates beyond a week from now are output as yyyy-mm-dd.

See also moment().calendar().

Format string is "date" resp. "jsDate"

This output format type is actually not working (see issue #37).

Locale

In case of a textual output string contents the Locale property defines the language of the textual parts (e.g. "October" vs. "Oktober" vs. "ottobre" vs. "lokakuuta").

If the output is shown in the wrong format, such as dates in US mm/dd/yy format, change the output locale. For example, using en_gb will force short dates to output in dd/mm/yy format. The default is en which moment assumes means the USA :-(

See also Locale Helper (Note: Not every locale given there is supported).

Topic (additional topic)

Using this property you can add an additional topic to the output msg.topic. A resulting msg may be (value "myTopicString"):

{"_msgid":"b16b00b5.bada55","payload":"2020-09-20T12:47:55.143Z","topic":"myTopicString"}

Input of node moment

Input values in the object Input from can be of the following types:

  • timestamp: The current date/time is used as input.

  • msg, global or flow and the given property is empty or does not exist: The current date/time is used as input.

  • JSON date time object: This data time object may contain the following elements: years, months, days, hours, minutes, seconds, milliseconds.

    Example: {"years":2020,"months":1,"date":11,"hours":5,"minutes":6}.

    If elements are not given (e.g. years and months are missing in the object) the actual time values are used instead.

  • a property containing a string that is a recognizable date/time: The value will be interpreted and processed.

    Example: 2020-02-11T05:06

  • a property containing a numeric value: The value will be assumed to be a UNIX time value (ms since 1970-01-01). Remark: This is the format which the node Inject emits at option timestamp.

  • a property containing a string that is not a recognisable date/time (including null): Then no conversion takes place, the output will be an empty string plus a debug warning.

Note that parsing date/time strings is a hard problem. moment.parseFormat helps but it isn't magic. We assume that ambiguous input dates such as 3/5/05 are in EU/UK format dd/mm/yy unless either the input timezone is in America or the locale is set to en_US.

Outputs of node moment

If the output property is not msg.payload the input msg.payload is retained in the output.

The date/time output is a formatted string if the configuration property Output Format is anything other than date resp. jsDate or object in which case the output is a Javascript date object or an object as described below respectively.

Output string formatting is controlled by the Locale and the Output Format setting. Note that the output Timezone is ignored for ISO8601 output (the default), such output is always in UTC. For other formats, the output will be in the specified timezone which defaults to your host timezone.

Specifying different input and output timezones allows you to translated between them.

The output msg will pass through the input msg.topic unless it is overridden by the Topic configuration property. If the Output to field is changed from the default msg.payload, the input msg.payload will also be passed through.

Usage of node humanizer

This node converts an input time span to a humanized text string to the output msg.payload.humanized. The language of the output string is derived from the locale of the system, i.e. it is not changeable (like the Locale property of the moment node).

See also moment.duration().humanize().

(Contributed by Laro88)

Configuration of node humanizer

humanizer-node-settings
Fig. 4: Properties of node humanizer

'Input variable'

This property defines the input msg.payload property which shall be used for the conversion. If left blank, msg.payload is used.

Input of node humanizer

The input is a number which defines a time span in seconds.

Outputs of node humanizer

The output is a string object in msg.payload.humanized.
The time spans are evaluated in intervals, see humanizer example for details.

Examples


Remark: Example flows are present in the examples subdirectory. In Node-RED they can be imported via the import function and then selecting Examples in the vertical tab menue.
All example flows can also be found in the examples folder of this package.


Usage of the moment node

The basic usage is shown in Fig. 2. The following examples shall give an overview how to use the rich configuration properties.

Usage of configuration properties Output Timezone, Output Format and Adjustment

A sample flow is:

Alt text
output-timezone-format-adjustment flow
Fig. 5: Example flow showing the usage of Output Timezone, Output Format and Adjustment

Usage of configuration property Input Timezone

A sample flow is:

Alt text
input-timezone flow
Fig. 6: Example flow showing the usage of Input Timezone

Usage of configuration properties Output Format and Locale

A sample flow is:

Alt text
output-format-locale flow
Fig. 7: Example flow showing the usage of Output Format and Locale

Usage of the humanizer node

A sample flow is:

Alt text
humanizer flow
Fig. 8: Example flow showing the usage of the humanizer node

Depends on

To Do

Summary of things I'd like to do with the moment node (not necessarily immediately):

  • [ ] Add some additional nodes for doing date/time calculations - partly complete, can do simple add/subtract from main node

  • [ ] Add additional node for doing duration calculations

  • [ ] Add a combo box to the Format field with common formats pre-populated

  • [x] Improve the error messages when Moment.JS fails to interpret the input (say why)

  • [x] Allow more input date/time formats - turns out Moment.JS doesn't really help here. At present, I see too many input failures from US/UK date formats, etc. It would be great if I could parse "human" inputs like "tomorrow" and "2 minutes from now". We can output them now but not input them. As of v1.0.5, a localisation parameter is supported.

    ~~Partly complete: Added the parseFormat plugin. That failed, see code for details.~~ Now complete.

License

This code is Open Source under an Apache 2 License. Please see the apache2-license.txt file for details.

You may not use this code except in compliance with the License. You may obtain an original copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Please see the License for the specific language governing permissions and limitations under the License.

Author

Julian Knight (Totally Information), https://github.com/totallyinformation

Contributors/Credits

Many thanks for the contributions.

Feedback and Support

Please report any issues or suggestions via the Github Issues list for this repository.

For more information, feedback, or community support see the Node-RED Google groups forum at https://groups.google.com/forum/#!forum/node-red

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文