2个不稳定版本
| 0.3.0 | 2023年12月1日 |
|---|---|
| 0.1.0 | 2018年9月27日 |
#64 在 日期和时间
994 每月下载量
在 3 crates 中使用
52KB
988 行
chrono systemd.time
该库将符合systemd.time规范的时戳解析为chrono类型。
时戳格式
支持的时戳格式包括systemd.time规范中定义的所有格式,但有少数例外
- 时间单位 必须 伴随所有时间跨度值。
- 不支持时区后缀。
- 不支持星期前缀。
时戳的格式可以是时间、时间跨度,或者时间 +/- 时间跨度的组合。
- 当只给出时间时,返回解析的时间。
- 当只给出时间跨度时,从当前时间(现在)添加或减去时间跨度。
- 当给出时间和时间跨度的组合时,从解析的时间添加或减去时间跨度。
以下为解析有效时戳的示例,假设现在为2018-06-21 01:02:03
parse_timestamp_tz("2018-08-20 09:11:12.123", Utc) == "2018-08-20T09:11:12.000123Z"
parse_timestamp_tz("2018-08-20 09:11:12", Utc) == "2018-08-20T09:11:12Z"
parse_timestamp_tz("18-08-20 09:11:12 +2m", Utc) == "2018-08-20T09:13:12Z"
parse_timestamp_tz("2018-08-20 + 1h2m3s", Utc) == "2018-08-20T01:02:03Z"
parse_timestamp_tz("18-08-20 - 1h 2m 3s", Utc) == "2018-08-19T22:57:57Z"
parse_timestamp_tz("09:11:12 -1day", Utc) == "2018-06-20T09:11:12Z"
parse_timestamp_tz("09:11:12.123", Utc) == "2018-06-21T09:11:12.000123Z"
parse_timestamp_tz("11:12", Utc) == "2018-06-21T11:12:00Z"
parse_timestamp_tz("now", Utc) == "2018-06-21T01:02:03.203918151Z"
parse_timestamp_tz("today", Utc) == "2018-06-21T00:00:00Z"
parse_timestamp_tz("yesterday -2days", Utc) == "2018-06-18T00:00:00Z"
parse_timestamp_tz("tomorrow +1week", Utc) == "2018-06-29T00:00:00Z"
parse_timestamp_tz("epoch +1529578800s", Utc) == "2018-06-21T11:00:00Z"
parse_timestamp_tz("@1529578800s", Utc) == "2018-06-21T11:00:00Z"
parse_timestamp_tz("now +4h50m", Utc) == "2018-06-21T05:52:03.203918151Z"
parse_timestamp_tz("4h50m left", Utc) == "2018-06-21T05:52:03.203918151Z"
parse_timestamp_tz("+4h50m", Utc) == "2018-06-21T05:52:03.203918151Z"
parse_timestamp_tz("now -3s", Utc) == "2018-06-21T01:02:00.203918151Z"
parse_timestamp_tz("3s ago", Utc) == "2018-06-21T01:02:00.203918151Z"
parse_timestamp_tz("-3s", Utc) == "2018-06-21T01:02:00.203918151Z"
时间
时间的语法由一组关键字和strftime格式组成
"now","epoch""today","yesterday","tomorrow""%y-%m-%d %H:%M:%S","%Y-%m-%d %H:%M:%S""%y-%m-%d %H:%M","%Y-%m-%d %H:%M""%y-%m-%d","%Y-%m-%d""%H:%M:%S""%H:%M"
包含秒数的strftime时间戳也可以包含微秒数,由'.'分隔。
- 省略日期时,默认为今天。
- 省略时间时,默认为00:00:00。
有效时间的示例(假设当前时间为2018-06-21 01:02:03)
"2018-08-20 09:11:12.123" == "2018-08-20T09:11:12.000123"
"2018-08-20 09:11:12" == "2018-08-20T09:11:12"
"18-08-20 09:11:12" == "2018-08-20T09:11:12"
"2018-08-20" == "2018-08-20T00:00:00"
"18-08-20" == "2018-08-20T00:00:00"
"09:11:12" == "2018-06-21T09:11:12"
"09:11:12.123" == "2018-06-21T09:11:12.000123"
"11:12" == "2018-06-21T11:12:00"
"now" == "2018-06-21T01:02:03.203918151"
"epoch" == "1970-01-01T00:00:00"
"today" == "2018-06-21T00:00:00"
"yesterday" == "2018-06-20T00:00:00"
"tomorrow" == "2018-06-22T00:00:00"
时间跨度
时间跨度由以下时间单位组合而成,以下时间单位被理解
"usec","us","µs""msec","ms""seconds","second","sec","s""minutes","minute","min","m""hours","hour","hr","h""days","day","d""weeks","week","w""months","month","M"(定义为30.44天)"years","year","y"(定义为365.25天)
时间跨度的所有部分相加。
有效时间跨度的示例
"3hours" == Duration::hours(3)
"2d 5h" == Duration::days(2) + Duration::hours(5)
"1y 10 months" == Duration::years(1) + Duration::months(10)
"30m22s" == Duration::minutes(30) + Duration::seconds(22)
"10m 2s 5m" == Duration::minutes(15) + Duration::seconds(2)
"10d 2 5m" == Duration::days(10) + Duration::minutes(25)
依赖关系
~1MB
~18K SLoC