/** formats time zone specifier. */
    private static void formatTimeZone(Calendar cal, StringBuilder buf) {
      TimeZone tz = cal.getTimeZone();

      if (tz == null) return;

      // otherwise print out normally.
      int offset;
      if (tz.inDaylightTime(cal.getTime())) {
        offset = tz.getRawOffset() + (tz.useDaylightTime() ? 3600000 : 0);
      } else {
        offset = tz.getRawOffset();
      }

      if (offset == 0) {
        buf.append('Z');
        return;
      }

      if (offset >= 0) buf.append('+');
      else {
        buf.append('-');
        offset *= -1;
      }

      offset /= 60 * 1000; // offset is in milli-seconds

      formatTwoDigits(offset / 60, buf);
      buf.append(':');
      formatTwoDigits(offset % 60, buf);
    }
示例#2
0
  // @Ignore
  @Test
  public void allTimeZoneTest() {

    String[] tzIds = TimeZone.getAvailableIDs();

    System.out.printf(
        "NUM) %32s, %5s \t,%15s\t,%15s,%6s\n",
        "Time Zone", "inDST", "offset", "DSTSavings", "useDST");
    for (int i = 0; i < tzIds.length; i++) {
      String string = tzIds[i];
      TimeZone timezone = TimeZone.getTimeZone(string);
      Date date = new Date();
      // System.out.println(i + ") " + timezone.getID() + ", offset " +
      // timezone.getOffset(date.getTime()) + ", DSTSavings " +
      // timezone.getDSTSavings());
      System.out.printf(
          "%3d) %32s, %5s \t,%15d\t,%15d,%6s\n",
          i,
          timezone.getID(),
          timezone.inDaylightTime(date),
          timezone.getOffset(date.getTime()),
          timezone.getDSTSavings(),
          timezone.useDaylightTime());
    }

    assertTrue(true);
  }
 /**
  * Sanity test for time zone generation. Most important, make sure that we can run through all of
  * the time zones without generating an exception. Second, make sure that we're finding rules for
  * at least 90% of time zones that use daylight time (empirically, it's more like 95%). Log those
  * without rules.
  *
  * @throws IOException
  */
 public void testTimeZoneToVTimezone() throws IOException {
   SimpleIcsWriter writer = new SimpleIcsWriter();
   int rule = 0;
   int nodst = 0;
   int norule = 0;
   ArrayList<String> norulelist = new ArrayList<String>();
   for (String tzs : TimeZone.getAvailableIDs()) {
     TimeZone tz = TimeZone.getTimeZone(tzs);
     writer = new SimpleIcsWriter();
     CalendarUtilities.timeZoneToVTimezone(tz, writer);
     String vc = writer.toString();
     boolean hasRule = vc.indexOf("RRULE") > 0;
     if (hasRule) {
       rule++;
     } else if (tz.useDaylightTime()) {
       norule++;
       norulelist.add(tz.getID());
     } else {
       nodst++;
     }
   }
   assertTrue(norule < rule / 10);
   Log.d("TimeZoneGeneration", "Rule: " + rule + ", No DST: " + nodst + ", No rule: " + norule);
   for (String nr : norulelist) {
     Log.d("TimeZoneGeneration", "No rule: " + nr);
   }
 }
示例#4
0
文件: Case.java 项目: halbbob/autopsy
  /**
   * Convert the Java timezone ID to the "formatted" string that can be accepted by the C/C++ code.
   * Example: "America/New_York" converted to "EST5EDT", etc
   *
   * @param timezoneID
   * @return
   */
  public static String convertTimeZone(String timezoneID) {
    String result = "";

    TimeZone zone = TimeZone.getTimeZone(timezoneID);
    int offset = zone.getRawOffset() / 1000;
    int hour = offset / 3600;
    int min = (offset % 3600) / 60;

    DateFormat dfm = new SimpleDateFormat("z");
    dfm.setTimeZone(zone);
    boolean hasDaylight = zone.useDaylightTime();
    String first =
        dfm.format(new GregorianCalendar(2010, 1, 1).getTime())
            .substring(0, 3); // make it only 3 letters code
    String second =
        dfm.format(new GregorianCalendar(2011, 6, 6).getTime())
            .substring(0, 3); // make it only 3 letters code
    int mid = hour * -1;
    result = first + Integer.toString(mid);
    if (min != 0) {
      result = result + ":" + Integer.toString(min);
    }
    if (hasDaylight) {
      result = result + second;
    }

    return result;
  }
示例#5
0
    /**
     * Construct a Strategy that parses a TimeZone
     *
     * @param locale The Locale
     */
    TimeZoneStrategy(final Locale locale) {
      final String[][] zones = DateFormatSymbols.getInstance(locale).getZoneStrings();
      for (String[] zone : zones) {
        if (zone[ID].startsWith("GMT")) {
          continue;
        }
        final TimeZone tz = TimeZone.getTimeZone(zone[ID]);
        if (!tzNames.containsKey(zone[LONG_STD])) {
          tzNames.put(zone[LONG_STD], tz);
        }
        if (!tzNames.containsKey(zone[SHORT_STD])) {
          tzNames.put(zone[SHORT_STD], tz);
        }
        if (tz.useDaylightTime()) {
          if (!tzNames.containsKey(zone[LONG_DST])) {
            tzNames.put(zone[LONG_DST], tz);
          }
          if (!tzNames.containsKey(zone[SHORT_DST])) {
            tzNames.put(zone[SHORT_DST], tz);
          }
        }
      }

      final StringBuilder sb = new StringBuilder();
      sb.append("(GMT[+\\-]\\d{0,1}\\d{2}|[+\\-]\\d{2}:?\\d{2}|");
      for (final String id : tzNames.keySet()) {
        escapeRegex(sb, id, false).append('|');
      }
      sb.setCharAt(sb.length() - 1, ')');
      validTimeZoneChars = sb.toString();
    }
 public TimeZoneRow(String id, String name) {
   mId = id;
   TimeZone tz = TimeZone.getTimeZone(id);
   boolean useDaylightTime = tz.useDaylightTime();
   mOffset = tz.getOffset(mTime);
   mDisplayName = buildGmtDisplayName(name, useDaylightTime);
 }
示例#7
0
 /**
  * Get the DST offset (if any) for the default locale and the given date.
  *
  * @param self a Date
  * @return the DST offset as a Duration.
  */
 public static Duration getDaylightSavingsOffset(Date self) {
   TimeZone timeZone = getTimeZone(self);
   int millis =
       (timeZone.useDaylightTime() && timeZone.inDaylightTime(self))
           ? timeZone.getDSTSavings()
           : 0;
   return new TimeDuration(0, 0, 0, millis);
 }
示例#8
0
 /** {@inheritDoc} */
 @Override
 public void appendTo(StringBuffer buffer, Calendar calendar) {
   if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
     buffer.append(mDaylight);
   } else {
     buffer.append(mStandard);
   }
 }
示例#9
0
 public String toSubtitle() {
   String result = "UTC" + Utils.offsetString(timeZone.getRawOffset(), true, false);
   if (timeZone.useDaylightTime()) {
     result += " / DST " + Utils.offsetString(timeZone.getDSTSavings(), false, true);
     // String.format(Locale.US, " / DST %+d minutes", timeZone.getDSTSavings()/1000/60);
   }
   return result;
 }
示例#10
0
 /** {@inheritDoc} */
 @Override
 public void appendTo(final StringBuffer buffer, final Calendar calendar) {
   final TimeZone zone = calendar.getTimeZone();
   if (zone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
     buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
   } else {
     buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
   }
 }
  private CharSequence buildGmtDisplayName(TimeZone tz, long timeMillis, boolean grayGmt) {
    Time time = new Time(tz.getID());
    time.set(timeMillis);

    StringBuilder sb = new StringBuilder();

    String displayName = getDisplayName(tz, time.isDst != 0);
    sb.append(displayName);

    sb.append("  ");
    final int gmtOffset = tz.getOffset(timeMillis);
    int gmtStart = sb.length();
    appendGmtOffset(sb, gmtOffset);
    int gmtEnd = sb.length();

    int symbolStart = 0;
    int symbolEnd = 0;
    if (tz.useDaylightTime()) {
      sb.append(" ");
      symbolStart = sb.length();
      sb.append(getDstSymbol()); // Sun symbol
      symbolEnd = sb.length();
    }

    // Set the gray colors.
    Spannable spannableText = mSpannableFactory.newSpannable(sb);
    if (grayGmt) {
      spannableText.setSpan(
          new ForegroundColorSpan(GMT_TEXT_COLOR),
          gmtStart,
          gmtEnd,
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (tz.useDaylightTime()) {
      spannableText.setSpan(
          new ForegroundColorSpan(DST_SYMBOL_COLOR),
          symbolStart,
          symbolEnd,
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    CharSequence gmtDisplayName = spannableText;
    return gmtDisplayName;
  }
示例#12
0
    public void appendTo(StringBuffer buffer, Calendar calendar) {
      TimeZone timeZone;
      if ((timeZone = mTimeZone) != null) {
        if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {

          buffer.append(mDaylight);
        } else {
          buffer.append(mStandard);
        }
      } else {
        timeZone = calendar.getTimeZone();
        if (timeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {

          buffer.append(getTimeZoneDisplay(timeZone, true, mStyle, mLocale));
        } else {
          buffer.append(getTimeZoneDisplay(timeZone, false, mStyle, mLocale));
        }
      }
    }
示例#13
0
 private static void printTZ(TimeZone tz) {
   String tzid = tz.getID();
   String gmtOffset = toGMTOffsetString(tz.getRawOffset());
   boolean isDst = tz.useDaylightTime();
   String clazz = tz.getClass().getSimpleName();
   String text =
       String.format(
           "%-20s (%s) - hasDST=%s (%s:%08x)",
           tzid, gmtOffset, (isDst ? "Y" : "N"), clazz, tz.hashCode());
   System.out.println(text);
 }
  void test1() {
    Locale[] available = Locale.getAvailableLocales();
    List<Locale> jreimplloc =
        Arrays.asList(
            LocaleProviderAdapter.forJRE().getTimeZoneNameProvider().getAvailableLocales());
    List<Locale> providerLocales = Arrays.asList(tznp.getAvailableLocales());
    String[] ids = TimeZone.getAvailableIDs();

    for (Locale target : available) {
      // pure JRE implementation
      OpenListResourceBundle rb =
          ((ResourceBundleBasedAdapter) LocaleProviderAdapter.forJRE())
              .getLocaleData()
              .getTimeZoneNames(target);
      boolean jreSupportsTarget = jreimplloc.contains(target);

      for (String id : ids) {
        // the time zone
        TimeZone tz = TimeZone.getTimeZone(id);

        // JRE string array for the id
        String[] jrearray = null;
        if (jreSupportsTarget) {
          try {
            jrearray = rb.getStringArray(id);
          } catch (MissingResourceException mre) {
          }
        }

        for (int i = 1; i <= (tz.useDaylightTime() ? 4 : 2); i++) {
          // the localized name
          String name = tz.getDisplayName(i >= 3, i % 2, target);

          // provider's name (if any)
          String providersname = null;
          if (providerLocales.contains(target)) {
            providersname = tznp.getDisplayName(id, i >= 3, i % 2, target);
          }

          // JRE's name
          String jresname = null;
          if (jrearray != null) {
            jresname = jrearray[i];
          }

          checkValidity(
              target, jresname, providersname, name, jreSupportsTarget && jresname != null);
        }
      }
    }
  }
示例#15
0
 private void displayTZ(TimeZone... timezone) {
   System.out.printf(
       "%32s, %5s \t,%15s\t,%15s,%6s\n", "Time Zone", "inDST", "offset", "DSTSavings", "useDST");
   Date date = new Date();
   for (TimeZone tz : timezone) {
     System.out.printf(
         "%32s, %5s \t,%15d(%d)\t,%15d,%6s\n",
         tz.getID(),
         tz.inDaylightTime(date),
         tz.getOffset(date.getTime()),
         tz.getOffset(date.getTime()) / (60 * 60 * 1000),
         tz.getDSTSavings(),
         tz.useDaylightTime());
   }
 }