Example #1
0
 internal static IEnumerable <TzdbStreamField> ReadFields(Stream input)
 {
     while (true)
     {
         int fieldId = input.ReadByte();
         if (fieldId == -1)
         {
             yield break;
         }
         TzdbStreamFieldId id = (TzdbStreamFieldId)(byte)fieldId;
         // Read 7-bit-encoded length
         int    length = new DateTimeZoneReader(input, null).ReadCount();
         byte[] data   = new byte[length];
         int    offset = 0;
         while (offset < data.Length)
         {
             int bytesRead = input.Read(data, offset, data.Length - offset);
             if (bytesRead == 0)
             {
                 throw new InvalidNodaDataException("Stream ended after reading " + offset + " bytes out of " + data.Length);
             }
             offset += bytesRead;
         }
         yield return(new TzdbStreamField(id, data));
     }
 }
Example #2
0
 internal IDictionary <string, string> ExtractSingleValue(IList <string> stringPool)
 {
     using (var stream = CreateStream())
     {
         var reader = new DateTimeZoneReader(stream, stringPool);
         return(reader.ReadDictionary());
     }
 }
Example #3
0
 internal void HandleStringPoolField(TzdbStreamField field)
 {
     CheckSingleField(field, stringPool);
     using (var stream = field.CreateStream())
     {
         var reader = new DateTimeZoneReader(stream, null);
         int count  = reader.ReadCount();
         stringPool = new string[count];
         for (int i = 0; i < count; i++)
         {
             stringPool[i] = reader.ReadString();
         }
     }
 }
Example #4
0
 internal void HandleZone1970LocationsField(TzdbStreamField field)
 {
     CheckSingleField(field, zone1970Locations);
     CheckStringPoolPresence(field);
     using (var stream = field.CreateStream())
     {
         var reader = new DateTimeZoneReader(stream, stringPool);
         var count  = reader.ReadCount();
         var array  = new TzdbZone1970Location[count];
         for (int i = 0; i < count; i++)
         {
             array[i] = TzdbZone1970Location.Read(reader);
         }
         zone1970Locations = array;
     }
 }
Example #5
0
 internal void HandleZoneField(TzdbStreamField field)
 {
     CheckStringPoolPresence(field);
     // Just read the ID from the zone - we don't parse the data yet.
     // (We could, but we might as well be lazy.)
     using (var stream = field.CreateStream())
     {
         var    reader = new DateTimeZoneReader(stream, stringPool);
         string id     = reader.ReadString();
         if (zoneFields.ContainsKey(id))
         {
             throw new InvalidNodaDataException("Multiple definitions for zone " + id);
         }
         zoneFields[id] = field;
     }
 }
Example #6
0
 /// <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);
                }
            }
        }