/// <summary>
        /// Creates a provider backed by the given <see cref="IDateTimeZoneSource"/>.
        /// </summary>
        /// <remarks>
        /// Note that the source will never be consulted for requests for the fixed-offset timezones "UTC" and
        /// "UTC+/-Offset" (a standard implementation will be returned instead). This is true even if these IDs are
        /// advertised by the source.
        /// </remarks>
        /// <param name="source">The <see cref="IDateTimeZoneSource"/> for this provider.</param>
        /// <exception cref="InvalidDateTimeZoneSourceException"><paramref name="source"/> violates its contract.</exception>
        public DateTimeZoneCache([NotNull] IDateTimeZoneSource source)
        {
            this.source    = Preconditions.CheckNotNull(source, nameof(source));
            this.VersionId = source.VersionId;
            if (VersionId == null)
            {
                throw new InvalidDateTimeZoneSourceException("Source-returned version ID was null");
            }
            var providerIds = source.GetIds();

            if (providerIds == null)
            {
                throw new InvalidDateTimeZoneSourceException("Source-returned ID sequence was null");
            }
            var idList = new List <string>(providerIds);

            idList.Sort(StringComparer.Ordinal);
            Ids = new ReadOnlyCollection <string>(idList);
            // Populate the dictionary with null values meaning "the ID is valid, we haven't fetched the zone yet".
            foreach (string id in Ids)
            {
                if (id == null)
                {
                    throw new InvalidDateTimeZoneSourceException("Source-returned ID sequence contained a null reference");
                }
                timeZoneMap[id] = null;
            }
        }
 /// <summary>
 /// Creates a provider backed by the given <see cref="IDateTimeZoneSource"/>.
 /// </summary>
 /// <remarks>
 /// Note that the source will never be consulted for requests for the fixed-offset timezones "UTC" and
 /// "UTC+/-Offset" (a standard implementation will be returned instead). This is true even if these IDs are
 /// advertised by the source.
 /// </remarks>
 /// <param name="source">The <see cref="IDateTimeZoneSource"/> for this provider.</param>
 /// <exception cref="InvalidDateTimeZoneSourceException"><paramref name="source"/> violates its contract.</exception>
 public DateTimeZoneCache([NotNull] IDateTimeZoneSource source)
 {
     this.source = Preconditions.CheckNotNull(source, nameof(source));
     this.VersionId = source.VersionId;
     if (VersionId == null)
     {
         throw new InvalidDateTimeZoneSourceException("Source-returned version ID was null");
     }
     var providerIds = source.GetIds();
     if (providerIds == null)
     {
         throw new InvalidDateTimeZoneSourceException("Source-returned ID sequence was null");
     }
     var idList = new List<string>(providerIds);
     idList.Sort(StringComparer.Ordinal);
     Ids = new ReadOnlyCollection<string>(idList);
     // Populate the dictionary with null values meaning "the ID is valid, we haven't fetched the zone yet".
     foreach (string id in Ids)
     {
         if (id == null)
         {
             throw new InvalidDateTimeZoneSourceException("Source-returned ID sequence contained a null reference");
         }
         timeZoneMap[id] = null;
     }
 }
Example #3
0
        private static int Main(string[] args)
        {
            Options            options = new Options();
            ICommandLineParser parser  = new CommandLineParser(new CommandLineParserSettings(Console.Error));

            if (!parser.ParseArguments(args, options))
            {
                return(1);
            }
            if (options.FromYear > options.ToYear)
            {
                Console.WriteLine("Error: 'from' year must be not be later than 'to' year");
                return(1);
            }
            IDateTimeZoneSource source = TzdbDateTimeZoneSource.Default;

            if (options.File != null)
            {
                using (var stream = File.OpenRead(options.File))
                {
                    source = TzdbDateTimeZoneSource.FromStream(stream);
                }
            }
            Console.WriteLine("TZDB version: {0}", source.VersionId);

            var provider = new DateTimeZoneCache(source);

            if (options.Zone != null)
            {
                var zone = provider.GetZoneOrNull(options.Zone);
                if (zone == null)
                {
                    Console.WriteLine("Unknown time zone: {0}", options.Zone);
                    return(1);
                }
                DumpZone(zone, Console.Out, options.FromYear, options.ToYear);
            }
            else
            {
                foreach (var id in provider.Ids)
                {
                    DumpZone(provider[id], Console.Out, options.FromYear, options.ToYear);
                }
            }
            return(0);
        }
Example #4
0
 public NodaTimeConverterService(IDateTimeZoneProvider timeZoneProvider, IDateTimeZoneSource timeZoneSource)
 {
     this.TimeZoneProvider = timeZoneProvider;
     this.TimeZoneSource   = timeZoneSource;
 }