ZonedDateTime.atOffset constructor
ZonedDateTime.atOffset(
- LocalDateTime localDateTime,
- DateTimeZone zone,
- Offset offset
Initializes a new instance of ZonedDateTime in the specified time zone from a given local time and offset. The offset is validated to be correct as part of initialization. In most cases a local time can only map to a single instant anyway, but the offset is included here for cases where the local time is ambiguous, usually due to daylight saving transitions.
-
localDateTime
: The local date and time. -
zone
: The time zone. -
offset
: The offset between UTC and local time at the desired instant. -
ArgumentError:
offset
is not a valid offset at the given local date and time.
Implementation
factory ZonedDateTime.atOffset(LocalDateTime localDateTime, DateTimeZone zone, Offset offset)
{
zone = Preconditions.checkNotNull(zone, 'zone');
Instant candidateInstant = ILocalDateTime.toLocalInstant(localDateTime).minus(offset);
Offset correctOffset = zone.getUtcOffset(candidateInstant);
// Not using Preconditions, to avoid building the string unnecessarily.
if (correctOffset != offset) {
throw ArgumentError('Offset $offset is invalid for local date and time $localDateTime in time zone ${zone.id} offset');
}
var offsetDateTime = OffsetDateTime(localDateTime, offset);
return ZonedDateTime._(offsetDateTime, zone);
}