/// <summary> /// Reads an <see cref="DateTimeZone" /> value from the stream. /// </summary> /// <remarks> /// The value must have been written by <see cref="LegacyDateTimeZoneWriter.WriteTimeZone" />. /// </remarks> /// <returns>The <see cref="DateTimeZone" /> value from the stream.</returns> public DateTimeZone ReadTimeZone(string id) { int flag = ReadByte(); switch (flag) { case LegacyDateTimeZoneWriter.FlagTimeZoneFixed: return(FixedDateTimeZone.Read(this, id)); case LegacyDateTimeZoneWriter.FlagTimeZonePrecalculated: return(PrecalculatedDateTimeZone.ReadLegacy(this, id)); case LegacyDateTimeZoneWriter.FlagTimeZoneNull: return(null); // Only used when reading a tail zone case LegacyDateTimeZoneWriter.FlagTimeZoneCached: return(CachedDateTimeZone.ReadLegacy(this, id)); case LegacyDateTimeZoneWriter.FlagTimeZoneDst: return(DaylightSavingsDateTimeZone.ReadLegacy(this, id)); default: throw new InvalidNodaDataException("Unknown time zone flag type: " + flag); } }
public void ZoneIntervalNameDefaultsToZoneId() { var zone = new FixedDateTimeZone("id", Offset.FromHours(5)); var interval = zone.GetZoneInterval(NodaConstants.UnixEpoch); Assert.AreEqual("id", interval.Name); Assert.AreEqual("id", zone.Name); }
public void For_Id_FixedOffset_NonCanonicalId() { string id = "UTC+05:00:00"; DateTimeZone zone = FixedDateTimeZone.GetFixedZoneOrNull(id) !; Assert.AreEqual(zone, DateTimeZone.ForOffset(Offset.FromHours(5))); Assert.AreEqual("UTC+05", zone.Id); }
public void For_Id_FixedOffset() { string id = "UTC+05:30"; DateTimeZone zone = FixedDateTimeZone.GetFixedZoneOrNull(id) !; Assert.AreEqual(DateTimeZone.ForOffset(Offset.FromHoursAndMinutes(5, 30)), zone); Assert.AreEqual(id, zone.Id); }
public void FromDateTimeOffset() { DateTimeOffset dateTimeOffset = new DateTimeOffset(2011, 3, 5, 1, 0, 0, TimeSpan.FromHours(3)); DateTimeZone fixedZone = new FixedDateTimeZone(Offset.FromHours(3)); ZonedDateTime expected = fixedZone.AtStrictly(new LocalDateTime(2011, 3, 5, 1, 0, 0)); ZonedDateTime actual = ZonedDateTime.FromDateTimeOffset(dateTimeOffset); Assert.AreEqual(expected, actual); }
public void ExplicitNameAppearsInZoneInterval() { var zone = new FixedDateTimeZone("id", Offset.FromHours(5), "name"); var interval = zone.GetZoneInterval(NodaConstants.UnixEpoch); Assert.AreEqual("id", zone.Id); // Check we don't get this wrong... Assert.AreEqual("name", interval.Name); Assert.AreEqual("name", zone.Name); }
public void MinMaxOffsetsWithOtherTailZone() { var tailZone = new FixedDateTimeZone("TestFixed", Offset.FromHours(8)); var testZone = new PrecalculatedDateTimeZone("Test", new[] { FirstInterval, SecondInterval, ThirdInterval }, tailZone); Assert.AreEqual(Offset.FromHours(-5), testZone.MinOffset); Assert.AreEqual(Offset.FromHours(8), testZone.MaxOffset); }
public void FixedZone_Eastern() { var offset = Offset.FromHours(5); var rules = new List <ZoneRuleSet> { new ZoneRuleSet("GMT-5", offset, Offset.Zero, int.MaxValue, null) }; var zone = DateTimeZoneBuilder.Build("GMT-5", rules); FixedDateTimeZone fixedZone = (FixedDateTimeZone)zone; Assert.AreEqual(offset, fixedZone.Offset); }
public void FixedWesternZone() { string id = "Etc/GMT-4"; var zone = DateTimeZoneProviders.Tzdb[id]; Assert.AreEqual(id, zone.Id); Assert.IsInstanceOf <FixedDateTimeZone>(zone); FixedDateTimeZone fixedZone = (FixedDateTimeZone)zone; Assert.AreEqual(Offset.FromHours(4), fixedZone.Offset); }
/// <summary> /// Creates a fixed time zone for offsets -12 to +15 at every half hour, /// fixing the 0 offset as DateTimeZone.Utc. /// </summary> private static DateTimeZone[] BuildFixedZoneCache() { DateTimeZone[] ret = new DateTimeZone[FixedZoneCacheSize]; for (int i = 0; i < FixedZoneCacheSize; i++) { int offsetSeconds = i * FixedZoneCacheGranularitySeconds + FixedZoneCacheMinimumSeconds; ret[i] = new FixedDateTimeZone(Offset.FromSeconds(offsetSeconds)); } ret[-FixedZoneCacheMinimumSeconds / FixedZoneCacheGranularitySeconds] = Utc; return(ret); }
public void Read_NoNameInStream() { var ioHelper = DtzIoHelper.CreateNoStringPool(); var offset = Offset.FromHours(5); ioHelper.Writer.WriteOffset(offset); var zone = (FixedDateTimeZone)FixedDateTimeZone.Read(ioHelper.Reader, "id"); Assert.AreEqual("id", zone.Id); Assert.AreEqual(offset, zone.Offset); Assert.AreEqual("id", zone.Name); }
public void Roundtrip() { var ioHelper = DtzIoHelper.CreateNoStringPool(); var oldZone = new FixedDateTimeZone("id", Offset.FromHours(4), "name"); oldZone.Write(ioHelper.Writer); var newZone = (FixedDateTimeZone)FixedDateTimeZone.Read(ioHelper.Reader, "id"); Assert.AreEqual(oldZone.Id, newZone.Id); Assert.AreEqual(oldZone.Offset, newZone.Offset); Assert.AreEqual(oldZone.Name, newZone.Name); }
public void FixedZone_Eastern() { var offset = Offset.FromHours(5); var builder = new DateTimeZoneBuilder(); builder.SetStandardOffset(offset); builder.SetFixedSavings("GMT-5", Offset.Zero); var zone = builder.ToDateTimeZone("GMT-5"); FixedDateTimeZone fixedZone = (FixedDateTimeZone)zone; Assert.AreEqual(offset, fixedZone.Offset); }
public void CompareTo_DifferentZones_OnlyInstantMatters() { var otherZone = new FixedDateTimeZone(Offset.FromHours(-20)); ZonedDateTime value1 = SampleZone.AtStrictly(new LocalDateTime(2011, 1, 2, 10, 30)); // Earlier local time, but later instant ZonedDateTime value2 = otherZone.AtStrictly(new LocalDateTime(2011, 1, 2, 5, 30)); ZonedDateTime value3 = value1.WithZone(otherZone); Assert.That(value1.CompareTo(value2), Is.LessThan(0)); Assert.That(value2.CompareTo(value1), Is.GreaterThan(0)); Assert.That(value1.CompareTo(value3), Is.EqualTo(0)); }
public void GetZoneIntervals_GapAroundTailZoneTransition() { // Tail zone is fixed at +5. A local instant of one hour after the transition // from the precalculated zone (which is -5) will therefore give an instant from // the tail zone which occurs before the precalculated-to-tail transition, // and can therefore be ignored, resulting in an overall gap. var tailZone = new FixedDateTimeZone(Offset.FromHours(5)); var gapZone = new PrecalculatedDateTimeZone("Test", new[] { FirstInterval, SecondInterval, ThirdInterval }, tailZone); var actual = gapZone.GetZoneIntervalPair(ThirdInterval.LocalEnd); var expected = ZoneIntervalPair.NoMatch; Assert.AreEqual(expected, actual); }
public void ComparisonOperators_DifferentZones_AlwaysReturnsFalse() { // Note that the offsets will be the same as for SampleZone in the values we're using var otherZone = new FixedDateTimeZone(SampleZone.EarlyInterval.WallOffset); ZonedDateTime value1 = SampleZone.AtStrictly(new LocalDateTime(2011, 1, 2, 10, 30)); ZonedDateTime value2 = otherZone.AtStrictly(new LocalDateTime(2011, 1, 3, 10, 30)); // All inequality comparisons return false Assert.IsFalse(value1 < value2); Assert.IsFalse(value1 <= value2); Assert.IsFalse(value1 > value2); Assert.IsFalse(value1 >= value2); }
public void GetZoneIntervals_SingleIntervalAroundTailZoneTransition() { // Tail zone is fixed at +5. A local instant of one hour before the transition // from the precalculated zone (which is -5) will therefore give an instant from // the tail zone which occurs before the precalculated-to-tail transition, // and can therefore be ignored, resulting in an overall unambiguous time. var tailZone = new FixedDateTimeZone(Offset.FromHours(5)); var gapZone = new PrecalculatedDateTimeZone("Test", new[] { FirstInterval, SecondInterval, ThirdInterval }, tailZone); var pair = gapZone.GetZoneIntervalPair(ThirdInterval.LocalEnd - Duration.FromHours(1)); Assert.AreEqual(ThirdInterval, pair.EarlyInterval); Assert.IsNull(pair.LateInterval); }
public void MapLocal_SingleIntervalAroundTailZoneTransition() { // Tail zone is fixed at +5. A local instant of one hour before the transition // from the precalculated zone (which is -5) will therefore give an instant from // the tail zone which occurs before the precalculated-to-tail transition, // and can therefore be ignored, resulting in an overall unambiguous time. var tailZone = new FixedDateTimeZone(Offset.FromHours(5)); var gapZone = new PrecalculatedDateTimeZone("Test", new[] { FirstInterval, SecondInterval, ThirdInterval }, tailZone); var mapping = gapZone.MapLocal(ThirdInterval.IsoLocalEnd.PlusHours(-1)); Assert.AreEqual(ThirdInterval, mapping.EarlyInterval); Assert.AreEqual(ThirdInterval, mapping.LateInterval); Assert.AreEqual(1, mapping.Count); }
public void MapLocal_GapAroundTailZoneTransition() { // Tail zone is fixed at +5. A local time at the transition // from the precalculated zone (which is -5) will therefore give an instant from // the tail zone which occurs before the precalculated-to-tail transition, // and can therefore be ignored, resulting in an overall gap. var tailZone = new FixedDateTimeZone(Offset.FromHours(5)); var gapZone = new PrecalculatedDateTimeZone("Test", new[] { FirstInterval, SecondInterval, ThirdInterval }, tailZone); var mapping = gapZone.MapLocal(ThirdInterval.IsoLocalEnd); Assert.AreEqual(ThirdInterval, mapping.EarlyInterval); Assert.AreEqual(new ZoneInterval("UTC+05", ThirdInterval.End, Instant.AfterMaxValue, Offset.FromHours(5), Offset.Zero), mapping.LateInterval); Assert.AreEqual(0, mapping.Count); }
/// <summary> /// Creates the <see cref="DateTimeZone"/> for the given canonical ID, which will definitely /// be one of the values of the TzdbAliases dictionary. /// </summary> /// <param name="id">ID for the returned zone, which may be an alias.</param> /// <param name="canonicalId">Canonical ID for zone data</param> public DateTimeZone CreateZone(string id, string canonicalId) { Preconditions.CheckNotNull(id, nameof(id)); Preconditions.CheckNotNull(canonicalId, nameof(canonicalId)); using (var stream = zoneFields[canonicalId].CreateStream()) { var reader = new DateTimeZoneReader(stream, stringPool); // Skip over the ID before the zone data itself reader.ReadString(); var type = (DateTimeZoneWriter.DateTimeZoneType)reader.ReadByte(); return(type switch { DateTimeZoneWriter.DateTimeZoneType.Fixed => FixedDateTimeZone.Read(reader, id), DateTimeZoneWriter.DateTimeZoneType.Precalculated => CachedDateTimeZone.ForZone(PrecalculatedDateTimeZone.Read(reader, id)), _ => throw new InvalidNodaDataException("Unknown time zone type " + type) });
/// <inheritdoc /> public DateTimeZone CreateZone(string id, string canonicalId) { using (var stream = zoneFields[canonicalId].CreateStream()) { var reader = new DateTimeZoneReader(stream, stringPool); // Skip over the ID before the zone data itself reader.ReadString(); var type = (DateTimeZoneWriter.DateTimeZoneType)reader.ReadByte(); switch (type) { case DateTimeZoneWriter.DateTimeZoneType.Fixed: return(FixedDateTimeZone.Read(reader, id)); case DateTimeZoneWriter.DateTimeZoneType.Precalculated: return(CachedDateTimeZone.ForZone(PrecalculatedDateTimeZone.Read(reader, id))); default: throw new InvalidNodaDataException("Unknown time zone type " + type); } } }
public void For_Id_InvalidFixedOffset() { Assert.IsNull(FixedDateTimeZone.GetFixedZoneOrNull("UTC+5Months")); }