Time.plainTime
func Time.plainTime(hour: int = 0, minute: int = 0, second: int = 0, millisecond: int = 0, microsecond: int = 0, nanosecond: int = 0): Time.PlainTimeTime.plainTime returns the time of day with the given fields, each 0 when omitted. It panics if a field is out of range: hour 0 to 23, minute and second 0 to 59, the subsecond fields 0 to 999.
Details
- The hour is 0 to 23, the minute and the second 0 to 59, and each subsecond field 0 to 999. A field outside its range panics; nothing carries into the next field, so
minute: 90panics rather than giving 01:30. - A
Time.PlainTimehas no date and no zone. Combine it with a date in Time.plainDateTime, and that with a zone in Time.toZoned. - To build a time from text, use Time.parsePlainTime, which returns a
Resultinstead of panicking.
Basic examples
Time.plainTime(14, 3, 5) // 14:03:05
Time.plainTime(9, 30) // 09:30:00
Time.plainTime() // 00:00:00A fractional second renders to its last nonzero digit:
Time.plainTime(14, 3, 5, 500) // 14:03:05.5
Time.plainTime(23, 59, 59, 999, 999, 999) // 23:59:59.999999999Scope
Each subsecond field adds three digits:
Time.plainTime(second: 1, millisecond: 250) // 00:00:01.25
Time.plainTime(microsecond: 5) // 00:00:00.000005
Time.plainTime(nanosecond: 1) // 00:00:00.000000001The result renders as text in interpolation and String.from:
let alarm = Time.plainTime(6, 45)
"Alarm at ${alarm}" // "Alarm at 06:45:00"
String.from(alarm) // "06:45:00"Named arguments
Name the fields to set some and leave the ones before them at 0:
Time.plainTime(minute: 15) // 00:15:00
Time.plainTime(hour: 7, second: 30) // 07:00:30
Time.plainTime(12, millisecond: 120) // 12:00:00.12Applications
Build the half-hour slots of a morning:
let start = Time.plainTime(9)
let slots = Array.range(0, 4).map({ start.add(Time.duration(minutes: 30 * x)) })
println(slots)[09:00:00, 09:30:00, 10:00:00, 10:30:00]Turn a count of minutes since midnight into a time, wrapping past the end of the day:
func fromMinutes(total: int): Time.PlainTime {
Time.plainTime(total / 60 % 24, total % 60)
}
fromMinutes(510) // 08:30:00
fromMinutes(1500) // 01:00:00Check numbers from the outside world before constructing, so a bad field is an error and not a panic:
func clockTime(h: int, m: int): Result[Time.PlainTime, Error] {
if h < 0 || h > 23 {
return Result.fail("hour ${h} is not 0 to 23")
}
if m < 0 || m > 59 {
return Result.fail("minute ${m} is not 0 to 59")
}
Result.ok(Time.plainTime(h, m))
}
clockTime(7, 45) // Ok{ value: 07:45:00 }
clockTime(25, 0) // Err{ error: Error{ message: "hour 25 is not 0 to 23" } }Put a time on a date and in a zone:
let meeting = Time.plainDateTime(Time.plainDate(2026, 11, 11), Time.plainTime(10, 30))
let stockholm = Time.zone("Europe/Stockholm").unwrap()
println(meeting)
println(meeting.toZoned(stockholm))2026-11-11T10:30:00
2026-11-11T10:30:00+01:00[Europe/Stockholm]Check the current time of day against an opening time. Time.toPlainTime takes the time of day from a clock reading, and the output depends on when it runs:
let opens = Time.plainTime(9, 30)
let now = Time.nowIn(Time.localZone()).toPlainTime()
println(if Time.compare(now, opens) < 0 { "closed until ${opens}" } else { "open since ${opens}" })Measure the length of a working day:
Time.plainTime(9).until(Time.plainTime(17, 30)) // PT8H30MProperties and relations
The fields read back what was given, and the rendered text parses back to the same time:
let t = Time.plainTime(14, 3, 5, 120)
assert(t.hour() == 14 && t.minute() == 3 && t.second() == 5)
assert(t.millisecond() == 120)
assert(Time.parsePlainTime(String.from(t)).unwrap() == t)Omitted fields are 0, and two times with the same fields are ==:
assert(Time.plainTime() == Time.plainTime(0, 0, 0, 0, 0, 0))
assert(Time.plainTime(9, 30) == Time.plainTime(minute: 30, hour: 9))Possible issues
A field out of range panics; an hour of 24 is not midnight:
Time.plainTime(24) // panic: Time.plainTime: hour out of range: 24
Time.plainTime(0, 90) // panic: Time.plainTime: minute out of range: 90
Time.plainTime(12, minute: -5) // panic: Time.plainTime: minute out of range: -5
Time.plainTime(millisecond: 1000) // panic: Time.plainTime: millisecond out of range: 1000Time.with constrains a field instead of panicking:
Time.plainTime(9).with(minute: 90) // 09:59:00< does not apply to time values; order times with Time.compare:
let early = Time.plainTime(8)
let late = Time.plainTime(9)
println(early < late)
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.PlainTimelet times = [Time.plainTime(17), Time.plainTime(8, 30), Time.plainTime(12)]
times.sortWith(Time.compare) // [08:30:00, 12:00:00, 17:00:00]A Time.PlainTime has no date, so the time from 23:00 until 01:00 is 22 hours back, not two hours forward:
Time.plainTime(23).until(Time.plainTime(1)) // -PT22HSee also
Time.parsePlainTime · Time.toPlainTime · Time.plainDate · Time.plainDateTime · Time.with · Time.hour · Time.compare · Time.types.PlainTime · Time
