3 个不稳定版本
| 0.3.1 | 2023年11月16日 |
|---|---|
| 0.3.0 | 2023年11月16日 |
| 0.2.0 | 2023年9月4日 |
#91 in #chrono
每月 27 次下载
52KB
988 行
chrono systemd.time
此仓库是从 chrono-systemd-time 分支出来的,因为作者已经很久没有更新了。
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