Skip to main content

Time


id: py-Time

Time

Represents a time of day without date or timezone — just hour, minute, second, and nanosecond. Use Time for daily schedules, business hours, or time-of-day comparisons.

Time instances are immutable — all modification methods return new instances.


At a Glance

# Current time
now = Time.now()

# Specific time
opening = Time.of(9, 0)
closing = Time.of(17, 30)

# From a string
parsed = Time.parse('14:30:00')

# Read from a message
start_time = message.getTime(dataDictionary.type.Schedule.START_TIME)

Properties

PropertyTypeDescription
hourintHour of day, 0–23
minuteintMinute of hour, 0–59
secondintSecond of minute, 0–59
nanointNanosecond of second, 0–999,999,999
t = Time.of(14, 30, 45, 500000000)

stream.log_info(f"{t.hour}:{t.minute}:{t.second}") # "14:30:45"
stream.log_info(f"Nanoseconds: {t.nano}") # 500000000

Creating Time

now(zone?)

Returns the current time.

now = Time.now()
ny_now = Time.now(TimeZone.of('America/New_York'))

of(hour, minute?, second?, nano?)

Creates a Time from components.

t1 = Time.of(9) # 09:00:00
t2 = Time.of(9, 30) # 09:30:00
t3 = Time.of(9, 30, 45) # 09:30:45
t4 = Time.of(9, 30, 45, 500000000) # 09:30:45.500000000

parse(value, format?)

Parses a string into a Time.

t1 = Time.parse('14:30')
t2 = Time.parse('14:30:45')
t3 = Time.parse('02:30 PM', 'hh:mm a')

Comparing Times

MethodReturnsDescription
isAfter(other)boolThis time is later
isBefore(other)boolThis time is earlier
isEqual(other)boolSame time of day
compareTo(other)intNegative, zero, or positive
opening = Time.of(9, 0)
closing = Time.of(17, 30)
now = Time.now()

if now.isAfter(opening) and now.isBefore(closing):
stream.log_info('Within business hours')

Adding Time

MethodDescription
plusHours(hours)Add hours
plusMinutes(minutes)Add minutes
plusSeconds(seconds)Add seconds
plusNanos(nanos)Add nanoseconds
plus(duration)Add a Duration
start = Time.of(9, 0)
end = start.plusHours(8) # 17:00:00
break_time = start.plusMinutes(15) # 09:15:00

Subtracting Time

MethodDescription
minusHours(hours)Subtract hours
minusMinutes(minutes)Subtract minutes
minusSeconds(seconds)Subtract seconds
minusNanos(nanos)Subtract nanoseconds
minus(duration)Subtract a Duration
closing = Time.of(17, 30)
warning = closing.minusMinutes(15) # 17:15:00 — send reminder

Changing Components

MethodDescription
withHour(hour)Set hour (0–23)
withMinute(minute)Set minute (0–59)
withSecond(second)Set second (0–59)
withNano(nano)Set nanosecond
t = Time.of(14, 30, 45)
on_the_hour = t.withMinute(0).withSecond(0) # 14:00:00

Formatting

toString(format?)

t = Time.of(14, 30, 45)

t.toString() # "14:30:45"
t.toString('HH:mm') # "14:30"
t.toString('hh:mm a') # "02:30 PM"
t.toString('HH:mm:ss.SSS') # "14:30:45.000"

See Also