Example #1
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);
        }
Example #2
0
 internal Location(TzdbZoneLocation location)
 {
     CountryCode = location.CountryCode;
     CountryName = location.CountryName;
     Comment     = location.Comment;
     Latitude    = location.Latitude;
     Longitude   = location.Longitude;
 }
Example #3
0
 internal void HandleZoneLocationsField(TzdbStreamField field)
 {
     CheckSingleField(field, zoneLocations);
     CheckStringPoolPresence(field);
     using (var stream = field.CreateStream())
     {
         var reader = new DateTimeZoneReader(stream, stringPool);
         var count  = reader.ReadCount();
         var array  = new TzdbZoneLocation[count];
         for (int i = 0; i < count; i++)
         {
             array[i] = TzdbZoneLocation.Read(reader);
         }
         zoneLocations = array;
     }
 }
Example #4
0
        public void Valid()
        {
            var location = new TzdbZoneLocation(
                60 * 3600 + 15 * 60 + 5,
                100 * 3600 + 30 * 60 + 10,
                "Country name",
                "CO",
                "Etc/MadeUpZone",
                "Comment");

            Assert.AreEqual(60.25 + 5.0 / 3600, location.Latitude, 0.000001);
            Assert.AreEqual(100.5 + 10.0 / 3600, location.Longitude, 0.000001);
            Assert.AreEqual("Country name", location.CountryName);
            Assert.AreEqual("CO", location.CountryCode);
            Assert.AreEqual("Etc/MadeUpZone", location.ZoneId);
            Assert.AreEqual("Comment", location.Comment);
        }
Example #5
0
        /// <summary>
        ///     Find the time in a certain city or country, if city and country are specified both are used, if one is missing only
        ///     the other is used. If none are present it will return default(ZonedDateTime).
        /// </summary>
        /// <param name="city">The city, "New York"</param>
        /// <param name="country">The country, "United States"</param>
        /// <returns>The zoned date time of the current time in that location or default(ZonedDateTime).</returns>
        public static ZonedDateTime TimeIn(out TzdbZoneLocation loc, string city = null, string country = null)
        {
            IClock clock = SystemClock.Instance;

            TzdbZoneLocation location = null;

            if (!string.IsNullOrEmpty(country) && !string.IsNullOrEmpty(city))
            {
                location = TzdbDateTimeZoneSource
                           .Default
                           .ZoneLocations
                           .FirstOrDefault(l => l.CountryName.ToLowerInvariant().StartsWith(country.ToLowerInvariant()) &&
                                           l.ZoneId.Split('/')[1]
                                           .ToLowerInvariant()
                                           .Replace("_", " ")
                                           .StartsWith(city.ToLowerInvariant())
                                           );
            }
            else if (!string.IsNullOrEmpty(city))
            {
                location = TzdbDateTimeZoneSource
                           .Default
                           .ZoneLocations
                           .FirstOrDefault(l => l.ZoneId.Split('/')[1]
                                           .ToLowerInvariant()
                                           .Replace("_", " ")
                                           .StartsWith(city.ToLowerInvariant())
                                           );
            }
            else if (!string.IsNullOrEmpty(country))
            {
                location = TzdbDateTimeZoneSource
                           .Default
                           .ZoneLocations
                           .FirstOrDefault(l => l.CountryName.ToLowerInvariant()
                                           .StartsWith(country.ToLowerInvariant()));
            }

            loc = location;
            if (location != null)
            {
                var zone = DateTimeZoneProviders.Tzdb[location.ZoneId];
                return(clock.GetCurrentInstant().InZone(zone));
            }

            return(default);
Example #6
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));
        }
        private TimeZoneInfo GetZoneInfo(TzdbZoneLocation location)
        {
            var zone = DateTimeZoneProviders.Tzdb[location.ZoneId];

            // Get the start and end of the year in this zone
            var startOfYear = zone.AtStartOfDay(new LocalDate(2017, 1, 1));
            var endOfYear   = zone.AtStrictly(new LocalDate(2018, 1, 1).AtMidnight().PlusNanoseconds(-1));

            // Get all intervals for current year
            var intervals = zone.GetZoneIntervals(startOfYear.ToInstant(), endOfYear.ToInstant()).ToList();

            // Try grab interval with DST. If none present, grab first one we can find
            var interval = intervals.FirstOrDefault(i => i.Savings.Seconds > 0) ?? intervals.FirstOrDefault();

            return(new TimeZoneInfo
            {
                TimeZoneId = location.ZoneId,
                Offset = interval.StandardOffset.ToTimeSpan(),
                DstOffset = interval.WallOffset.ToTimeSpan(),
                CountryCode = location.CountryCode,
                CountryName = location.CountryName
            });
        }