Java Code Examples for java.util.TimeZone#getDSTSavings()

The following examples show how to use java.util.TimeZone#getDSTSavings() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: opentasks   File: TimeZoneWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object object)
{
    if (!(object instanceof TimeZoneWrapper)) // matches null too
    {
        return false;
    }

    TimeZone otherTimeZone = (TimeZone) object;

    /*
     * This is a very simple check for equality of two time zones. It returns the wrong result if two time zones have the same UTC offset, but use different
     * dates to switch to summer time.
     *
     * Are there such cases? How can we improve it? Maybe by testing a few more days in March and October?
     *
     * TODO: improve the check
     */
    return (mTimeZone.getID().equals(otherTimeZone.getID()))
            || (mTimeZone.useDaylightTime() == otherTimeZone.useDaylightTime() && mTimeZone.getRawOffset() == otherTimeZone.getRawOffset()
            && mTimeZone.getDSTSavings() == otherTimeZone.getDSTSavings() && mTimeZone.inDaylightTime(TEST_DATE) == otherTimeZone.inDaylightTime(
            TEST_DATE));
}
 
Example 2
Source Project: reladomo   File: TestImmutableTimestamp.java    License: Apache License 2.0 5 votes vote down vote up
protected static int getOffsetFromTimeZone(TimeZone tz, Date date)
{
    int offset = tz.getRawOffset();
    if (tz.inDaylightTime(date))
    {
        offset += tz.getDSTSavings();
    }
    return offset;
}
 
Example 3
public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException {
	cfStructData timeInfo = new cfStructData();
	TimeZone tz = (TimeZone) TimeZone.getDefault().clone();
	int dstCompensation = 0;

	boolean dst = tz.inDaylightTime(new Date());

	if (dst)
		dstCompensation = tz.getDSTSavings(); // the # of milliseconds in an hour.

	long offset = tz.getRawOffset() + dstCompensation;

	offset = offset * -1; // cfmx livedocs for this function mandate that the
												// sign be this way (which happens to be opposite of
												// what Java does)

	long totalOffSet = offset / 1000L;
	long hour = offset / (1000L * 60L * 60L);
	long minutes = offset / (1000L * 60L);
	long partialHourAsMinutes = minutes % 60; // to remove all whole hours

	timeInfo.setData("utctotaloffset", new cfNumberData(totalOffSet));
	timeInfo.setData("utchouroffset", new cfNumberData(hour));
	timeInfo.setData("utcminuteoffset", new cfNumberData(partialHourAsMinutes));
	timeInfo.setData("isdston", cfBooleanData.getcfBooleanData(dst));

	return timeInfo;
}
 
Example 4
Source Project: es6draft   File: TimeZoneInfo.java    License: MIT License 5 votes vote down vote up
@Override
public int getDSTSavings(TimeZone tz, long date) {
    if (tz.inDaylightTime(new Date(date))) {
        return tz.getDSTSavings();
    }
    return 0;
}
 
Example 5
Source Project: Bats   File: Util.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Writes a daylight savings time transition to a POSIX timezone
 * description.
 *
 * @param tz        Timezone
 * @param buf       Buffer to append to
 * @param mode      Transition mode
 * @param day       Day of transition
 * @param month     Month of transition
 * @param dayOfWeek Day of week of transition
 * @param time      Time of transition in millis
 * @param timeMode  Mode of time transition
 * @param verbose   Verbose
 * @param isEnd     Whether this transition is leaving DST
 */
private static void appendPosixDaylightTransition(
    TimeZone tz,
    StringBuilder buf,
    int mode,
    int day,
    int month,
    int dayOfWeek,
    int time,
    int timeMode,
    boolean verbose,
    boolean isEnd) {
  buf.append(',');
  int week = day;
  switch (mode) {
  case 1: // SimpleTimeZone.DOM_MODE
    throw Util.needToImplement(0);

  case 3: // SimpleTimeZone.DOW_GE_DOM_MODE

    // If the day is 1, 8, 15, 22, we can translate this to case 2.
    switch (day) {
    case 1:
      week = 1; // 1st week of month
      break;
    case 8:
      week = 2; // 2nd week of month
      break;
    case 15:
      week = 3; // 3rd week of month
      break;
    case 22:
      week = 4; // 4th week of month
      break;
    default:
      throw new AssertionError(
          "POSIX timezone format cannot represent " + tz);
    }
    // fall through

  case 2: // SimpleTimeZone.DOW_IN_MONTH_MODE
    buf.append('M');
    buf.append(month + 1); // 1 <= m <= 12
    buf.append('.');
    if (week == -1) {
      // java represents 'last week' differently from POSIX
      week = 5;
    }
    buf.append(week); // 1 <= n <= 5, 5 means 'last'
    buf.append('.');
    buf.append(dayOfWeek - 1); // 0 <= d <= 6, 0=Sunday
    break;

  case 4: // SimpleTimeZone.DOW_LE_DOM_MODE
    throw Util.needToImplement(0);
  default:
    throw new AssertionError("unexpected value: " + mode);
  }
  switch (timeMode) {
  case 0: // SimpleTimeZone.WALL_TIME
    break;
  case 1: // SimpleTimeZone.STANDARD_TIME, e.g. Australia/Sydney
    if (isEnd) {
      time += tz.getDSTSavings();
    }
    break;
  case 2: // SimpleTimeZone.UTC_TIME, e.g. Europe/Paris
    time += tz.getRawOffset();
    if (isEnd) {
      time += tz.getDSTSavings();
    }
    break;
  }
  if (verbose || (time != 7200000)) {
    // POSIX allows us to omit the time if it is 2am (the default)
    buf.append('/');
    appendPosixTime(buf, time);
  }
}
 
Example 6
/**
 * find time zone 'text' matched zoneStrings and set to internal
 * calendar.
 */
private int subParseZoneString(String text, int start, CalendarBuilder calb) {
    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
    TimeZone currentTimeZone = getTimeZone();

    // At this point, check for named time zones by looking through
    // the locale data from the TimeZoneNames strings.
    // Want to be able to parse both short and long forms.
    int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
    TimeZone tz = null;
    String[][] zoneStrings = formatData.getZoneStringsWrapper();
    String[] zoneNames = null;
    int nameIndex = 0;
    if (zoneIndex != -1) {
        zoneNames = zoneStrings[zoneIndex];
        if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
            if (nameIndex <= 2) {
                // Check if the standard name (abbr) and the daylight name are the same.
                useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
            }
            tz = TimeZone.getTimeZone(zoneNames[0]);
        }
    }
    if (tz == null) {
        zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
        if (zoneIndex != -1) {
            zoneNames = zoneStrings[zoneIndex];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
            }
        }
    }

    if (tz == null) {
        int len = zoneStrings.length;
        for (int i = 0; i < len; i++) {
            zoneNames = zoneStrings[i];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
                break;
            }
        }
    }
    if (tz != null) { // Matched any ?
        if (!tz.equals(currentTimeZone)) {
            setTimeZone(tz);
        }
        // If the time zone matched uses the same name
        // (abbreviation) for both standard and daylight time,
        // let the time zone in the Calendar decide which one.
        //
        // Also if tz.getDSTSaving() returns 0 for DST, use tz to
        // determine the local time. (6645292)
        int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
        if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
            calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
        }
        return (start + zoneNames[nameIndex].length());
    }
    return -start;
}
 
Example 7
Source Project: Quicksql   File: Util.java    License: MIT License 4 votes vote down vote up
/**
 * Writes a daylight savings time transition to a POSIX timezone
 * description.
 *
 * @param tz        Timezone
 * @param buf       Buffer to append to
 * @param mode      Transition mode
 * @param day       Day of transition
 * @param month     Month of transition
 * @param dayOfWeek Day of week of transition
 * @param time      Time of transition in millis
 * @param timeMode  Mode of time transition
 * @param verbose   Verbose
 * @param isEnd     Whether this transition is leaving DST
 */
private static void appendPosixDaylightTransition(
    TimeZone tz,
    StringBuilder buf,
    int mode,
    int day,
    int month,
    int dayOfWeek,
    int time,
    int timeMode,
    boolean verbose,
    boolean isEnd) {
  buf.append(',');
  int week = day;
  switch (mode) {
  case 1: // SimpleTimeZone.DOM_MODE
    throw Util.needToImplement(0);

  case 3: // SimpleTimeZone.DOW_GE_DOM_MODE

    // If the day is 1, 8, 15, 22, we can translate this to case 2.
    switch (day) {
    case 1:
      week = 1; // 1st week of month
      break;
    case 8:
      week = 2; // 2nd week of month
      break;
    case 15:
      week = 3; // 3rd week of month
      break;
    case 22:
      week = 4; // 4th week of month
      break;
    default:
      throw new AssertionError(
          "POSIX timezone format cannot represent " + tz);
    }
    // fall through

  case 2: // SimpleTimeZone.DOW_IN_MONTH_MODE
    buf.append('M');
    buf.append(month + 1); // 1 <= m <= 12
    buf.append('.');
    if (week == -1) {
      // java represents 'last week' differently from POSIX
      week = 5;
    }
    buf.append(week); // 1 <= n <= 5, 5 means 'last'
    buf.append('.');
    buf.append(dayOfWeek - 1); // 0 <= d <= 6, 0=Sunday
    break;

  case 4: // SimpleTimeZone.DOW_LE_DOM_MODE
    throw Util.needToImplement(0);
  default:
    throw new AssertionError("unexpected value: " + mode);
  }
  switch (timeMode) {
  case 0: // SimpleTimeZone.WALL_TIME
    break;
  case 1: // SimpleTimeZone.STANDARD_TIME, e.g. Australia/Sydney
    if (isEnd) {
      time += tz.getDSTSavings();
    }
    break;
  case 2: // SimpleTimeZone.UTC_TIME, e.g. Europe/Paris
    time += tz.getRawOffset();
    if (isEnd) {
      time += tz.getDSTSavings();
    }
    break;
  }
  if (verbose || (time != 7200000)) {
    // POSIX allows us to omit the time if it is 2am (the default)
    buf.append('/');
    appendPosixTime(buf, time);
  }
}
 
Example 8
/**
 * find time zone 'text' matched zoneStrings and set to internal
 * calendar.
 */
private int subParseZoneString(String text, int start, CalendarBuilder calb) {
    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
    TimeZone currentTimeZone = getTimeZone();

    // At this point, check for named time zones by looking through
    // the locale data from the TimeZoneNames strings.
    // Want to be able to parse both short and long forms.
    int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
    TimeZone tz = null;
    String[][] zoneStrings = formatData.getZoneStringsWrapper();
    String[] zoneNames = null;
    int nameIndex = 0;
    if (zoneIndex != -1) {
        zoneNames = zoneStrings[zoneIndex];
        if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
            if (nameIndex <= 2) {
                // Check if the standard name (abbr) and the daylight name are the same.
                useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
            }
            tz = TimeZone.getTimeZone(zoneNames[0]);
        }
    }
    if (tz == null) {
        zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
        if (zoneIndex != -1) {
            zoneNames = zoneStrings[zoneIndex];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
            }
        }
    }

    if (tz == null) {
        int len = zoneStrings.length;
        for (int i = 0; i < len; i++) {
            zoneNames = zoneStrings[i];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
                break;
            }
        }
    }
    if (tz != null) { // Matched any ?
        if (!tz.equals(currentTimeZone)) {
            setTimeZone(tz);
        }
        // If the time zone matched uses the same name
        // (abbreviation) for both standard and daylight time,
        // let the time zone in the Calendar decide which one.
        //
        // Also if tz.getDSTSaving() returns 0 for DST, use tz to
        // determine the local time. (6645292)
        int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
        if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
            calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
        }
        return (start + zoneNames[nameIndex].length());
    }
    return -start;
}
 
Example 9
/**
 * find time zone 'text' matched zoneStrings and set to internal
 * calendar.
 */
private int subParseZoneString(String text, int start, CalendarBuilder calb) {
    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
    TimeZone currentTimeZone = getTimeZone();

    // At this point, check for named time zones by looking through
    // the locale data from the TimeZoneNames strings.
    // Want to be able to parse both short and long forms.
    int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
    TimeZone tz = null;
    String[][] zoneStrings = formatData.getZoneStringsWrapper();
    String[] zoneNames = null;
    int nameIndex = 0;
    if (zoneIndex != -1) {
        zoneNames = zoneStrings[zoneIndex];
        if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
            if (nameIndex <= 2) {
                // Check if the standard name (abbr) and the daylight name are the same.
                useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
            }
            tz = TimeZone.getTimeZone(zoneNames[0]);
        }
    }
    if (tz == null) {
        zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
        if (zoneIndex != -1) {
            zoneNames = zoneStrings[zoneIndex];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
            }
        }
    }

    if (tz == null) {
        int len = zoneStrings.length;
        for (int i = 0; i < len; i++) {
            zoneNames = zoneStrings[i];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
                break;
            }
        }
    }
    if (tz != null) { // Matched any ?
        if (!tz.equals(currentTimeZone)) {
            setTimeZone(tz);
        }
        // If the time zone matched uses the same name
        // (abbreviation) for both standard and daylight time,
        // let the time zone in the Calendar decide which one.
        //
        // Also if tz.getDSTSaving() returns 0 for DST, use tz to
        // determine the local time. (6645292)
        int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
        if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
            calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
        }
        return (start + zoneNames[nameIndex].length());
    }
    return 0;
}
 
Example 10
/**
 * find time zone 'text' matched zoneStrings and set to internal
 * calendar.
 */
private int subParseZoneString(String text, int start, CalendarBuilder calb) {
    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
    TimeZone currentTimeZone = getTimeZone();

    // At this point, check for named time zones by looking through
    // the locale data from the TimeZoneNames strings.
    // Want to be able to parse both short and long forms.
    int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
    TimeZone tz = null;
    String[][] zoneStrings = formatData.getZoneStringsWrapper();
    String[] zoneNames = null;
    int nameIndex = 0;
    if (zoneIndex != -1) {
        zoneNames = zoneStrings[zoneIndex];
        if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
            if (nameIndex <= 2) {
                // Check if the standard name (abbr) and the daylight name are the same.
                useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
            }
            tz = TimeZone.getTimeZone(zoneNames[0]);
        }
    }
    if (tz == null) {
        zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
        if (zoneIndex != -1) {
            zoneNames = zoneStrings[zoneIndex];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
            }
        }
    }

    if (tz == null) {
        int len = zoneStrings.length;
        for (int i = 0; i < len; i++) {
            zoneNames = zoneStrings[i];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
                break;
            }
        }
    }
    if (tz != null) { // Matched any ?
        if (!tz.equals(currentTimeZone)) {
            setTimeZone(tz);
        }
        // If the time zone matched uses the same name
        // (abbreviation) for both standard and daylight time,
        // let the time zone in the Calendar decide which one.
        //
        // Also if tz.getDSTSaving() returns 0 for DST, use tz to
        // determine the local time. (6645292)
        int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
        if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
            calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
        }
        return (start + zoneNames[nameIndex].length());
    }
    return 0;
}
 
Example 11
private void init()
{
	// Setup full event string that will be used in ping
	StringBuilder event = new StringBuilder();
	IAnalyticsUserManager userManager = getUserManager();
	IAnalyticsUser user = (userManager == null) ? null : userManager.getUser();

	addPostEntry(event, "event", eventName); //$NON-NLS-1$
	addPostEntry(event, "type", eventType); //$NON-NLS-1$
	if (StringUtil.isEmpty(sessionId))
	{
		if (user != null)
		{
			sessionId = user.getSessionID();
		}
		if (StringUtil.isEmpty(sessionId))
		{
			sessionId = UUID.randomUUID().toString();
		}
	}
	addPostEntry(event, "sid", sessionId); //$NON-NLS-1$
	addPostEntry(event, "guid", APP_INFO.getAppGuid()); //$NON-NLS-1$
	addPostEntry(event, "mid", CorePlugin.getMID()); //$NON-NLS-1$
	addPostEntry(event, "app_id", APP_INFO.getAppId()); //$NON-NLS-1$
	addPostEntry(event, "creator_user_id", (user == null) ? StringUtil.EMPTY : user.getUID()); //$NON-NLS-1$
	addPostEntry(event, "app_name", APP_INFO.getAppName()); //$NON-NLS-1$
	addPostEntry(event, "app_version", EclipseUtil.getPluginVersion(APP_INFO.getVersionPluginId())); //$NON-NLS-1$
	addPostEntry(event, "mac_addr", MACAddress.getMACAddress()); //$NON-NLS-1$
	addPostEntry(event, "platform", Platform.OS_MACOSX.equals(Platform.getOS()) ? "osx" : Platform.getOS()); //$NON-NLS-1$ //$NON-NLS-2$
	// This field was used for the versions of titanium sdk that developer was build on. This does not apply to
	// studio so we are leaving it as 1.1.0 for now.
	addPostEntry(event, "version", "1.1.0"); //$NON-NLS-1$ //$NON-NLS-2$
	addPostEntry(event, "os", System.getProperty("os.name")); //$NON-NLS-1$ //$NON-NLS-2$
	addPostEntry(event, "ostype", System.getProperty("sun.arch.data.model") + "bit"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	addPostEntry(event, "osver", System.getProperty("os.version")); //$NON-NLS-1$ //$NON-NLS-2$
	addPostEntry(event, "osarch", System.getProperty("os.arch")); //$NON-NLS-1$ //$NON-NLS-2$
	addPostEntry(event, "oscpu", Integer.toString(Runtime.getRuntime().availableProcessors())); //$NON-NLS-1$
	addPostEntry(event, "un", (user == null) ? StringUtil.EMPTY : user.getUsername()); //$NON-NLS-1$
	addPostEntry(event, "ver", SPEC_VERSION); //$NON-NLS-1$

	TimeZone tz = TimeZone.getDefault();
	int results = -(tz.getDSTSavings() + tz.getRawOffset());
	results = (results / 1000) / 60;
	addPostEntry(event, "tz", Integer.toString(results)); //$NON-NLS-1$

	InetAddress ip;
	try
	{
		ip = InetAddress.getLocalHost();
		addPostEntry(event, "ip", ip.getHostAddress()); //$NON-NLS-1$
	}
	catch (UnknownHostException e)
	{
		addPostEntry(event, "ip", StringUtil.EMPTY); //$NON-NLS-1$
	}

	if (!EMPTY_JSON_PAYLOAD.equals(getJSONPayloadString()))
	{
		// We need to strip the quotes surrounding the languageModules entry, also unescape quotes inside the entry
		String formattedJSON = getJSONPayloadString();
		if (formattedJSON.contains("languageModules")) //$NON-NLS-1$
		{
			formattedJSON = formattedJSON.replace("\"{", "{"); //$NON-NLS-1$ //$NON-NLS-2$
			formattedJSON = formattedJSON.replace("}\"", "}"); //$NON-NLS-1$ //$NON-NLS-2$
			formattedJSON = formattedJSON.replace("\\\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		addPostEntry(event, "data", formattedJSON); //$NON-NLS-1$
	}

	// Remove the extra & that we insert
	event.deleteCharAt(event.lastIndexOf("&")); //$NON-NLS-1$

	eventString = event.toString();
}
 
Example 12
Source Project: OpenFit   File: OpenFitApi.java    License: MIT License 4 votes vote down vote up
public static byte[] getCurrentTimeInfo(boolean is24Hour) {
    //011E0000000141CB3555F8FFFFFF000000000101010201A01DFC5490D43556100E0000
    //01
    //1e000000
    //01
    //41cb3555
    //f8ffffff
    //00000000
    //01
    //01
    //01
    //02
    //01
    //a01dfc54
    //90d43556
    //100e0000

    // build time data
    int millis = (int)(System.currentTimeMillis() / 1000L);
    Calendar oCalendar = Calendar.getInstance();
    TimeZone oTimeZone = oCalendar.getTimeZone();
    int i = oTimeZone.getRawOffset() / 60000;
    int j = i / 60;
    int k = i % 60;
    Date oDate = oCalendar.getTime();
    boolean inDaylightTime = oTimeZone.inDaylightTime(oDate);
    boolean useDaylightTime = oTimeZone.useDaylightTime();
    long l = oCalendar.getTimeInMillis();
    int m = (int)(OpenFitTimeZoneUtil.prevTransition(oTimeZone, l) / 1000L);
    int n = (int)(OpenFitTimeZoneUtil.nextTransition(oTimeZone, l) / 1000L);
    int dst = oTimeZone.getDSTSavings() / 1000;

    // write time data
    OpenFitVariableDataComposer oVDC = new OpenFitVariableDataComposer();
    oVDC.writeByte((byte)1);
    oVDC.writeInt(millis);
    oVDC.writeInt(j);
    oVDC.writeInt(k);
    oVDC.writeByte(OpenFitData.TEXT_DATE_FORMAT_TYPE);
    //oVDC.writeBoolean(OpenFitData.IS_TIME_DISPLAY_24);
    oVDC.writeBoolean(is24Hour);
    oVDC.writeBoolean(inDaylightTime);
    oVDC.writeByte(OpenFitData.NUMBER_DATE_FORMAT_TYPE);
    oVDC.writeBoolean(useDaylightTime);
    oVDC.writeInt(m);
    oVDC.writeInt(n);
    oVDC.writeInt(dst);
    int length = oVDC.toByteArray().length;

    // write time byte array
    OpenFitVariableDataComposer oVariableDataComposer = new OpenFitVariableDataComposer();
    oVariableDataComposer.writeByte((byte)1);
    oVariableDataComposer.writeInt(length);
    oVariableDataComposer.writeBytes(oVDC.toByteArray());
    return oVariableDataComposer.toByteArray();
}
 
Example 13
/**
 * find time zone 'text' matched zoneStrings and set to internal
 * calendar.
 */
private int subParseZoneString(String text, int start, CalendarBuilder calb) {
    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
    TimeZone currentTimeZone = getTimeZone();

    // At this point, check for named time zones by looking through
    // the locale data from the TimeZoneNames strings.
    // Want to be able to parse both short and long forms.
    int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
    TimeZone tz = null;
    String[][] zoneStrings = formatData.getZoneStringsWrapper();
    String[] zoneNames = null;
    int nameIndex = 0;
    if (zoneIndex != -1) {
        zoneNames = zoneStrings[zoneIndex];
        if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
            if (nameIndex <= 2) {
                // Check if the standard name (abbr) and the daylight name are the same.
                useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
            }
            tz = TimeZone.getTimeZone(zoneNames[0]);
        }
    }
    if (tz == null) {
        zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
        if (zoneIndex != -1) {
            zoneNames = zoneStrings[zoneIndex];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
            }
        }
    }

    if (tz == null) {
        int len = zoneStrings.length;
        for (int i = 0; i < len; i++) {
            zoneNames = zoneStrings[i];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
                break;
            }
        }
    }
    if (tz != null) { // Matched any ?
        if (!tz.equals(currentTimeZone)) {
            setTimeZone(tz);
        }
        // If the time zone matched uses the same name
        // (abbreviation) for both standard and daylight time,
        // let the time zone in the Calendar decide which one.
        //
        // Also if tz.getDSTSaving() returns 0 for DST, use tz to
        // determine the local time. (6645292)
        int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
        if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
            calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
        }
        return (start + zoneNames[nameIndex].length());
    }
    return 0;
}
 
Example 14
Source Project: PrayTime-Android   File: PrayTime.java    License: Apache License 2.0 4 votes vote down vote up
private double detectDaylightSaving() {
  TimeZone timez = TimeZone.getDefault();
  double hoursDiff = timez.getDSTSavings();
  return hoursDiff;
}
 
Example 15
Source Project: logging-log4j2   File: FastDateParser.java    License: Apache License 2.0 4 votes vote down vote up
TzInfo(final TimeZone tz, final boolean useDst) {
    zone = tz;
    dstOffset = useDst ?tz.getDSTSavings() :0;
}
 
Example 16
Source Project: java-util   File: TestSimpleDateFormat.java    License: Apache License 2.0 4 votes vote down vote up
@Test
  public void testTimeZone() throws Exception
  {
      SafeSimpleDateFormat x = new SafeSimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      String s = x.format(getDate(2013, 9, 7, 16, 15, 31));
      assertEquals("2013-09-07 04:15:31", s);

      Date then = x.parse(s);
      Calendar cal = Calendar.getInstance();
      cal.clear();
      cal.setTime(then);
      assertEquals(2013, cal.get(Calendar.YEAR));
      assertEquals(8, cal.get(Calendar.MONTH));   // Sept
      assertEquals(7, cal.get(Calendar.DAY_OF_MONTH));
      assertEquals(4, cal.get(Calendar.HOUR_OF_DAY));
      assertEquals(15, cal.get(Calendar.MINUTE));
      assertEquals(31, cal.get(Calendar.SECOND));

      TimeZone localTz = TimeZone.getDefault();

      TimeZone tzLA = TimeZone.getTimeZone("America/Los_Angeles");

long txDiff = localTz.getRawOffset() + localTz.getDSTSavings()
		- tzLA.getRawOffset() - tzLA.getDSTSavings();

      x.setTimeZone(tzLA);

      Calendar expectedDate = Calendar.getInstance();
      expectedDate.setTimeInMillis(then.getTime() + txDiff);

      then = x.parse(s);
      cal = Calendar.getInstance();
      cal.clear();
      cal.setTime(then);
      assertEquals(expectedDate.get(Calendar.YEAR), cal.get(Calendar.YEAR));
      assertEquals(expectedDate.get(Calendar.MONTH), cal.get(Calendar.MONTH));   // Sept
      assertEquals(expectedDate.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH));
      assertEquals(expectedDate.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.HOUR_OF_DAY));
      assertEquals(expectedDate.get(Calendar.MINUTE), cal.get(Calendar.MINUTE));
      assertEquals(expectedDate.get(Calendar.SECOND), cal.get(Calendar.SECOND));
  }
 
Example 17
Source Project: xDrip   File: SetTimeTx.java    License: GNU General Public License v3.0 4 votes vote down vote up
private static long getActualDSTOffset(final TimeZone mTimeZone) {
    return (mTimeZone.useDaylightTime() && mTimeZone.inDaylightTime(new Date())) ? mTimeZone.getDSTSavings() : 0;
}
 
Example 18
Source Project: cloudstack   File: ApiResponseHelper.java    License: Apache License 2.0 4 votes vote down vote up
public String getDateStringInternal(Date inputDate) {
    if (inputDate == null) {
        return null;
    }

    TimeZone tz = _usageSvc.getUsageTimezone();
    Calendar cal = Calendar.getInstance(tz);
    cal.setTime(inputDate);

    StringBuilder sb = new StringBuilder(32);
    sb.append(cal.get(Calendar.YEAR)).append('-');

    int month = cal.get(Calendar.MONTH) + 1;
    if (month < 10) {
        sb.append('0');
    }
    sb.append(month).append('-');

    int day = cal.get(Calendar.DAY_OF_MONTH);
    if (day < 10) {
        sb.append('0');
    }
    sb.append(day);

    sb.append("'T'");

    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if (hour < 10) {
        sb.append('0');
    }
    sb.append(hour).append(':');

    int minute = cal.get(Calendar.MINUTE);
    if (minute < 10) {
        sb.append('0');
    }
    sb.append(minute).append(':');

    int seconds = cal.get(Calendar.SECOND);
    if (seconds < 10) {
        sb.append('0');
    }
    sb.append(seconds);

    double offset = cal.get(Calendar.ZONE_OFFSET);
    if (tz.inDaylightTime(inputDate)) {
        offset += (1.0 * tz.getDSTSavings()); // add the timezone's DST
        // value (typically 1 hour
        // expressed in milliseconds)
    }

    offset = offset / (1000d * 60d * 60d);
    int hourOffset = (int)offset;
    double decimalVal = Math.abs(offset) - Math.abs(hourOffset);
    int minuteOffset = (int)(decimalVal * 60);

    if (hourOffset < 0) {
        if (hourOffset > -10) {
            sb.append("-0");
        } else {
            sb.append('-');
        }
        sb.append(Math.abs(hourOffset));
    } else {
        if (hourOffset < 10) {
            sb.append("+0");
        } else {
            sb.append("+");
        }
        sb.append(hourOffset);
    }

    sb.append(':');

    if (minuteOffset == 0) {
        sb.append("00");
    } else if (minuteOffset < 10) {
        sb.append('0').append(minuteOffset);
    } else {
        sb.append(minuteOffset);
    }

    return sb.toString();
}
 
Example 19
Source Project: calcite   File: Util.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Writes a daylight savings time transition to a POSIX timezone
 * description.
 *
 * @param tz        Timezone
 * @param buf       Buffer to append to
 * @param mode      Transition mode
 * @param day       Day of transition
 * @param month     Month of transition
 * @param dayOfWeek Day of week of transition
 * @param time      Time of transition in millis
 * @param timeMode  Mode of time transition
 * @param verbose   Verbose
 * @param isEnd     Whether this transition is leaving DST
 */
private static void appendPosixDaylightTransition(
    TimeZone tz,
    StringBuilder buf,
    int mode,
    int day,
    int month,
    int dayOfWeek,
    int time,
    int timeMode,
    boolean verbose,
    boolean isEnd) {
  buf.append(',');
  int week = day;
  switch (mode) {
  case 1: // SimpleTimeZone.DOM_MODE
    throw Util.needToImplement(0);

  case 3: // SimpleTimeZone.DOW_GE_DOM_MODE

    // If the day is 1, 8, 15, 22, we can translate this to case 2.
    switch (day) {
    case 1:
      week = 1; // 1st week of month
      break;
    case 8:
      week = 2; // 2nd week of month
      break;
    case 15:
      week = 3; // 3rd week of month
      break;
    case 22:
      week = 4; // 4th week of month
      break;
    default:
      throw new AssertionError(
          "POSIX timezone format cannot represent " + tz);
    }
    // fall through

  case 2: // SimpleTimeZone.DOW_IN_MONTH_MODE
    buf.append('M');
    buf.append(month + 1); // 1 <= m <= 12
    buf.append('.');
    if (week == -1) {
      // java represents 'last week' differently from POSIX
      week = 5;
    }
    buf.append(week); // 1 <= n <= 5, 5 means 'last'
    buf.append('.');
    buf.append(dayOfWeek - 1); // 0 <= d <= 6, 0=Sunday
    break;

  case 4: // SimpleTimeZone.DOW_LE_DOM_MODE
    throw Util.needToImplement(0);
  default:
    throw new AssertionError("unexpected value: " + mode);
  }
  switch (timeMode) {
  case 0: // SimpleTimeZone.WALL_TIME
    break;
  case 1: // SimpleTimeZone.STANDARD_TIME, e.g. Australia/Sydney
    if (isEnd) {
      time += tz.getDSTSavings();
    }
    break;
  case 2: // SimpleTimeZone.UTC_TIME, e.g. Europe/Paris
    time += tz.getRawOffset();
    if (isEnd) {
      time += tz.getDSTSavings();
    }
    break;
  }
  if (verbose || (time != 7200000)) {
    // POSIX allows us to omit the time if it is 2am (the default)
    buf.append('/');
    appendPosixTime(buf, time);
  }
}
 
Example 20
/**
 * find time zone 'text' matched zoneStrings and set to internal
 * calendar.
 */
private int subParseZoneString(String text, int start, CalendarBuilder calb) {
    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
    TimeZone currentTimeZone = getTimeZone();

    // At this point, check for named time zones by looking through
    // the locale data from the TimeZoneNames strings.
    // Want to be able to parse both short and long forms.
    int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
    TimeZone tz = null;
    String[][] zoneStrings = formatData.getZoneStringsWrapper();
    String[] zoneNames = null;
    int nameIndex = 0;
    if (zoneIndex != -1) {
        zoneNames = zoneStrings[zoneIndex];
        if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
            if (nameIndex <= 2) {
                // Check if the standard name (abbr) and the daylight name are the same.
                useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
            }
            tz = TimeZone.getTimeZone(zoneNames[0]);
        }
    }
    if (tz == null) {
        zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
        if (zoneIndex != -1) {
            zoneNames = zoneStrings[zoneIndex];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
            }
        }
    }

    if (tz == null) {
        int len = zoneStrings.length;
        for (int i = 0; i < len; i++) {
            zoneNames = zoneStrings[i];
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
                if (nameIndex <= 2) {
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
                }
                tz = TimeZone.getTimeZone(zoneNames[0]);
                break;
            }
        }
    }
    if (tz != null) { // Matched any ?
        if (!tz.equals(currentTimeZone)) {
            setTimeZone(tz);
        }
        // If the time zone matched uses the same name
        // (abbreviation) for both standard and daylight time,
        // let the time zone in the Calendar decide which one.
        //
        // Also if tz.getDSTSaving() returns 0 for DST, use tz to
        // determine the local time. (6645292)
        int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
        if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
            calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
        }
        return (start + zoneNames[nameIndex].length());
    }
    return 0;
}