Exemple #1
0
        static void Main(string[] args)
        {
            if (args.Length < 1 || args.Length > 2)
            {
                Console.WriteLine("Usage: NodaDump <source> [zone]");
                Console.WriteLine("The source may be a local nzd file, or a URL, e.g. http://nodatime.org/tzdb/tzdb2015e.nzd");
                return;
            }
            var sourceData = LoadSource(args[0]);
            var source = TzdbDateTimeZoneSource.FromStream(new MemoryStream(sourceData));
            var provider = new DateTimeZoneCache(source);

            if (args.Length == 2)
            {
                DumpZone(provider[args[1]]);
            }
            else
            {
                // Ids is already sorted
                foreach (var id in provider.Ids)
                {
                    DumpZone(provider[id]);
                    Console.Write("\r\n");
                }
            }
        }
        private static async Task<CurrentTzdbProvider> DownloadAsync()
        {
            using (var client = new HttpClient())
            {
                var latest = new Uri((await client.GetStringAsync("http://nodatime.org/tzdb/latest.txt")).TrimEnd());
                var fileName = latest.Segments.Last();
                var path = Path.Combine(Path.GetTempPath(), fileName);

                if (!File.Exists(path))
                {
                    using (var httpStream = await client.GetStreamAsync(latest))
                    using (var fileStream = File.Create(path))
                    {
                        await httpStream.CopyToAsync(fileStream);
                    }
                }

                using (var fileStream = File.OpenRead(path))
                {
                    var source = TzdbDateTimeZoneSource.FromStream(fileStream);
                    var provider = new DateTimeZoneCache(source);
                    return new CurrentTzdbProvider(provider, source.Aliases);
                }
            }
        }
 public void SourceIsNotAskedForUtcIfAdvertised()
 {
     var source = new TestDateTimeZoneSource("Test1", "Test2", "UTC");
     var provider = new DateTimeZoneCache(source);
     var zone = provider[DateTimeZone.UtcId];
     Assert.IsNotNull(zone);
     Assert.IsNull(source.LastRequestedId);
 }
Exemple #4
0
 private static void CreateTextDump(TzdbDateTimeZoneSource source, string file)
 {
     var provider = new DateTimeZoneCache(source);
     using (var writer = File.CreateText(file))
     {
         provider.Dump(writer);
     }
 }
        public void CachingForPresentValues()
        {
            var source = new TestDateTimeZoneSource("Test1", "Test2");
            var provider = new DateTimeZoneCache(source);
            var zone1a = provider["Test1"];
            Assert.IsNotNull(zone1a);
            Assert.AreEqual("Test1", source.LastRequestedId);

            // Hit up the cache (and thus the source) for Test2
            Assert.IsNotNull(provider["Test2"]);
            Assert.AreEqual("Test2", source.LastRequestedId);

            // Ask for Test1 again
            var zone1b = provider["Test1"];
            // We won't have consulted the source again
            Assert.AreEqual("Test2", source.LastRequestedId);

            Assert.AreSame(zone1a, zone1b);
        }
        public static IDateTimeZoneProvider LoadTzdb(string path = null)
        {
            IDateTimeZoneProvider provider;

            // Load the time zone database from a file, if specified.
            path = path ?? UtilityConfiguration.Active.TimeZoneDB;
            if (!string.IsNullOrWhiteSpace(path))
            {
                Uri uri = new Uri(path.TrimStart('\\', '/'), UriKind.RelativeOrAbsolute);
                if (!uri.IsAbsoluteUri)
                    uri = new Uri(UtilityExtensions.AppDomainBaseUri, uri);

                // If the URI is a file, load it from the file system, otherwise download it
                if (uri.IsFile)
                {
                    path = uri.LocalPath;
                    if (!File.Exists(path))
                        throw new FileNotFoundException(
                            // ReSharper disable once AssignNullToNotNullAttribute
                            string.Format(Resources.TimeHelper_TimeHelper_TimeZoneDB_Not_Found, path));

                    try
                    {
                        using (FileStream stream = File.OpenRead(path))
                            // ReSharper disable once AssignNullToNotNullAttribute
                            provider = new DateTimeZoneCache(TzdbDateTimeZoneSource.FromStream(stream));
                    }
                    catch (Exception e)
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        throw new FileLoadException(
                            string.Format(Resources.TimeHelper_TimeHelper_TimeZoneDB_Failed, path),
                            e);
                    }
                }
                else
                {
                    path = uri.AbsoluteUri;
                    try
                    {
                        // ReSharper disable AssignNullToNotNullAttribute
                        WebRequest request = WebRequest.Create(uri);
                        using (WebResponse response = request.GetResponse())
                        using (Stream stream = response.GetResponseStream())
                            provider = new DateTimeZoneCache(TzdbDateTimeZoneSource.FromStream(stream));
                        // ReSharper restore AssignNullToNotNullAttribute
                    }
                    catch (Exception e)
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        throw new FileLoadException(
                            string.Format(Resources.TimeHelper_TimeHelper_TimeZoneDB_Failed, path),
                            e);
                    }
                }
            }
            // ReSharper disable once AssignNullToNotNullAttribute
            else
            // ReSharper disable once AssignNullToNotNullAttribute
                provider = DateTimeZoneProviders.Tzdb;

            Debug.Assert(provider != null);

            return provider;
        }
 public void UtcIsReturnedInIdsIfAdvertisedByProvider()
 {
     var source = new TestDateTimeZoneSource("Test1", "Test2", "UTC");
     var provider = new DateTimeZoneCache(source);
     Assert.True(provider.Ids.Contains(DateTimeZone.UtcId));
 }
 public void SourceIsNotAskedForUnknownIds()
 {
     var source = new TestDateTimeZoneSource("Test1", "Test2");
     var provider = new DateTimeZoneCache(source);
     Assert.Throws<DateTimeZoneNotFoundException>(() => { var ignored = provider["Unknown"]; });
     Assert.IsNull(source.LastRequestedId);
 }
 public void VersionIdPassThrough()
 {
     var provider = new DateTimeZoneCache(new TestDateTimeZoneSource("Test1", "Test2") { VersionId = "foo" });
     Assert.AreEqual("foo", provider.VersionId);
 }
 public void EmptyIdAccepted()
 {
     var provider = new DateTimeZoneCache(new TestDateTimeZoneSource("Test1", "Test2"));
     Assert.Throws<DateTimeZoneNotFoundException>(() => { var ignored = provider[""]; });
 }
 public void NullIdRejected()
 {
     var provider = new DateTimeZoneCache(new TestDateTimeZoneSource("Test1", "Test2"));
     // GetType call just to avoid trying to use a property as a statement...
     Assert.Throws<ArgumentNullException>(() => provider[null].GetType());
 }
 public void FixedOffsetZeroReturnsUtc()
 {
     string id = "UTC+00:00";
     var source = new TestDateTimeZoneSource("Test1", "Test2", id);
     var provider = new DateTimeZoneCache(source);
     DateTimeZone zone = provider[id];
     Assert.AreEqual(DateTimeZone.Utc, zone);
     Assert.IsNull(source.LastRequestedId);
 }
 public void FixedOffsetUncached()
 {
     string id = "UTC+05:26";
     var source = new TestDateTimeZoneSource("Test1", "Test2");
     var provider = new DateTimeZoneCache(source);
     DateTimeZone zone1 = provider[id];
     DateTimeZone zone2 = provider[id];
     Assert.AreNotSame(zone1, zone2);
     Assert.AreEqual(zone1, zone2);
 }
 public void FixedOffsetSucceedsWithoutConsultingSourceWhenAdvertised()
 {
     string id = "UTC+05:30";
     var source = new TestDateTimeZoneSource("Test1", "Test2", id);
     var provider = new DateTimeZoneCache(source);
     DateTimeZone zone = provider[id];
     Assert.AreEqual(DateTimeZone.ForOffset(Offset.FromHoursAndMinutes(5, 30)), zone);
     Assert.AreEqual(id, zone.Id);
     Assert.IsNull(source.LastRequestedId);
 }