Skip to content

Commit 48cf498

Browse files
committed
Add date and today
Add two new functions for working with dates, without having to care about times.
1 parent 02e8faa commit 48cf498

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

book/src/date-and-time.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ now() + 40 days
1313
now() - 1 million seconds
1414
1515
# How many days are left until September 1st?
16-
datetime("2024-11-01 12:30:00") - now() -> days
16+
date("2024-11-01") - today() -> days
1717
1818
# What time is it in Nepal right now?
1919
now() -> tz("Asia/Kathmandu") # use tab completion to find time zone names
@@ -58,7 +58,9 @@ April 1st"?
5858
The following functions are available for date and time handling:
5959

6060
- `now() -> DateTime`: Returns the current date and time.
61-
- `datetime(input: String) -> DateTime`: Parses a string into a `DateTime` object.
61+
- `today() -> DateTime`: Returns the current date at midnight (in the local time).
62+
- `datetime(input: String) -> DateTime`: Parses a string (date and time) into a `DateTime` object.
63+
- `date(input: String) -> DateTime`: Parses a string (only date) into a `DateTime` object.
6264
- `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.
6365
- `tz(tz: String) -> Fn[(DateTime) -> DateTime]`: Returns a timezone conversion function, typically used with the conversion operator (`datetime -> tz("Europe/Berlin")`)
6466
- `local(dt: DateTime) -> DateTime`: Timezone conversion function targeting the users local timezone (`datetime -> local`)
@@ -85,3 +87,11 @@ zone is specified, the local time zone is used.
8587
| `%Y/%m/%d %I:%M:%S%.f %p` | same, but with `/` separator |
8688
| `%Y-%m-%d %I:%M %p` | `2024-02-10 12:30 PM`<br>`2024-02-10 06:30 AM -0600`<br>`2024-02-10 07:30 AM US/Eastern` |
8789
| `%Y/%m/%d %I:%M %p` | same, but with `/` separator |
90+
91+
The `date` function supports the following formats. It returns a `DateTime` object with the time set to midnight in the
92+
specified timezone (or the local timezone if no timezone is specified).
93+
94+
| Format | Examples |
95+
| ------ | ------- |
96+
| `%Y-%m-%d` | `2024-02-10`<br>`2024-02-10 +0100`<br>`2024-02-10 Europe/Berlin` |
97+
| `%Y/%m/%d` | `2024/02/10`<br>`2024/02/10 +0100`<br>`2024/02/10 Europe/Berlin` |

numbat/modules/datetime/functions.nbt

+12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
use core::strings
12
use units::si
23

34
fn now() -> DateTime
5+
46
fn datetime(input: String) -> DateTime
57
fn format_datetime(format: String, input: DateTime) -> String
8+
69
fn get_local_timezone() -> String
710
fn tz(tz: String) -> Fn[(DateTime) -> DateTime]
811
let local: Fn[(DateTime) -> DateTime] = tz(get_local_timezone())
912
let UTC: Fn[(DateTime) -> DateTime] = tz("UTC")
13+
1014
fn unixtime(input: DateTime) -> Scalar
1115
fn from_unixtime(input: Scalar) -> DateTime
16+
17+
fn _today_str() = format_datetime("%Y-%m-%d", now())
18+
fn today() -> DateTime = datetime("{_today_str()} 00:00:00")
19+
20+
fn date(input: String) -> DateTime =
21+
if str_contains(input, " ")
22+
then datetime(str_replace(input, " ", " 00:00:00 "))
23+
else datetime("{input} 00:00:00")

0 commit comments

Comments
 (0)