Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class NodaTime.​DateTimeZone
Assembly: NodaTime
Inheritance: object → DateTimeZone
Implemented Interfaces
Represents a time zone - a mapping between UTC and local time. A time zone maps UTC instants to local times - or, equivalently, to the offset from UTC at any particular instant.
Remarks
<para>
The mapping is unambiguous in the "UTC to local" direction, but
the reverse is not true: when the offset changes, usually due to a Daylight Saving transition,
the change either creates a gap (a period of local time which never occurs in the time zone)
or an ambiguity (a period of local time which occurs twice in the time zone). Mapping back from
local time to an instant requires consideration of how these problematic times will be handled.
</para> <para>
Noda Time provides various options when mapping local time to a specific instant:
<list type="bullet"><item><description><see cref="M:NodaTime.DateTimeZone.AtStrictly(NodaTime.LocalDateTime)" /> will throw an exception if the mapping from local time is either ambiguous
or impossible, i.e. if there is anything other than one instant which maps to the given local time.</description></item><item><description><see cref="M:NodaTime.DateTimeZone.AtLeniently(NodaTime.LocalDateTime)" /> will never throw an exception due to ambiguous or skipped times,
resolving to the earlier option of ambiguous matches, or to a value that's forward-shifted by the duration
of the gap for skipped times.</description></item><item><description><see cref="M:NodaTime.DateTimeZone.ResolveLocal(NodaTime.LocalDateTime,NodaTime.TimeZones.ZoneLocalMappingResolver)" /> will apply a <see cref="T:NodaTime.TimeZones.ZoneLocalMappingResolver" /> to the result of
a mapping.</description></item><item><description><see cref="M:NodaTime.DateTimeZone.MapLocal(NodaTime.LocalDateTime)" /> will return a <see cref="T:NodaTime.TimeZones.ZoneLocalMapping" />
with complete information about whether the given local time occurs zero times, once or twice. This is the most
fine-grained approach, which is the fiddliest to use but puts the caller in the most control.</description></item></list></para> <para>
Noda Time has two built-in sources of time zone data available: a copy of the
<a href="https://www.iana.org/time-zones">tz database</a> (also known as the IANA Time Zone database, or zoneinfo
or Olson database), and the ability to convert .NET's own <see cref="T:System.TimeZoneInfo" /> format into a "native" Noda
Time zone. Which of these is most appropriate for you to use will very much depend on your exact needs. The
zoneinfo database is widely used outside Windows, and has more historical data than the Windows-provided
information, but if you need to interoperate with other Windows systems by specifying time zone IDs, you may
wish to stick to the Windows time zones.
</para> <para>
To obtain a <see cref="T:NodaTime.DateTimeZone" /> for a given timezone ID, use one of the methods on
<see cref="T:NodaTime.IDateTimeZoneProvider" /> (and see <see cref="T:NodaTime.DateTimeZoneProviders" /> for access to the built-in
providers). The UTC timezone is also available via the <see cref="P:NodaTime.DateTimeZone.Utc" /> property on this class.
</para> <para>
To obtain a <see cref="T:NodaTime.DateTimeZone" /> representing the system default time zone, you can either call
<see cref="M:NodaTime.IDateTimeZoneProvider.GetSystemDefault" /> on a provider to obtain the <see cref="T:NodaTime.DateTimeZone" /> that
the provider considers matches the system default time zone, or you can construct a
<see cref="T:NodaTime.TimeZones.BclDateTimeZone" /> via <see cref="M:NodaTime.TimeZones.BclDateTimeZone.ForSystemDefault" />, which returns a
<see cref="T:NodaTime.DateTimeZone" /> that wraps the system local <see cref="T:System.TimeZoneInfo" />. The latter will always
succeed, but has access only to that information available via the .NET time zone; the former may contain more
complete data, but may (in uncommon cases) fail to find a matching <see cref="T:NodaTime.DateTimeZone" />.
Note that <c>BclDateTimeZone</c> may not be available in all versions of Noda Time 1.x and 2.x; see the class
documentation for more details.
</para> <para>
Note that Noda Time does not require that <see cref="T:NodaTime.DateTimeZone" /> instances be singletons.
Comparing two time zones for equality is not straightforward: if you care about whether two
zones act the same way within a particular portion of time, use <see cref="T:NodaTime.TimeZones.ZoneEqualityComparer" />.
Additional guarantees are provided by <see cref="T:NodaTime.IDateTimeZoneProvider" /> and <see cref="M:NodaTime.DateTimeZone.ForOffset(NodaTime.Offset)" />.
</para>
Properties
public static
DateTimeZone
Utc
Gets the UTC (Coordinated Universal Time) time zone.
Remarks This is a single instance which is not provider-specific; it is guaranteed to have the ID "UTC", and to
compare equal to an instance returned by calling <see cref="M:NodaTime.DateTimeZone.ForOffset(NodaTime.Offset)" /> with an offset of zero, but it may
or may not compare equal to an instance returned by e.g. <c>DateTimeZoneProviders.Tzdb["UTC"]</c> .
public
string
Id
Get the provider's ID for the time zone.
Remarks <para>
This identifies the time zone within the current time zone provider; a different provider may
provide a different time zone with the same ID, or may not provide a time zone with that ID at all.
</para>
public
Offset
MinOffset
Gets the least (most negative) offset within this time zone, over all time.
public
Offset
MaxOffset
Gets the greatest (most positive) offset within this time zone, over all time.
Methods
public static
DateTimeZone
ForOffset​(Offset offset)
Returns a fixed time zone with the given offset.
Returns A fixed time zone with the given offset.
offset
The offset for the returned time zone
Remarks
<para>
The returned time zone will have an ID of "UTC" if the offset is zero, or "UTC+/-Offset"
otherwise. In the former case, the returned instance will be equal to <see cref="P:NodaTime.DateTimeZone.Utc" />.
</para> <para>
Note also that this method is not required to return the same <see cref="T:NodaTime.DateTimeZone" /> instance for
successive requests for the same offset; however, all instances returned for a given offset will compare
as equal.
</para>
public
Offset
GetUtcOffset​(Instant instant)
Returns the offset from UTC, where a positive duration indicates that local time is
later than UTC. In other words, local time = UTC + offset.
Returns The offset from UTC at the specified instant.
instant
The instant for which to calculate the offset.
Remarks
This is mostly a convenience method for calling <c>GetZoneInterval(instant).WallOffset</c> ,
although it can also be overridden for more efficiency.
public
ZoneInterval
GetZoneInterval​(Instant instant)
Gets the zone interval for the given instant; the range of time around the instant in which the same Offset
applies (with the same split between standard time and daylight saving time, and with the same offset).
Returns The defined <see cref="T:NodaTime.TimeZones.ZoneInterval" /> .
instant
The <see cref="T:NodaTime.Instant" /> to query.
Remarks
This will always return a valid zone interval, as time zones cover the whole of time.
public
ZoneLocalMapping
MapLocal​(LocalDateTime localDateTime)
Returns complete information about how the given <see cref="T:NodaTime.LocalDateTime" /> is mapped in this time zone.
Returns A mapping of the given local date and time to zero, one or two zoned date/time values.
localDateTime
The local date and time to map in this time zone.
Remarks
<para>
Mapping a local date/time to a time zone can give an unambiguous, ambiguous or impossible result, depending on
time zone transitions. Use the return value of this method to handle these cases in an appropriate way for
your use case.
</para> <para>
As an alternative, consider <see cref="M:NodaTime.DateTimeZone.ResolveLocal(NodaTime.LocalDateTime,NodaTime.TimeZones.ZoneLocalMappingResolver)" />, which uses a caller-provided strategy to
convert the <see cref="T:NodaTime.TimeZones.ZoneLocalMapping" /> returned here to a <see cref="T:NodaTime.ZonedDateTime" />.
</para>
public
ZonedDateTime
AtStartOfDay​(LocalDate date)
Returns the earliest valid <see cref="T:NodaTime.ZonedDateTime" /> with the given local date.
Returns The <see cref="T:NodaTime.ZonedDateTime" /> representing the earliest time in the given date, in this time zone.
date
The local date to map in this time zone.
Remarks
If midnight exists unambiguously on the given date, it is returned.
If the given date has an ambiguous start time (e.g. the clocks go back from 1am to midnight)
then the earlier ZonedDateTime is returned. If the given date has no midnight (e.g. the clocks
go forward from midnight to 1am) then the earliest valid value is returned; this will be the instant
of the transition.
public
ZonedDateTime
ResolveLocal​(LocalDateTime localDateTime,
ZoneLocalMappingResolver resolver)
Maps the given <see cref="T:NodaTime.LocalDateTime" /> to the corresponding <see cref="T:NodaTime.ZonedDateTime" /> , following
the given <see cref="T:NodaTime.TimeZones.ZoneLocalMappingResolver" /> to handle ambiguity and skipped times.
Returns The result of resolving the mapping.
localDateTime
The local date and time to map in this time zone.
resolver
The resolver to apply to the mapping.
Remarks
<para>
This is a convenience method for calling <see cref="M:NodaTime.DateTimeZone.MapLocal(NodaTime.LocalDateTime)" /> and passing the result to the resolver.
Common options for resolvers are provided in the static <see cref="T:NodaTime.TimeZones.Resolvers" /> class.
</para> <para>
See <see cref="M:NodaTime.DateTimeZone.AtStrictly(NodaTime.LocalDateTime)" /> and <see cref="M:NodaTime.DateTimeZone.AtLeniently(NodaTime.LocalDateTime)" /> for alternative ways to map a local time to a
specific instant.
</para>
public
ZonedDateTime
AtStrictly​(LocalDateTime localDateTime)
Maps the given <see cref="T:NodaTime.LocalDateTime" /> to the corresponding <see cref="T:NodaTime.ZonedDateTime" /> , if and only if
that mapping is unambiguous in this time zone. Otherwise, <see cref="T:NodaTime.SkippedTimeException" /> or
<see cref="T:NodaTime.AmbiguousTimeException" /> is thrown, depending on whether the mapping is ambiguous or the local
date/time is skipped entirely.
Returns The unambiguous matching <see cref="T:NodaTime.ZonedDateTime" /> if it exists.
localDateTime
The local date and time to map into this time zone.
Remarks
See <see cref="M:NodaTime.DateTimeZone.AtLeniently(NodaTime.LocalDateTime)" /> and <see cref="M:NodaTime.DateTimeZone.ResolveLocal(NodaTime.LocalDateTime,NodaTime.TimeZones.ZoneLocalMappingResolver)" /> for alternative ways to map a local time to a
specific instant.
public
ZonedDateTime
AtLeniently​(LocalDateTime localDateTime)
Maps the given <see cref="T:NodaTime.LocalDateTime" /> to the corresponding <see cref="T:NodaTime.ZonedDateTime" /> in a lenient
manner: ambiguous values map to the earlier of the alternatives, and "skipped" values are shifted forward
by the duration of the "gap".
Returns The unambiguous mapping if there is one, the earlier result if the mapping is ambiguous,
or the forward-shifted value if the given local date/time is skipped.
localDateTime
The local date/time to map.
Remarks
See <see cref="M:NodaTime.DateTimeZone.AtStrictly(NodaTime.LocalDateTime)" /> and <see cref="M:NodaTime.DateTimeZone.ResolveLocal(NodaTime.LocalDateTime,NodaTime.TimeZones.ZoneLocalMappingResolver)" /> for alternative ways to map a local time to a
specific instant.
<para>Note: The behavior of this method was changed in version 2.0 to fit the most commonly seen real-world
usage pattern. Previous versions returned the later instance of ambiguous values, and returned the start of
the zone interval after the gap for skipped value. The previous functionality can still be used if desired,
by using <see cref="M:NodaTime.DateTimeZone.ResolveLocal(NodaTime.LocalDateTime,NodaTime.TimeZones.ZoneLocalMappingResolver)" />, passing in a resolver
created from <see cref="P:NodaTime.TimeZones.Resolvers.ReturnLater" /> and <see cref="P:NodaTime.TimeZones.Resolvers.ReturnStartOfIntervalAfter" />.</para>
public
string
ToString​()
Returns the ID of this time zone.
Returns The ID of this time zone.
public
System.​Collections.​Generic.​IEnumerable<​ZoneInterval>
GetZoneIntervals​(Instant start,
Instant end)
Returns all the zone intervals which occur for any instant in the interval [ <paramref name="start" /> , <paramref name="end" /> ).
Returns A sequence of zone intervals covering the given interval.
start
Inclusive start point of the interval for which to retrieve zone intervals.
end
Exclusive end point of the interval for which to retrieve zone intervals.
Remarks
<para>This method is simply a convenience method for calling <see cref="M:NodaTime.DateTimeZone.GetZoneIntervals(NodaTime.Interval)" /> without
explicitly constructing the interval beforehand.
</para>
public
System.​Collections.​Generic.​IEnumerable<​ZoneInterval>
GetZoneIntervals​(Interval interval)
Returns all the zone intervals which occur for any instant in the given interval.
Returns A sequence of zone intervals covering the given interval.
interval
Interval to find zone intervals for. This is allowed to be unbounded (i.e.
infinite in both directions).
Remarks
<para>The zone intervals are returned in chronological order.
This method is equivalent to calling <see cref="M:NodaTime.DateTimeZone.GetZoneInterval(NodaTime.Instant)" /> for every
instant in the interval and then collapsing to a set of distinct zone intervals.
The first and last zone intervals are likely to also cover instants outside the given interval;
the zone intervals returned are not truncated to match the start and end points.
</para>
public
System.​Collections.​Generic.​IEnumerable<​ZoneInterval>
GetZoneIntervals​(Interval interval,
Options options)
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object