コード例 #1
0
        private static long SeekTimeZoneFile(string hash)
        {
            var min       = 1L;
            var max       = TimezoneFileReader.Count;
            var converged = false;

            while (true)
            {
                var mid     = ((max - min) / 2) + min;
                var midLine = TimezoneFileReader.GetLine(mid);

                for (int i = 0; i < hash.Length; i++)
                {
                    if (midLine[i] == '-')
                    {
                        return(mid);
                    }

                    if (midLine[i] > hash[i])
                    {
                        max = mid == max ? min : mid;
                        break;
                    }
                    if (midLine[i] < hash[i])
                    {
                        min = mid == min ? max : mid;
                        break;
                    }

                    if (i == 4)
                    {
                        return(mid);
                    }

                    if (min == mid)
                    {
                        min = max;
                        break;
                    }
                }

                if (min == max)
                {
                    if (converged)
                    {
                        break;
                    }

                    converged = true;
                }
            }
            return(0);
        }
コード例 #2
0
        private static IEnumerable <int> GetTzDataLineNumbers(string geohash)
        {
            var seeked = SeekTimeZoneFile(geohash);

            if (seeked == 0)
            {
                return(new List <int>());
            }

            long min = seeked, max = seeked;
            var  seekedGeohash = TimezoneFileReader.GetLine(seeked).Substring(0, 5);

            while (true)
            {
                var prevGeohash = TimezoneFileReader.GetLine(min - 1).Substring(0, 5);
                if (seekedGeohash == prevGeohash)
                {
                    min--;
                }
                else
                {
                    break;
                }
            }

            while (true)
            {
                var nextGeohash = TimezoneFileReader.GetLine(max + 1).Substring(0, 5);
                if (seekedGeohash == nextGeohash)
                {
                    max++;
                }
                else
                {
                    break;
                }
            }

            var lineNumbers = new List <int>();

            for (var i = min; i <= max; i++)
            {
                var lineNumber = int.Parse(TimezoneFileReader.GetLine(i).Substring(5));
                lineNumbers.Add(lineNumber);
            }

            return(lineNumbers);
        }