Dates and times: date, time, datetime
Luma has three types for dates and times, one for each thing you may want to hold:
| Type | Holds | Looks like |
|---|---|---|
date | a day on the calendar | 2024-12-25 |
time | a time of day | 14:30:00 |
datetime | a day and a time | 2024-12-25 14:30:00 |
xmas: date = date(2024, 12, 25)
today: date = date(2024, 11, 26)
print(xmas)
print("${today.until(xmas).days()} days to Christmas")
print(xmas.format("Weekday, D. Month YYYY"))Output:
2024-12-25
29 days to Christmas
Wednesday, 25. December 2024Making them
now() gives the current date, time of day, or both, on the computer’s clock:
d: date = date.now() // 2026-02-09
t: time = time.now() // 16:45:30.250
dt: datetime = datetime.now() // 2026-02-09 16:45:30.250
utc: datetime = datetime.utc() // the same moment in UTCFrom numbers: year, month and day for a date; hour, minute, and optionally second and millisecond for a time; and the date’s numbers, then the time’s, for a datetime (the time’s numbers you leave out are 0):
print(date(2024, 12, 25))
print(time(9, 30))
print(time(9, 30, 15, 500))
print(datetime(2024, 12, 25, 18, 0))
print(datetime(2024, 12, 25))Output:
2024-12-25
09:30:00
09:30:15.500
2024-12-25 18:00:00
2024-12-25 00:00:00From text, with parse or convert:
d: date = date.parse("2024-12-25")
t: time = convert("14:30")
dt: datetime = datetime.parse("2024-12-25T14:30:00")
print(d)
print(t)
print(dt)Output:
2024-12-25
14:30:00
2024-12-25 14:30:00The text is read as:
| Type | Text |
|---|---|
date | YYYY-MM-DD: 2024-12-25 |
time | HH:MM, HH:MM:SS or HH:MM:SS.fff: 14:30, 14:30:05, 14:30:05.250 |
datetime | a date alone (midnight), or a date, a space or T, and a time |
Spaces around the text don’t matter. Anything else, or a day that doesn’t exist like 2023-02-29, raises error 11 (see Errors).
Parts
A method named for a part, in the singular, reads it:
dt: datetime = datetime(2024, 12, 25, 14, 30, 5)
print(dt.year())
print(dt.month())
print(dt.day())
print(dt.hour())
print(dt.minute())
print(dt.second())
print(dt.weekday())
print(dt.yearday())Output:
2024
12
25
14
30
5
3
360| Method | Gives |
|---|---|
year(), month(), day() | the date’s numbers; months count from 1 (January) |
hour(), minute(), second(), milli() | the time’s numbers; hours go from 0 to 23 |
weekday() | 1 for Monday up to 7 for Sunday |
yearday() | the day of the year, from 1 to 366 |
A date has the date’s parts and a time the time’s; a datetime has all of them.
Adding: days(3), hours(-5)
The same names in the plural add that many: dt.days(3) is three days later, dt.hours(-5) five hours earlier. They give a new value, which you keep by assigning it:
d: date = date(2024, 12, 25)
print(d.days(7))
print(d.days(-25))
print(d.weeks(2))
d = d.months(1)
print(d)
print(time(23, 30).hours(2))Output:
2025-01-01
2024-11-30
2025-01-08
2025-01-25
01:30:00The units are years, months, weeks, days, hours, minutes, seconds and millis. A date has the ones from years to days, a time the ones from hours to millis, a datetime all of them.
- Months and years keep the day of the month. When the new month is shorter, you get its last day: January 31 plus one month is February 28 (or 29).
- A time goes round at midnight: 23:30 plus two hours is 01:30.
Singular reads, plural adds. d.day(3) is a mistake the compiler points out:
d.day(3) // error: day() reads the day; to add days, write d.days(3)
d.days(3) // error: the result of days is not used: it gives a new date; write d = d.days(...)Counting: until and since
a.until(b) followed by a unit counts the whole units from a to b:
xmas: date = date(2024, 12, 25)
print(date(2024, 12, 1).until(xmas).days())
print(date(2024, 12, 1).until(xmas).weeks())
born: date = date(1990, 5, 15)
print(born.until(date(2026, 5, 14)).years())
print(born.until(date(2026, 5, 15)).years())
print(time(9, 0).until(time(17, 45)).hours())
print(time(9, 0).until(time(17, 45)).minutes())Output:
24
3
35
36
8
525a.since(b) counts the other way, from b to a. Both say the same; use the one that reads better:
start: date = date(2024, 1, 1)
today: date = date(2024, 3, 1)
print(today.since(start).days())
print(start.until(today).days())
print(today.until(start).days())Output:
60
60
-60- The count is in whole units, rounded toward zero: 8 hours 45 minutes is 8 hours. If
bcomes beforea, it is negative. - Years and months go by the calendar, so ages come out right: from 1990-05-15 to 2026-05-14 is 35 years, and a day later 36.
Helpers
| Method | Gives | For |
|---|---|---|
first(), last() | the first or last day of the month | date, datetime |
leap() | true if the year has a February 29 | date, datetime |
weekend() | true on Saturday and Sunday | date, datetime |
past(), future() | true if it is before or after now | date, datetime |
d: date = date(2024, 2, 10)
print(d.first())
print(d.last())
print(d.last().day())
print(d.leap())
print(d.weekend())Output:
2024-02-01
2024-02-29
29
true
truedeadline: datetime = datetime(2030, 6, 30, 17, 0)
if deadline.future() {
print("still time")
}Dates and times together
d.at(t) puts a date and a time together into a datetime; dt.date() and dt.time() take them apart:
d: date = date(2024, 12, 24)
dinner: datetime = d.at(time(18, 0))
print(dinner)
print(dinner.date())
print(dinner.time())Output:
2024-12-24 18:00:00
2024-12-24
18:00:00Comparing
==, !=, <, <=, > and >= compare two values of the same type: earlier is smaller.
xmas: date = date(2024, 12, 25)
print(date(2024, 12, 1) < xmas)
print(xmas == date.parse("2024-12-25"))
print(time(9, 0) >= time(17, 0))Output:
true
true
falseText: format
print, interpolation and convert to a str give the text shown above. A time shows its milliseconds only when it has some. format writes it your own way:
dt: datetime = datetime(2024, 12, 25, 9, 5)
print(dt.format("DD.MM.YYYY hh:mm"))
print(dt.format("Weekday, D. Month YYYY"))
print(dt.format("Wkd D Mon YY, h:mm"))Output:
25.12.2024 09:05
Wednesday, 25. December 2024
Wed 25 Dec 24, 9:05| In the pattern | Becomes |
|---|---|
YYYY, YY | the year: 2024, 24 |
MM, M | the month: 05, 5 |
DD, D | the day: 09, 9 |
hh, h | the hour, 0 to 23: 07, 7 |
mm, ss, SSS | the minute, second and millisecond: 05, 09, 250 |
Month, Mon | the month’s name: December, Dec |
Weekday, Wkd | the day’s name: Wednesday, Wed |
Everything else in the pattern is copied as it is. Names are in English. Since letters like D and M are replaced, put words outside the pattern: "Due: ${d.format("DD.MM")}".
Unix time
A Unix time counts the seconds since 1970-01-01 00:00 UTC. convert turns it into a datetime on the local clock and back:
dt: datetime = convert(1735122600) // 2024-12-25 12:30:00 in Estonia (UTC+2)
n: int = convert(dt) // 1735122600Only a datetime converts to and from an int; for a date, convert to a datetime and take .date(). Milliseconds are dropped on the way to an int.
The local clock
A datetime holds what a clock on the wall shows, without a time zone. now(), past(), future() and Unix times use the computer’s local time zone, which a program reads once when it starts:
- On Linux, from
/etc/localtime. TheTZenvironment variable chooses another zone, by name (TZ=America/New_York), as a file (TZ=/path/to/zone), or as a rule (TZ=EST5EDT,M3.2.0,M11.1.0). An unknown zone is UTC. - On Windows, from the system’s settings.
TZ=UTC luma run app.lumaTimes when the clocks change are taken as the zone’s rules say. A local time that is skipped in spring or happens twice in autumn converts to one of the Unix times around it.
In lists, maps, objects and JSON
The three types work like int everywhere: in lists, as map keys and set elements, as fields of objects. A field’s default is made with fixed numbers:
obj Meeting {
title: str
at: datetime = datetime(2025, 1, 6, 9, 0)
}
m: Meeting = Meeting { title: "Planning" }
m.at = m.at.days(7)
print(m)
days: {date: str} = {date(2024, 12, 25): "Christmas"}
print(days)
j: json = convert(m)
print(j)Output:
Meeting {title: "Planning", at: 2025-01-13 09:00:00}
{2024-12-25: "Christmas"}
{"title":"Planning","at":"2025-01-13 09:00:00"}In JSON, dates and times are strings with the text above, and are read back from such strings.
Errors
date(...), time(...) and datetime(...) with numbers that don’t make a real date or time raise error 12. With fixed numbers, the compiler already says so:
err: error = nil
day: int = 30
try err {
d: date = date(2024, 2, day)
}
print(err.code)
print(err.message)
try err {
d: date = convert("2023-02-29")
}
print(err.code)
print(err.message)Output:
12
invalid date: day 30 is not in February 2024 (1..29)
11
cannot convert "2023-02-29" to a dateReading text that isn’t a date or time raises error 11, like any convert that doesn’t fit.
Details
- Years go from 1 to 9999 when you make a date. Adding can go past them; such years are printed as plain numbers (
10000-01-01). - A
datetimeis exact to the millisecond. - Dates follow the Gregorian calendar, also for days before it was introduced.
date,timeanddatetimeare built-in names, so they can’t be names of variables or functions.- Most of what dates and times do is written in Luma itself and added to programs that use them. A program that only prints fixed dates doesn’t need it.
Not available yet
- Time zones other than the local one and UTC: a
datetimeholds no zone. - Reading phrases like “next monday” or “+3 days”.
promptandflagfor dates and times: read astrandconvertit.- On Windows,
TZis not read, and Unix times convert with the offset that applies now (summer or winter time), not the one of their own day.