|
| 1 | +# About |
| 2 | + |
| 3 | +Factor's [`calendar`][calendar] vocabulary is built around two tuples: |
| 4 | + |
| 5 | +- `timestamp` — a point in time (`year`, `month`, `day`, `hour`, |
| 6 | + `minute`, `second`, and a GMT offset). |
| 7 | +- `duration` — a span of time (the same fields, used as amounts). |
| 8 | + |
| 9 | +## Timestamps |
| 10 | + |
| 11 | +`<date>` builds a midnight timestamp; `>date<` splits one apart: |
| 12 | + |
| 13 | +``` |
| 14 | +<date> ( year month day -- timestamp ) |
| 15 | +>date< ( timestamp -- year month day ) |
| 16 | +``` |
| 17 | + |
| 18 | +```factor |
| 19 | +USING: calendar ; |
| 20 | +
|
| 21 | +2024 7 14 <date> >date< . ! => 14 (2024 and 7 below it) |
| 22 | +``` |
| 23 | + |
| 24 | +Common queries: |
| 25 | + |
| 26 | +```factor |
| 27 | +USING: calendar calendar.english sequences ; |
| 28 | +
|
| 29 | +2024 1 8 <date> day-of-week . ! => 1 (0 = Sunday .. 6 = Saturday) |
| 30 | +2024 1 8 <date> day-of-week day-names nth . ! => "Monday" |
| 31 | +2024 leap-year? . ! => t |
| 32 | +2024 2 1 <date> days-in-month . ! => 29 |
| 33 | +``` |
| 34 | + |
| 35 | +`day-names` (and `month-names`) live in |
| 36 | +[`calendar.english`][english]. |
| 37 | + |
| 38 | +## Durations and arithmetic |
| 39 | + |
| 40 | +Words like `days`, `weeks`, `hours`, and `minutes` build a `duration`, |
| 41 | +and `time+` / `time-` shift a timestamp by one: |
| 42 | + |
| 43 | +``` |
| 44 | +days ( x -- duration ) |
| 45 | +time+ ( timestamp duration -- timestamp ) |
| 46 | +``` |
| 47 | + |
| 48 | +```factor |
| 49 | +2024 2 28 <date> 1 days time+ >date< . ! => 29 (2024-02-29) |
| 50 | +``` |
| 51 | + |
| 52 | +Arithmetic carries across month and year boundaries, and two |
| 53 | +timestamps naming the same instant compare equal with `=`. |
| 54 | + |
| 55 | +Beyond these, `calendar` offers times of day (`set-time`, `+hour`), |
| 56 | +time-zone conversions (`>gmt`, `>local-time`), the current time |
| 57 | +(`now`), and — via `calendar.format` — parsing and formatting. |
| 58 | + |
| 59 | +[calendar]: https://docs.factorcode.org/content/vocab-calendar.html |
| 60 | +[english]: https://docs.factorcode.org/content/vocab-calendar.english.html |
0 commit comments