Пример #1
0
        /// <summary>
        /// Gets the array of <c>TimeZoneRule</c> which represents the rule of
        /// this time zone object since the specified start time. The first element
        /// in the result array will be the <c>InitialTimeZoneRule</c> instance
        /// for the initial rule. The rest will be either
        /// <c>AnnualTimeZoneRule</c> or <c>TimeArrayTimeZoneRule</c>
        /// instances representing transitions.
        /// </summary>
        ///
        /// <param name="start">The start time (inclusive).</param>
        /// <returns>The array of <c>TimeZoneRule</c> which represents this time
        /// zone since the start time.</returns>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public virtual TimeZoneRule[] GetTimeZoneRules(long start)
        {
            TimeZoneRule[]     all = GetTimeZoneRules();
            TimeZoneTransition tzt = GetPreviousTransition(start, true);

            if (tzt == null)
            {
                // No need to filter out rules only applicable to time before the
                // start
                return(all);
            }

            BitSet isProcessed   = new BitSet(all.Length);
            IList  filteredRules = new LinkedList();

            // Create initial rule
            TimeZoneRule initial = new InitialTimeZoneRule(tzt.GetTo().GetName(),
                                                           tzt.GetTo().GetRawOffset(), tzt.GetTo().GetDSTSavings());

            ILOG.J2CsMapping.Collections.Generics.Collections.Add(filteredRules, initial);
            isProcessed.Set(0);

            // Mark rules which does not need to be processed
            for (int i = 1; i < all.Length; i++)
            {
                DateTime d = all[i].GetNextStart(start, initial.GetRawOffset(),
                                                 initial.GetDSTSavings(), false);
                if (d == null)
                {
                    isProcessed.Set(i);
                }
            }

            long time = start;
            bool bFinalStd = false, bFinalDst = false;

            while (!bFinalStd || !bFinalDst)
            {
                tzt = GetNextTransition(time, false);
                if (tzt == null)
                {
                    break;
                }
                time = tzt.GetTime();

                TimeZoneRule toRule  = tzt.GetTo();
                int          ruleIdx = 1;
                for (; ruleIdx < all.Length; ruleIdx++)
                {
                    if (all[ruleIdx].Equals(toRule))
                    {
                        break;
                    }
                }
                if (ruleIdx >= all.Length)
                {
                    throw new InvalidOperationException("The rule was not found");
                }
                if (isProcessed.Get(ruleIdx))
                {
                    continue;
                }
                if (toRule  is  TimeArrayTimeZoneRule)
                {
                    TimeArrayTimeZoneRule tar = (TimeArrayTimeZoneRule)toRule;

                    // Get the previous raw offset and DST savings before the very
                    // first start time
                    long t = start;
                    while (true)
                    {
                        tzt = GetNextTransition(t, false);
                        if (tzt == null)
                        {
                            break;
                        }
                        if (tzt.GetTo().Equals(tar))
                        {
                            break;
                        }
                        t = tzt.GetTime();
                    }
                    if (tzt != null)
                    {
                        // Check if the entire start times to be added
                        DateTime firstStart = tar.GetFirstStart(tzt.GetFrom()
                                                                .GetRawOffset(), tzt.GetFrom().GetDSTSavings());
                        if ((firstStart.Ticks / 10000) > start)
                        {
                            // Just add the rule as is
                            ILOG.J2CsMapping.Collections.Generics.Collections.Add(filteredRules, tar);
                        }
                        else
                        {
                            // Collect transitions after the start time
                            long[] times    = tar.GetStartTimes();
                            int    timeType = tar.GetTimeType();
                            int    idx;
                            for (idx = 0; idx < times.Length; idx++)
                            {
                                t = times[idx];
                                if (timeType == IBM.ICU.Util.DateTimeRule.STANDARD_TIME)
                                {
                                    t -= tzt.GetFrom().GetRawOffset();
                                }
                                if (timeType == IBM.ICU.Util.DateTimeRule.WALL_TIME)
                                {
                                    t -= tzt.GetFrom().GetDSTSavings();
                                }
                                if (t > start)
                                {
                                    break;
                                }
                            }
                            int asize = times.Length - idx;
                            if (asize > 0)
                            {
                                long[] newtimes = new long[asize];
                                System.Array.Copy((Array)(times), idx, (Array)(newtimes), 0, asize);
                                TimeArrayTimeZoneRule newtar = new TimeArrayTimeZoneRule(
                                    tar.GetName(), tar.GetRawOffset(),
                                    tar.GetDSTSavings(), newtimes,
                                    tar.GetTimeType());
                                ILOG.J2CsMapping.Collections.Generics.Collections.Add(filteredRules, newtar);
                            }
                        }
                    }
                }
                else if (toRule  is  AnnualTimeZoneRule)
                {
                    AnnualTimeZoneRule ar           = (AnnualTimeZoneRule)toRule;
                    DateTime           firstStart_0 = ar.GetFirstStart(
                        tzt.GetFrom().GetRawOffset(), tzt.GetFrom()
                        .GetDSTSavings());
                    if ((firstStart_0.Ticks / 10000) == tzt.GetTime())
                    {
                        // Just add the rule as is
                        ILOG.J2CsMapping.Collections.Generics.Collections.Add(filteredRules, ar);
                    }
                    else
                    {
                        // Calculate the transition year
                        int[] dfields = new int[6];
                        IBM.ICU.Impl.Grego.TimeToFields(tzt.GetTime(), dfields);
                        // Recreate the rule
                        AnnualTimeZoneRule newar = new AnnualTimeZoneRule(
                            ar.GetName(), ar.GetRawOffset(),
                            ar.GetDSTSavings(), ar.GetRule(), dfields[0],
                            ar.GetEndYear());
                        ILOG.J2CsMapping.Collections.Generics.Collections.Add(filteredRules, newar);
                    }
                    // Check if this is a final rule
                    if (ar.GetEndYear() == IBM.ICU.Util.AnnualTimeZoneRule.MAX_YEAR)
                    {
                        // After both final standard and dst rule are processed,
                        // exit this while loop.
                        if (ar.GetDSTSavings() == 0)
                        {
                            bFinalStd = true;
                        }
                        else
                        {
                            bFinalDst = true;
                        }
                    }
                }
                isProcessed.Set(ruleIdx);
            }
            TimeZoneRule[] rules = new TimeZoneRule[filteredRules.Count];
            ILOG.J2CsMapping.Collections.Collections.ToArray(filteredRules, rules);
            return(rules);
        }
Пример #2
0
        /// <summary>
        /// Gets the array of <c>TimeZoneRule</c> which represents the rule of
        /// this time zone object near the specified date. Some applications are not
        /// capable to handle historic time zone rule changes. Also some applications
        /// can only handle certain type of rule definitions. This method returns
        /// either a single <c>InitialTimeZoneRule</c> if this time zone does
        /// not have any daylight saving time within 1 year from the specified time,
        /// or a pair of <c>AnnualTimeZoneRule</c> whose rule type is
        /// <c>DateTimeRule.DOW</c> for date and
        /// <c>DateTimeRule.WALL_TIME</c> for time with a single
        /// <c>InitialTimeZoneRule</c> representing the initial time, when this
        /// time zone observes daylight saving time near the specified date. Thus,
        /// the result may be only valid for dates around the specified date.
        /// </summary>
        ///
        /// <param name="date">The date to be used for <c>TimeZoneRule</c> extraction.</param>
        /// <returns>The array of <c>TimeZoneRule</c>, either a single
        /// <c>InitialTimeZoneRule</c> object, or a pair of
        /// <c>AnnualTimeZoneRule</c> with a single
        /// <c>InitialTimeZoneRule</c>. The first element in the array
        /// is always a <c>InitialTimeZoneRule</c>.</returns>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public TimeZoneRule[] GetSimpleTimeZoneRulesNear(long date)
        {
            AnnualTimeZoneRule[] annualRules = null;
            TimeZoneRule         initialRule = null;
            // Get the next transition
            TimeZoneTransition tr = GetNextTransition(date, false);

            if (tr != null)
            {
                String initialName = tr.GetFrom().GetName();
                int    initialRaw  = tr.GetFrom().GetRawOffset();
                int    initialDst  = tr.GetFrom().GetDSTSavings();

                // Check if the next transition is either DST->STD or STD->DST and
                // within roughly 1 year from the specified date
                long nextTransitionTime = tr.GetTime();
                if (((tr.GetFrom().GetDSTSavings() == 0 && tr.GetTo()
                      .GetDSTSavings() != 0) || (tr.GetFrom().GetDSTSavings() != 0 && tr
                                                 .GetTo().GetDSTSavings() == 0)) &&
                    date + MILLIS_PER_YEAR > nextTransitionTime)
                {
                    // Get the next next transition
                    annualRules = new AnnualTimeZoneRule[2];
                    int[] dtfields = IBM.ICU.Impl.Grego.TimeToFields(
                        nextTransitionTime + tr.GetFrom().GetRawOffset()
                        + tr.GetFrom().GetDSTSavings(), null);
                    int weekInMonth = IBM.ICU.Impl.Grego.GetDayOfWeekInMonth(dtfields[0],
                                                                             dtfields[1], dtfields[2]);
                    // Create DOW rule
                    DateTimeRule dtr = new DateTimeRule(dtfields[1], weekInMonth,
                                                        dtfields[3], dtfields[5], IBM.ICU.Util.DateTimeRule.WALL_TIME);
                    annualRules[0] = new AnnualTimeZoneRule(tr.GetTo().GetName(),
                                                            tr.GetTo().GetRawOffset(), tr.GetTo().GetDSTSavings(),
                                                            dtr, dtfields[0], IBM.ICU.Util.AnnualTimeZoneRule.MAX_YEAR);

                    tr = GetNextTransition(nextTransitionTime, false);
                    AnnualTimeZoneRule secondRule = null;
                    if (tr != null)
                    {
                        // Check if the next next transition is either DST->STD or
                        // STD->DST
                        // and within roughly 1 year from the next transition
                        if (((tr.GetFrom().GetDSTSavings() == 0 && tr.GetTo()
                              .GetDSTSavings() != 0) || (tr.GetFrom()
                                                         .GetDSTSavings() != 0 && tr.GetTo().GetDSTSavings() == 0)) &&
                            nextTransitionTime + MILLIS_PER_YEAR > tr
                            .GetTime())
                        {
                            // Generate another DOW rule
                            dtfields = IBM.ICU.Impl.Grego.TimeToFields(tr.GetTime()
                                                                       + tr.GetFrom().GetRawOffset()
                                                                       + tr.GetFrom().GetDSTSavings(), dtfields);
                            weekInMonth = IBM.ICU.Impl.Grego.GetDayOfWeekInMonth(dtfields[0],
                                                                                 dtfields[1], dtfields[2]);
                            dtr = new DateTimeRule(dtfields[1], weekInMonth,
                                                   dtfields[3], dtfields[5],
                                                   IBM.ICU.Util.DateTimeRule.WALL_TIME);
                            secondRule = new AnnualTimeZoneRule(tr.GetTo()
                                                                .GetName(), tr.GetTo().GetRawOffset(), tr
                                                                .GetTo().GetDSTSavings(), dtr, dtfields[0] - 1,
                                                                IBM.ICU.Util.AnnualTimeZoneRule.MAX_YEAR);
                            // Make sure this rule can be applied to the specified
                            // date
                            DateTime d = secondRule.GetPreviousStart(date, tr.GetFrom()
                                                                     .GetRawOffset(), tr.GetFrom().GetDSTSavings(),
                                                                     true);
                            if (d != null && (d.Ticks / 10000) <= date &&
                                initialRaw == tr.GetTo().GetRawOffset() &&
                                initialDst == tr.GetTo().GetDSTSavings())
                            {
                                // We can use this rule as the second transition
                                // rule
                                annualRules[1] = secondRule;
                            }
                        }
                    }
                    if (annualRules[1] == null)
                    {
                        // Try previous transition
                        tr = GetPreviousTransition(date, true);
                        if (tr != null)
                        {
                            // Check if the previous transition is either DST->STD
                            // or STD->DST.
                            // The actual transition time does not matter here.
                            if ((tr.GetFrom().GetDSTSavings() == 0 && tr.GetTo()
                                 .GetDSTSavings() != 0) ||
                                (tr.GetFrom().GetDSTSavings() != 0 && tr
                                 .GetTo().GetDSTSavings() == 0))
                            {
                                // Generate another DOW rule
                                dtfields = IBM.ICU.Impl.Grego.TimeToFields(tr.GetTime()
                                                                           + tr.GetFrom().GetRawOffset()
                                                                           + tr.GetFrom().GetDSTSavings(), dtfields);
                                weekInMonth = IBM.ICU.Impl.Grego.GetDayOfWeekInMonth(
                                    dtfields[0], dtfields[1], dtfields[2]);
                                dtr = new DateTimeRule(dtfields[1], weekInMonth,
                                                       dtfields[3], dtfields[5],
                                                       IBM.ICU.Util.DateTimeRule.WALL_TIME);
                                secondRule = new AnnualTimeZoneRule(tr.GetTo()
                                                                    .GetName(), tr.GetTo().GetRawOffset(), tr
                                                                    .GetTo().GetDSTSavings(), dtr,
                                                                    annualRules[0].GetStartYear() - 1,
                                                                    IBM.ICU.Util.AnnualTimeZoneRule.MAX_YEAR);
                                // Check if this rule start after the first rule
                                // after the specified date
                                DateTime d_0 = secondRule.GetNextStart(date, tr.GetFrom()
                                                                       .GetRawOffset(), tr.GetFrom()
                                                                       .GetDSTSavings(), false);
                                if ((d_0.Ticks / 10000) > nextTransitionTime)
                                {
                                    // We can use this rule as the second transition
                                    // rule
                                    annualRules[1] = secondRule;
                                }
                            }
                        }
                    }
                    if (annualRules[1] == null)
                    {
                        // Cannot generate a good pair of AnnualTimeZoneRule
                        annualRules = null;
                    }
                    else
                    {
                        // The initial rule should represent the rule before the
                        // previous transition
                        initialName = annualRules[0].GetName();
                        initialRaw  = annualRules[0].GetRawOffset();
                        initialDst  = annualRules[0].GetDSTSavings();
                    }
                }
                initialRule = new InitialTimeZoneRule(initialName, initialRaw,
                                                      initialDst);
            }
            else
            {
                // Try the previous one
                tr = GetPreviousTransition(date, true);
                if (tr != null)
                {
                    initialRule = new InitialTimeZoneRule(tr.GetTo().GetName(), tr
                                                          .GetTo().GetRawOffset(), tr.GetTo().GetDSTSavings());
                }
                else
                {
                    // No transitions in the past. Just use the current offsets
                    int[] offsets = new int[2];
                    GetOffset(date, false, offsets);
                    initialRule = new InitialTimeZoneRule(GetID(), offsets[0],
                                                          offsets[1]);
                }
            }

            TimeZoneRule[] result = null;
            if (annualRules == null)
            {
                result    = new TimeZoneRule[1];
                result[0] = initialRule;
            }
            else
            {
                result    = new TimeZoneRule[3];
                result[0] = initialRule;
                result[1] = annualRules[0];
                result[2] = annualRules[1];
            }

            return(result);
        }
Пример #3
0
 /// <summary>
 /// Constructs a <c>RuleBasedTimeZone</c> object with the ID and the
 /// <c>InitialTimeZoneRule</c>
 /// </summary>
 ///
 /// <param name="id">The time zone ID.</param>
 /// <param name="initialRule_0">The initial time zone rule.</param>
 /// @draft ICU 3.8
 /// @provisional This API might change or be removed in a future release.
 public RuleBasedTimeZone(String id, InitialTimeZoneRule initialRule_0)
 {
     base.SetID(id);
     this.initialRule = initialRule_0;
 }