Esempio n. 1
0
        public void HasMoreData_Buffers()
        {
            var stream = new MemoryStream(new byte[] { 1, 2 });
            var reader = new DateTimeZoneReader(stream, null);

            // HasMoreData reads a byte and buffers it
            Assert.AreEqual(0, stream.Position);
            Assert.IsTrue(reader.HasMoreData);
            Assert.AreEqual(1, stream.Position);
            Assert.IsTrue(reader.HasMoreData);
            Assert.AreEqual(1, stream.Position);

            // Consume the buffered byte
            Assert.AreEqual((byte)1, reader.ReadByte());
            Assert.AreEqual(1, stream.Position);

            // HasMoreData reads the next byte
            Assert.IsTrue(reader.HasMoreData);
            Assert.AreEqual(2, stream.Position);
            Assert.IsTrue(reader.HasMoreData);
            Assert.AreEqual(2, stream.Position);

            // Consume the buffered byte
            Assert.AreEqual((byte)2, reader.ReadByte());
            Assert.AreEqual(2, stream.Position);

            // No more data
            Assert.IsFalse(reader.HasMoreData);
        }
Esempio n. 2
0
        public void Serialization()
        {
            var location = new TzdbZoneLocation(
                60 * 3600 + 15 * 60 + 5,
                100 * 3600 + 30 * 60 + 10,
                "Country name",
                "CO",
                "Etc/MadeUpZone",
                "Comment");

            var stream = new MemoryStream();
            var writer = new DateTimeZoneWriter(stream, null);

            location.Write(writer);
            stream.Position = 0;

            var reader    = new DateTimeZoneReader(stream, null);
            var location2 = TzdbZoneLocation.Read(reader);

            Assert.AreEqual(60.25 + 5.0 / 3600, location2.Latitude, 0.000001);
            Assert.AreEqual(100.5 + 10.0 / 3600, location2.Longitude, 0.000001);
            Assert.AreEqual("Country name", location2.CountryName);
            Assert.AreEqual("CO", location2.CountryCode);
            Assert.AreEqual("Etc/MadeUpZone", location2.ZoneId);
            Assert.AreEqual("Comment", location2.Comment);
        }
Esempio n. 3
0
        private static void ReadPrecalculatedTimeZone(DateTimeZoneReader reader)
        {
            int size = reader.ReadCount();

            Console.WriteLine($"  Precalculated intervals: {size}");
            var start = reader.ReadZoneIntervalTransition(null);

            for (int i = 0; i < size; i++)
            {
                var name      = reader.ReadString();
                var offset    = reader.ReadOffset();
                var savings   = reader.ReadOffset();
                var nextStart = reader.ReadZoneIntervalTransition(start);
                Console.WriteLine(Invariant($"  {start:yyyy-MM-dd'T'HH:mm:ss} - {nextStart:yyyy-MM-dd'T'HH:mm:ss}; wall offset: {offset}; savings: {savings}; name: {name}"));
                start = nextStart;
            }
            if (reader.ReadByte() == 1)
            {
                Offset         standardOffset     = reader.ReadOffset();
                string         standardName       = reader.ReadString();
                ZoneYearOffset standardYearOffset = ZoneYearOffset.Read(reader);
                string         daylightName       = reader.ReadString();
                ZoneYearOffset daylightYearOffset = ZoneYearOffset.Read(reader);
                Offset         savings            = reader.ReadOffset();
                Console.WriteLine("  Tail zone:");
                Console.WriteLine($"    Standard time: {standardYearOffset}; offset: {standardOffset}; name: {standardName}");
                Console.WriteLine($"    Daylight time: {daylightYearOffset}; offset: {standardOffset + savings}; name: {daylightName}");
            }
            else
            {
                Console.WriteLine("  No tail zone");
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DtzIoHelper" /> class.
 /// </summary>
 private DtzIoHelper(IList <string> stringPool)
 {
     ioStream        = new IoStream();
     Reader          = new DateTimeZoneReader(ioStream.GetReadStream(), stringPool);
     Writer          = new DateTimeZoneWriter(ioStream.GetWriteStream(), stringPool);
     this.stringPool = stringPool;
 }
Esempio n. 5
0
        private static void ReadZone1970Locations(DateTimeZoneReader reader)
        {
            var count = reader.ReadCount();

            Console.WriteLine($"  Entries: {count}");
            for (int i = 0; i < count; i++)
            {
                int           latitudeSeconds  = reader.ReadSignedCount();
                int           longitudeSeconds = reader.ReadSignedCount();
                int           countryCount     = reader.ReadCount();
                StringBuilder countries        = new StringBuilder();
                for (int j = 0; j < countryCount; j++)
                {
                    countries.Append($"{reader.ReadString()} ({reader.ReadString()}), ");
                }
                if (countries.Length > 0)
                {
                    // Remove the trailing comma+space.
                    countries.Length -= 2;
                }
                string zoneId  = reader.ReadString();
                string comment = reader.ReadString();
                Console.WriteLine($"  Lat seconds: {latitudeSeconds}; long seconds: {longitudeSeconds}: countries: [{countries}]; id: {zoneId}; comment: {comment}");
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DtzIoHelper" /> class.
 /// </summary>
 private DtzIoHelper(IList<string> stringPool)
 {
     ioStream = new IoStream();
     Reader = new DateTimeZoneReader(ioStream.GetReadStream(), stringPool);
     Writer = new DateTimeZoneWriter(ioStream.GetWriteStream(), stringPool);
     this.stringPool = stringPool;
 }
Esempio n. 7
0
        public void ReadZoneIntervalTransition_InvalidMarkerValue()
        {
            byte[] data   = new byte[] { 4, 0, 0, 0, 0, 0 }; // Marker value of 4 (followed by 0s to check that it's not just out of data)
            var    stream = new MemoryStream(data);
            var    reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => reader.ReadZoneIntervalTransition(previous: NodaConstants.UnixEpoch));
        }
Esempio n. 8
0
 private static void ReadFixedTimeZone(DateTimeZoneReader reader)
 {
     Console.WriteLine($"  Offset: {reader.ReadOffset()}");
     if (reader.HasMoreData)
     {
         Console.WriteLine($"  Name: {reader.ReadString()}");
     }
 }
Esempio n. 9
0
        public void ReadString_NotEnoughData()
        {
            // We say there are 5 bytes, but there are only 4 left...
            byte[] data   = new byte[] { 0x05, 0x40, 0x40, 0x40, 0x40 };
            var    stream = new MemoryStream(data);
            var    reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => reader.ReadString());
        }
Esempio n. 10
0
        public void ReadMilliseconds_InvalidFlag()
        {
            // Top 3 bits are the flag. Valid flag values are 0b100 (minutes), 0b101 (seconds)  and 0b110 (milliseconds)
            byte[] data   = new byte[] { 0xe0, 0, 0, 0, 0, 0 }; // Invalid flag (followed by 0s to check that it's not just out of data)
            var    stream = new MemoryStream(data);
            var    reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => reader.ReadMilliseconds());
        }
Esempio n. 11
0
        public void ReadByte_NotEnoughData()
        {
            byte[] data   = new byte[] { 0x05 };
            var    stream = new MemoryStream(data);
            var    reader = new DateTimeZoneReader(stream, null);

            // Just check we can read the first byte, then fail on the second
            Assert.AreEqual((byte)5, reader.ReadByte());
            Assert.Throws <InvalidNodaDataException>(() => reader.ReadByte());
        }
Esempio n. 12
0
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: NodaTime.NzdPrinter <path/url to nzd file>");
                return(1);
            }
            var stream  = new MemoryStream(FileUtility.LoadFileOrUrl(args[0]));
            int version = new BinaryReader(stream).ReadInt32();

            Console.WriteLine($"File format version: {version}");
            string[] stringPool = null; // Will be populated before it's used...
            foreach (var field in TzdbStreamField.ReadFields(stream))
            {
                Console.WriteLine($"Field: {field.Id}");
                var reader = new DateTimeZoneReader(field.CreateStream(), stringPool);
                switch (field.Id)
                {
                case StringPool:
                    stringPool = ReadStringPool(reader);
                    break;

                case TzdbStreamFieldId.TimeZone:
                    ReadTimeZone(reader);
                    break;

                case TzdbVersion:
                    Console.WriteLine($"TZDB version: {reader.ReadString()}");
                    break;

                case TzdbIdMap:
                    ReadMap(reader);
                    break;

                case CldrSupplementalWindowsZones:
                    ReadWindowsZones(reader);
                    break;

                case WindowsAdditionalStandardNameToIdMapping:
                    ReadMap(reader);
                    break;

                case ZoneLocations:
                    ReadZoneLocations(reader);
                    break;

                case Zone1970Locations:
                    ReadZone1970Locations(reader);
                    break;
                }
                Console.WriteLine();
            }

            return(0);
        }
Esempio n. 13
0
        private static void ReadMap(DateTimeZoneReader reader)
        {
            int count = reader.ReadCount();

            Console.WriteLine($"  Entries: {count}");
            for (int i = 0; i < count; i++)
            {
                string key   = reader.ReadString();
                string value = reader.ReadString();
                Console.WriteLine($"    {key} -> {value}");
            }
        }
Esempio n. 14
0
        private static void ReadWindowsZones(DateTimeZoneReader reader)
        {
            var zones = WindowsZones.Read(reader);

            Console.WriteLine($"  Version: {zones.Version}");
            Console.WriteLine($"  TZDB version: {zones.TzdbVersion}");
            Console.WriteLine($"  Windows version: {zones.WindowsVersion}");
            Console.WriteLine($"  Mappings: {zones.MapZones.Count}");
            foreach (var mapZone in zones.MapZones)
            {
                Console.WriteLine($"  {mapZone}");
            }
        }
Esempio n. 15
0
        private static string[] ReadStringPool(DateTimeZoneReader reader)
        {
            int count = reader.ReadCount();

            Console.WriteLine($"  String pool contains {count} entries");
            var stringPool = new string[count];

            for (int i = 0; i < count; i++)
            {
                stringPool[i] = reader.ReadString();
            }
            return(stringPool);
        }
Esempio n. 16
0
        public void ReadWrite()
        {
            var zone   = new MapZone("windowsId", "territory", new[] { "id1", "id2", "id3" });
            var stream = new MemoryStream();
            var writer = new DateTimeZoneWriter(stream, null);

            zone.Write(writer);
            stream.Position = 0;

            var reader = new DateTimeZoneReader(stream, null);
            var zone2  = MapZone.Read(reader);

            Assert.AreEqual(zone, zone2);
        }
Esempio n. 17
0
        public void ReadZoneIntervalTransition_NoPreviousValue()
        {
            // Count value between 1 << 7 and 1 << 21 (followed by 0s to check that it's not just out of data)
            byte[] data   = new byte[] { 0xff, 0x7f, 0, 0, 0, 0 };
            var    stream = new MemoryStream(data);
            var    reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => reader.ReadZoneIntervalTransition(previous: null));

            // Validate that when we *do* provide a previous value, it doesn't throw
            stream.Position = 0;
            reader          = new DateTimeZoneReader(stream, null);
            reader.ReadZoneIntervalTransition(previous: NodaConstants.UnixEpoch);
        }
        public void ReadWrite()
        {
            var map1   = new StandardDaylightAlternatingMap(Offset.FromHours(1), Summer, Winter);
            var stream = new MemoryStream();
            var writer = new DateTimeZoneWriter(stream, null);

            map1.Write(writer);
            stream.Position = 0;

            var reader = new DateTimeZoneReader(stream, null);
            var map2   = StandardDaylightAlternatingMap.Read(reader);

            Assert.AreEqual(map1, map2);
        }
Esempio n. 19
0
        public void ReadCount_OutOfRange()
        {
            // Int32.MaxValue + 1 (as a uint) is 10000000_00000000_00000000_00000000
            // So only bit 31 is set.
            // Divided into 7 bit chunks (reverse order), with top bit set for continuation, this is:
            // 10000000 - bits 0-6
            // 10000000 - bits 7-13
            // 10000000 - bits 14-20
            // 10000000 - bits 21-27
            // 00001000 - bits 28-34
            byte[] data   = new byte[] { 0x80, 0x80, 0x80, 0x80, 0b0000_1000 };
            var    stream = new MemoryStream(data);
            var    reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => reader.ReadCount());
        }
Esempio n. 20
0
        private static void ReadZoneLocations(DateTimeZoneReader reader)
        {
            var count = reader.ReadCount();

            Console.WriteLine($"  Entries: {count}");
            for (int i = 0; i < count; i++)
            {
                int    latitudeSeconds  = reader.ReadSignedCount();
                int    longitudeSeconds = reader.ReadSignedCount();
                string countryName      = reader.ReadString();
                string countryCode      = reader.ReadString();
                string zoneId           = reader.ReadString();
                string comment          = reader.ReadString();
                Console.WriteLine($"  Lat seconds: {latitudeSeconds}; long seconds: {longitudeSeconds}: country: {countryName} ({countryCode}); id: {zoneId}; comment: {comment}");
            }
        }
Esempio n. 21
0
        public void ReadInvalid()
        {
            var stream = new MemoryStream();
            var writer = new DateTimeZoneWriter(stream, null);

            // This is invalid
            writer.WriteSignedCount(90 * 3600 + 1);
            writer.WriteSignedCount(0);
            writer.WriteString("name");
            writer.WriteString("co");
            writer.WriteString("Europe/Somewhere");
            writer.WriteString("");
            stream.Position = 0;
            var reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => TzdbZoneLocation.Read(reader));
        }
Esempio n. 22
0
        public void ReadInvalid()
        {
            var stream = new MemoryStream();
            var writer = new DateTimeZoneWriter(stream, null);

            // Valid latitude/longitude
            writer.WriteSignedCount(0);
            writer.WriteSignedCount(0);
            // But no countries
            writer.WriteCount(0);
            writer.WriteString("Europe/Somewhere");
            writer.WriteString("");
            stream.Position = 0;
            var reader = new DateTimeZoneReader(stream, null);

            Assert.Throws <InvalidNodaDataException>(() => TzdbZone1970Location.Read(reader));
        }
Esempio n. 23
0
        public void ReadWrite()
        {
            var zones = new WindowsZones("version", "tzdbVersion", "windowsVersion", new[] { MapZone1, MapZone2, MapZone3 });

            var stream = new MemoryStream();
            var writer = new DateTimeZoneWriter(stream, null);

            zones.Write(writer);
            stream.Position = 0;

            var reader = new DateTimeZoneReader(stream, null);
            var zones2 = WindowsZones.Read(reader);

            Assert.AreEqual("version", zones2.Version);
            Assert.AreEqual("tzdbVersion", zones2.TzdbVersion);
            Assert.AreEqual("windowsVersion", zones2.WindowsVersion);
            Assert.AreEqual("primaryId2", zones2.PrimaryMapping["windowsId2"]);
            Assert.AreEqual("primaryId3", zones2.PrimaryMapping["windowsId3"]);
            Assert.AreEqual(new[] { MapZone1, MapZone2, MapZone3 }, zones2.MapZones);
        }
Esempio n. 24
0
        private static void ReadTimeZone(DateTimeZoneReader reader)
        {
            Console.WriteLine($"  ID: {reader.ReadString()}");
            var type = (DateTimeZoneWriter.DateTimeZoneType)reader.ReadByte();

            Console.WriteLine($"  Type: {type}");
            switch (type)
            {
            case DateTimeZoneWriter.DateTimeZoneType.Fixed:
                ReadFixedTimeZone(reader);
                break;

            case DateTimeZoneWriter.DateTimeZoneType.Precalculated:
                ReadPrecalculatedTimeZone(reader);
                break;

            default:
                Console.WriteLine("  (Unknown time zone type)");
                break;
            }
        }