4 个版本
| 0.2.0 | 2024年7月30日 |
|---|---|
| 0.1.2 | 2024年7月30日 |
| 0.1.1 | 2024年4月23日 |
| 0.1.0 | 2024年3月16日 |
#43 in 模板引擎
242 个月下载量
在 ctgen 中使用
120KB
2.5K SLoC
handlebars-chrono
chrono DateTime 辅助函数,用于 handlebars-rust
快速入门
与 handlebars-rust v5.1.0 和 chrono v0.4.35 开发和测试。
可选功能
locale包含unstable-locales在chrono中,并启用locale参数以生成本地化时间戳timezone包含chrono-tz并启用解析 IANA 时区名称作为timezone参数
版本
版本 0.1.* 与 handlebars 5 兼容。
版本 0.2.* 与 handlebars 6 兼容。
包含
添加到 Cargo.toml
handlebars-chrono = { version = "^0", features = [ "locale", "timezone" ] }
注册
use handlebars::Handlebars;
use handlebars_chrono::HandlebarsChronoDateTime;
let mut h = Handlebars::new();
h.register_helper("datetime", Box::new(HandlebarsChronoDateTime));
行为
默认情况下,如果未向辅助函数提供参数,则生成一个 RFC3339 当前 UTC 时间戳。
参数
存在三组可能的参数,以各种不同的组合形式存在
- 初始化器:控制
chrono::DateTime如何构建的参数。所有初始化器都内部生成一个DateTime<Utc> - 修饰符:修改内部构建的
DateTime<Utc>的参数。在此阶段,内部DateTime转换为DateTime<FixedOffset>以支持所有可能的修饰符和终结符。默认情况下,FixedOffset保持在 UTC 上。 - 终结器:确定内部
DateTime以String形式输出的参数
初始化器
默认初始化器等同于 Utc::now() 并生成当前时间的 DateTime<Utc>
其他可能的初始化器
from_timestamp:以秒为值的 UNIX 时间戳from_timestamp_millis:以毫秒为值的 UNIX 时间戳from_timestamp_micros:以微秒为值的 UNIX 时间戳from_timestamp_nanos:以纳秒为值的 UNIX 时间戳from_rfc2822:以有效的 RFC2822 格式化的字符串为值from_rfc3339:以有效的 RFC3339 格式化的字符串为值from_str+input_format:以任意格式的日期时间字符串及其对应格式为值
如果选定的初始化器包含时区或偏移信息,它将被丢弃并转换为等效的 UTC DateTime。
修饰符
默认情况下,不应用任何修饰符。每个修饰符只能应用一次。任何修饰符的变化都可以一起使用。
可能的修饰符
with_timezone:设置内部DateTime的偏移量。可能的值有:local(本地时间)、有效的固定偏移量(例如-06:00)或有效的 IANA 时区,如果启用了timezone功能(例如America/Edmonton)。with_ordinal:将日期设置为一年中的特定一天,从 1 开始。with_ordinal0:将日期设置为一年中的特定一天,从 0 开始。with_year:将日期设置为特定的年份。with_month:将日期设置为特定的月份,从 1 开始。with_month0:将日期设置为特定的月份,从 0 开始。with_day:将日期设置为一个月中的特定一天,从 1 开始。with_day0:将日期设置为一个月中的特定一天,从 0 开始。with_hour:将时间设置为一天中的特定小时。with_minute:将时间设置为一小时中的特定分钟。with_second:将时间设置为一分钟中的特定秒。with_nanosecond:设置DateTime的纳秒段。add_months:添加给定数量的月份。add_weeks:添加给定数量的周。add_days:添加给定数量的天。add_hours:添加给定数量的小时。add_minutes:添加给定数量的分钟。add_seconds:添加给定数量的秒。add_milliseconds:添加给定数量的毫秒。add_microseconds:添加给定数量的微秒。add_nanoseconds:添加给定数量的纳秒。sub_months:减去给定数量的月份。sub_weeks:减去给定数量的周。sub_days:减去给定数量的天。sub_hours:减去给定数量的小时。sub_minutes:减去给定数量的分钟。sub_seconds:减去指定秒数。sub_milliseconds:减去指定毫秒数。sub_microseconds:减去指定微秒数。sub_nanoseconds:减去指定纳秒数。
终结器
默认终结器等同于 DateTime::to_rfc3339(),并生成一个有效的 RFC3339 String。
其他可能的终结器
output_format+locale:使用一个 strftime 日期时间格式 作为输出格式。只有当locale功能启用时,locale参数才有效。在这种情况下,它接受 有效的区域名称。to_rfc2822:使辅助程序输出一个有效的 RFC2822 字符串。要成为有效的哈希参数,必须提供值。例如true,或1。该值被忽略。to_timestamp:使辅助程序输出以秒为单位的 UNIX 时间戳。要成为有效的哈希参数,必须提供值。例如true,或1。该值被忽略。to_timestamp_millis:使辅助程序输出以毫秒为单位的 UNIX 时间戳。要成为有效的哈希参数,必须提供值。例如true,或1。该值被忽略。to_timestamp_micros:使辅助程序输出以微秒为单位的 UNIX 时间戳。要成为有效的哈希参数,必须提供值。例如true,或1。该值被忽略。to_timestamp_nanos:使辅助程序输出以纳秒为单位的 UNIX 时间戳。要成为有效的哈希参数,必须提供值。例如true,或1。该值被忽略。years_since:接受一个 RFC3339 格式的日期时间,与内部DateTime进行比较并计算年份差异。必须提供years_since值,该值必须是过去更远的时间。
示例
当前日期和时间,格式为 RFC3339
{{datetime}}
当前日期和时间,格式类似于 2024-03-16 15:39:42
{{datetime output_format="%Y-%m-%d %H:%M:%S"}}
法语中的日历日期,例如 samedi, mars 16
{{datetime output_format="%A, %B %e" locale="fr_FR"}}
当前日期和时间,格式为 RFC2822,例如 Sat, 16 Mar 2024 21:43:31 +0000
{{datetime to_rfc2822=true}}
当前 UNIX 时间戳
{{datetime to_timestamp=true}}
1985年6月以来的年数
{{datetime years_since="1985-06-16T12:00:00Z"}}
将 UNIX 时间戳重新格式化为 MySQL 日期时间
{{datetime from_timestamp="618658211" output_format="%Y-%m-%d %H:%M:%S"}}
过去两个日期之间的年数
{{datetime from_timestamp="618658211" years_since="1985-06-16T12:00:00Z"}}
过去两个日期之间的年数,但仅使用 UNIX 时间戳
{{datetime from_timestamp="618658211" years_since=(datetime from_timestamp="487771200")}}
将毫秒级的 UNIX 时间戳转换为法语中的日历日期(例如 mercredi, août 9)
{{datetime from_timestamp_millis="618658211123" output_format="%A, %B %e" locale="fr_FR"}}
从RFC2822日期时间到MySQL日期时间
{{datetime from_rfc2822="Wed, 09 Aug 1989 09:30:11 +0200" output_format="%Y-%m-%d %H:%M:%S"}}
从RFC3339到RFC2822
{{datetime from_rfc3339="1989-08-09T09:30:11+02:00" to_rfc2822=true}}
从MySQL日期时间到RFC2822
{{datetime from_str="1989-08-09 09:30:11" input_format="%Y-%m-%d %H:%M:%S" to_rfc2822=true}}
从/到RFC3339并改变时区
{{datetime from_rfc3339="1989-08-09T09:30:11+02:00" with_timezone="America/Edmonton"}}
从八月切换到十一月
{{datetime from_timestamp="618658211" with_month="11" to_timestamp=true}}
从第九天切换到第十一天
{{datetime from_timestamp="618658211" with_day="11" to_timestamp=true}}
增加24个月
{{datetime from_timestamp="618658211" add_months="24" to_timestamp=true}}
减去4周
{{datetime from_timestamp="618658211" sub_weeks="4" to_timestamp=true}}
许可证
此库(handlebars-chrono)在BSD 2许可证下开源。
依赖
~4–5.5MB
~103K SLoC