Ejemplo n.º 1
0
        ///// <summary>
        ///// Gets/sets/resets legacy configuration setting "date.timezone".
        ///// </summary>
        //internal static object GsrTimeZone(LibraryConfiguration/*!*/ local, LibraryConfiguration/*!*/ @default, object value, IniAction action)
        //{
        //    string result = (local.Date.TimeZone != null) ? local.Date.TimeZone.StandardName : null;

        //    switch (action)
        //    {
        //        case IniAction.Set:
        //            {
        //                string name = Core.Convert.ObjectToString(value);
        //                TimeZoneInfo zone = GetTimeZone(name);

        //                if (zone == null)
        //                {
        //                    PhpException.Throw(PhpError.Warning, LibResources.GetString("unknown_timezone", name));
        //                }
        //                else
        //                {
        //                    local.Date.TimeZone = zone;
        //                }
        //                break;
        //            }

        //        case IniAction.Restore:
        //            local.Date.TimeZone = @default.Date.TimeZone;
        //            break;
        //    }
        //    return result;
        //}

        /// <summary>
        /// Gets an instance of <see cref="TimeZone"/> corresponding to specified PHP name for time zone.
        /// </summary>
        /// <param name="phpName">PHP time zone name.</param>
        /// <returns>The time zone or a <B>null</B> reference.</returns>
        internal static TimeZoneInfo GetTimeZone(string /*!*/ phpName)
        {
            if (string.IsNullOrEmpty(phpName))
            {
                return(null);
            }

            // simple binary search (not the Array.BinarySearch)
            var timezones = PhpTimeZone.s_lazyTimeZones.Value;
            int a = 0, b = timezones.Length - 1;

            while (a <= b)
            {
                int x          = (a + b) >> 1;
                int comparison = StringComparer.OrdinalIgnoreCase.Compare(timezones[x].PhpName, phpName);
                if (comparison == 0)
                {
                    return(timezones[x].Info);
                }

                if (comparison < 0)
                {
                    a = x + 1;
                }
                else //if (comparison > 0)
                {
                    b = x - 1;
                }
            }

            // try custom offset or a known abbreviation:
            var dt = new DateInfo();
            var _  = 0;

            if (dt.SetTimeZone(phpName, ref _))
            {
                // +00:00
                // -00:00
                // abbr
                return(dt.ResolveTimeZone());
            }

            //
            return(null);
        }