/// <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 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); }
/// <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); } } }