Skip to content

Commit 0e887c7

Browse files
sharkdpDavid Peter
authored and
David Peter
committed
Add documentation for datetime handling
1 parent 67e77b2 commit 0e887c7

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [Unit conversions](./unit-conversions.md)
3131
- [Function definitions](./function-definitions.md)
3232
- [Conditionals](./conditionals.md)
33+
- [Date and time](./date-and-time.md)
3334
- [Printing, testing, debugging](./procedures.md)
3435
- [Advanced](./advanced.md)
3536
- [Unit definitions](./unit-definitions.md)

book/src/date-and-time.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Date and time
2+
3+
Numbat supports date and time handling based on the [proleptic Gregorian calendar](https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar),
4+
which is the (usual) Gregorian calendar extended to dates before its introduction in 1582. Julian calendar dates are currently not supported.
5+
6+
A few examples of useful operations that can be performed on dates and times:
7+
8+
```nbt
9+
# Which date is 40 days from now?
10+
now() + 40 days
11+
12+
# Which date was 1 million seconds ago?
13+
now() - 1 million seconds
14+
15+
# How many days are left until September 1st?
16+
parse_datetime("2024-11-01 12:30:00") - now() -> days
17+
18+
# What time is it in Nepal right now?
19+
now() -> "Asia/Kathmandu"
20+
21+
# What is the local time when it is 2024-11-01 12:30:00 in Australia?
22+
parse_datetime("2024-11-01 12:30:00 Australia/Sydney") -> "local"
23+
24+
# What is the current UNIX timestamp?
25+
now() // to_unixtime
26+
27+
# What is the date corresponding to the UNIX timestamp 1707568901?
28+
from_unixtime(1707568901)
29+
```
30+
31+
As you can see from the examples, datetimes are either created using the `now()` function or by
32+
parsing a string using the `parse_datetime` function. The following formats are supported.
33+
UTC offsets are mandatory for the RFC 3339 and RFC 2822 formats. The other formats can optionally
34+
include a time zone name or UTC offset. If no time zone is specified, the local time zone is used.
35+
36+
| Format | Examples |
37+
| ------ | ------- |
38+
| [RFC 3339](https://tools.ietf.org/html/rfc3339) | `2024-02-10T12:30:00Z`<br>`2024-02-10T06:30:00-06:00` |
39+
| [RFC 2822](https://tools.ietf.org/html/rfc2822) | `Sat, 10 Feb 2024 12:30:00 Z`<br>`Sat, 10 Feb 2024 06:30:00 -0600` |
40+
| `%Y-%m-%d %H:%M:%S%.f` | `2024-02-10 12:30:00`<br>`2024-02-10 06:30:00 -0600`<br>`2024-02-10 07:30:00 US/Eastern`<br>`2024-02-10 12:30:00.123456` |
41+
| `%Y/%m/%d %H:%M:%S%.f` | same, but with `/` separator |
42+
| `%Y-%m-%d %H:%M` | `2024-02-10 12:30`<br>`2024-02-10 06:30 -0600`<br>`2024-02-10 07:30 US/Eastern` |
43+
| `%Y/%m/%d %H:%M` | same, but with `/` separator |
44+
| `%Y-%m-%d %I:%M:%S%.f %p` | `2024-02-10 12:30:00 PM`<br>`2024-02-10 06:30:00 AM -0600`<br>`2024-02-10 07:30:00 AM US/Eastern`<br>`2024-02-10 12:30:00.123456 PM` |
45+
| `%Y/%m/%d %I:%M:%S%.f %p` | same, but with `/` separator |
46+
| `%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` |
47+
| `%Y/%m/%d %I:%M %p` | same, but with `/` separator |
48+
49+
50+
## Date and time arithmetic
51+
52+
The following operations are supported for `DateTime` objects:
53+
54+
| Left | Operator | Right | Result |
55+
| ---- | -------- | ----- | ------ |
56+
| `DateTime` | `-` | `DateTime` | Duration between the two dates as a `Time` |
57+
| `DateTime` | `+` | `Time` | New `DateTime` by adding the duration to the date |
58+
| `DateTime` | `-` | `Time` | New `DateTime` by subtracting the duration from the date |
59+
| `DateTime` | `->` | `String` | Converts the datetime to the specified time zone |
60+
61+
## Date and time functions
62+
63+
The following functions are available for date and time handling:
64+
65+
- `now() -> DateTime`: Returns the current date and time.
66+
- `parse_datetime(input: String) -> DateTime`: Parses a string into a `DateTime` object.
67+
- `format_datetime(format: String, dt: DateTime) -> String`: Formats a `DateTime` object as a string.
68+
- `to_unixtime(dt: DateTime) -> Scalar`: Converts a `DateTime` to a UNIX timestamp.
69+
- `from_unixtime(ut: Scalar) -> DateTime`: Converts a UNIX timestamp to a `DateTime` object.

0 commit comments

Comments
 (0)