OffsetTime class

The time of the day with an offset from UTC/Greenwich, such as 10:15+08:00.

An OffsetTime is immutable. Time is stored to microsecond precision.

Working with OffsetTimes

To create an OffsetTime:

final now = OffsetTime.now();
final moonLanding = OffsetTime(Offset(-6), 18, 4);

You can add and subtract different types of time intervals, including:

OffsetTime behaves the same for all 3 types of time intervals. All calculations wrap around midnight.

final later = now.add(const Duration(hours: 1);
final evenLater = now + const Period(hour: 2);
final latest = now.plus(hours: 3);

OffsetTime can be compared with isBefore, isSameMomentAs and isAfter. Two OffsetTime may be in different timezones but still represent the same moment. However, they are only considered equal, using ==, if they represent the same moment in the same timezone.

print(now.isBefore(later)); // true
print(latest.isAfter(evenLater)); // true

You can also truncate, round, ceil and floor OffsetTime.

print(moonLanding.truncate(to: TimeUnit.hours); // 18:00-06:00
print(moonLanding.round(TimeUnit.minutes, 5);   // 18:05-06:00
print(moonLanding.ceil(TimeUnit.minutes, 5);    // 18:05-06:00
print(moonLanding.floor(TimeUnit.minutes, 5);   // 18:00-06:00

Constructors

OffsetTime(Offset offset, [int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0])
Creates a OffsetTime.
OffsetTime.fromDayMicroseconds(Offset offset, DayMicroseconds microseconds)
Creates a OffsetTime with the offset and microseconds since midnight, wrapping around midnight.
OffsetTime.fromDayMilliseconds(Offset offset, DayMilliseconds milliseconds)
Creates a OffsetTime with the offset and milliseconds since midnight, wrapping around midnight.
OffsetTime.now()
Creates a OffsetTime that represents the current time.

Properties

hashCode int
The hash code for this object.
no setteroverride
hour int
The hour.
no setterinherited
microsecond int
The microsecond.
no setterinherited
millisecond int
The millisecond.
no setterinherited
minute int
The minute.
no setterinherited
offset Offset
The offset.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
second int
The second.
no setterinherited

Methods

add(Duration duration) OffsetTime
Returns a copy of this with the duration added, wrapping around midnight.
ceil(TimeUnit unit, int value) OffsetTime
Returns a copy of this ceiled to the nearest unit and value.
copyWith({Offset? offset, int? hour, int? minute, int? second, int? millisecond, int? microsecond}) OffsetTime
Returns a copy of this with the updated offset and units of time.
difference(OffsetTime other) Duration
Returns the difference between this and other.
floor(TimeUnit unit, int value) OffsetTime
Returns a copy of this floored to the nearest unit and value.
isAfter(OffsetTime other) bool
Returns true if this is after other.
isBefore(OffsetTime other) bool
Returns true if this is before other.
isSameMomentAs(OffsetTime other) bool
Returns true if this occurs at the same moment as other.
minus({int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0, int microseconds = 0}) OffsetTime
Returns a copy of this with the units of time subtracted, wrapping around midnight.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
plus({int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0, int microseconds = 0}) OffsetTime
Returns a copy of this with the units of time added, wrapping around midnight.
round(TimeUnit unit, int value) OffsetTime
Returns a copy of this rounded to the nearest unit and value.
subtract(Duration duration) OffsetTime
Returns a copy of this with the duration subtracted, wrapping around midnight.
toLocal() LocalTime
Converts this OffsetTime to a LocalTime.
toString() String
Returns a ISO formatted string representation.
override
truncate({required TimeUnit to}) OffsetTime
Returns a copy of this truncated to the TimeUnit.

Operators

operator +(Period period) OffsetTime
Returns a copy of this with the period added.
operator -(Period period) OffsetTime
Returns a copy of this with the period subtracted.
operator ==(Object other) bool
Returns true if other is a OffsetTime at the same moment and in the same timezone.
override