Skip to content

Commit a26bfed

Browse files
sharkdpDavid Peter
authored and
David Peter
committed
Add 'time' function
1 parent 7a354f3 commit a26bfed

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

book/src/date-and-time.md

+11
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The following functions are available for date and time handling:
6161
- `today() -> DateTime`: Returns the current date at midnight (in the local time).
6262
- `datetime(input: String) -> DateTime`: Parses a string (date and time) into a `DateTime` object.
6363
- `date(input: String) -> DateTime`: Parses a string (only date) into a `DateTime` object.
64+
- `time(input: String) -> DateTime`: Parses a string (only time) into a `DateTime` object.
6465
- `format_datetime(format: String, dt: DateTime) -> String`: Formats a `DateTime` object as a string. See [this page](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers) for possible format specifiers.
6566
- `tz(tz: String) -> Fn[(DateTime) -> DateTime]`: Returns a timezone conversion function, typically used with the conversion operator (`datetime -> tz("Europe/Berlin")`)
6667
- `local(dt: DateTime) -> DateTime`: Timezone conversion function targeting the users local timezone (`datetime -> local`)
@@ -95,3 +96,13 @@ specified timezone (or the local timezone if no timezone is specified).
9596
| ------ | ------- |
9697
| `%Y-%m-%d` | `2024-02-10`<br>`2024-02-10 +0100`<br>`2024-02-10 Europe/Berlin` |
9798
| `%Y/%m/%d` | `2024/02/10`<br>`2024/02/10 +0100`<br>`2024/02/10 Europe/Berlin` |
99+
100+
The `time` function supports the following formats. It returns a `DateTime` object with the date set to the current date.
101+
If no timezone is specified, the local timezone is used.
102+
103+
| Format | Examples |
104+
| ------ | ------- |
105+
| `%H:%M:%S%.f` | `12:30:00`<br>`06:30:00 -0600`<br>`07:30:00 US/Eastern`<br>`12:30:00.123456` |
106+
| `%H:%M` | `12:30`<br>`06:30 -0600`<br>`07:30 US/Eastern` |
107+
| `%I:%M:%S%.f %p` | `12:30:00 PM`<br>`06:30:00 AM -0600`<br>`07:30:00 AM US/Eastern`<br>`12:30:00.123456 PM` |
108+
| `%I:%M %p` | `12:30 PM`<br>`06:30 AM -0600`<br>`07:30 AM US/Eastern` |

book/src/list-functions.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,7 @@ fn sphere_volume<L>(radius: L) -> L^3
101101

102102
## Date and time
103103

104-
```nbt
105-
fn now() -> DateTime
106-
fn datetime(input: String) -> DateTime
107-
fn format_datetime(format: String, dt: DateTime) -> String
108-
fn tz(tz: String) -> Fn[(DateTime) -> DateTime]
109-
fn local(dt: DateTime) -> DateTime
110-
fn get_local_timezone() -> String
111-
fn from_unixtime(t: Scalar) -> DateTime
112-
fn unixtime(dt: DateTime) -> Scalar
113-
fn human(t: Time) -> String
114-
```
104+
See [this page](date-and-time.md) for details.
115105

116106
## Physics
117107

numbat/modules/datetime/functions.nbt

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ fn date(input: String) -> DateTime =
2121
if str_contains(input, " ")
2222
then datetime(str_replace(input, " ", " 00:00:00 "))
2323
else datetime("{input} 00:00:00")
24+
25+
fn time(input: String) -> DateTime =
26+
datetime("{_today_str()} {input}")

0 commit comments

Comments
 (0)