Skip to content

Commit

Permalink
Add date and today
Browse files Browse the repository at this point in the history
Add two new functions for working with dates, without having to care
about times.
  • Loading branch information
sharkdp committed Feb 12, 2024
1 parent 02e8faa commit 48cf498
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 12 additions & 2 deletions book/src/date-and-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ now() + 40 days
now() - 1 million seconds
# How many days are left until September 1st?
datetime("2024-11-01 12:30:00") - now() -> days
date("2024-11-01") - today() -> days
# What time is it in Nepal right now?
now() -> tz("Asia/Kathmandu") # use tab completion to find time zone names
Expand Down Expand Up @@ -58,7 +58,9 @@ April 1st"?
The following functions are available for date and time handling:

- `now() -> DateTime`: Returns the current date and time.
- `datetime(input: String) -> DateTime`: Parses a string into a `DateTime` object.
- `today() -> DateTime`: Returns the current date at midnight (in the local time).
- `datetime(input: String) -> DateTime`: Parses a string (date and time) into a `DateTime` object.
- `date(input: String) -> DateTime`: Parses a string (only date) into a `DateTime` object.
- `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.
- `tz(tz: String) -> Fn[(DateTime) -> DateTime]`: Returns a timezone conversion function, typically used with the conversion operator (`datetime -> tz("Europe/Berlin")`)
- `local(dt: DateTime) -> DateTime`: Timezone conversion function targeting the users local timezone (`datetime -> local`)
Expand All @@ -85,3 +87,11 @@ zone is specified, the local time zone is used.
| `%Y/%m/%d %I:%M:%S%.f %p` | same, but with `/` separator |
| `%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` |
| `%Y/%m/%d %I:%M %p` | same, but with `/` separator |

The `date` function supports the following formats. It returns a `DateTime` object with the time set to midnight in the
specified timezone (or the local timezone if no timezone is specified).

| Format | Examples |
| ------ | ------- |
| `%Y-%m-%d` | `2024-02-10`<br>`2024-02-10 +0100`<br>`2024-02-10 Europe/Berlin` |
| `%Y/%m/%d` | `2024/02/10`<br>`2024/02/10 +0100`<br>`2024/02/10 Europe/Berlin` |
12 changes: 12 additions & 0 deletions numbat/modules/datetime/functions.nbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use core::strings
use units::si

fn now() -> DateTime

fn datetime(input: String) -> DateTime
fn format_datetime(format: String, input: DateTime) -> String

fn get_local_timezone() -> String
fn tz(tz: String) -> Fn[(DateTime) -> DateTime]
let local: Fn[(DateTime) -> DateTime] = tz(get_local_timezone())
let UTC: Fn[(DateTime) -> DateTime] = tz("UTC")

fn unixtime(input: DateTime) -> Scalar
fn from_unixtime(input: Scalar) -> DateTime

fn _today_str() = format_datetime("%Y-%m-%d", now())
fn today() -> DateTime = datetime("{_today_str()} 00:00:00")

fn date(input: String) -> DateTime =
if str_contains(input, " ")
then datetime(str_replace(input, " ", " 00:00:00 "))
else datetime("{input} 00:00:00")

0 comments on commit 48cf498

Please sign in to comment.