Method from com.mysql.jdbc.TimeUtil Detail: |
public static Time changeTimezone(ConnectionImpl conn,
Calendar sessionCalendar,
Calendar targetCalendar,
Time t,
TimeZone fromTz,
TimeZone toTz,
boolean rollForward) {
if ((conn != null)) {
if (conn.getUseTimezone() &&
!conn.getNoTimezoneConversionForTimeType()) {
// Convert the timestamp from GMT to the server's timezone
Calendar fromCal = Calendar.getInstance(fromTz);
fromCal.setTime(t);
int fromOffset = fromCal.get(Calendar.ZONE_OFFSET)
+ fromCal.get(Calendar.DST_OFFSET);
Calendar toCal = Calendar.getInstance(toTz);
toCal.setTime(t);
int toOffset = toCal.get(Calendar.ZONE_OFFSET)
+ toCal.get(Calendar.DST_OFFSET);
int offsetDiff = fromOffset - toOffset;
long toTime = toCal.getTime().getTime();
if (rollForward || (conn.isServerTzUTC() && !conn.isClientTzUTC())) {
toTime += offsetDiff;
} else {
toTime -= offsetDiff;
}
Time changedTime = new Time(toTime);
return changedTime;
} else if (conn.getUseJDBCCompliantTimezoneShift()) {
if (targetCalendar != null) {
Time adjustedTime = new Time(
jdbcCompliantZoneShift(sessionCalendar,
targetCalendar, t));
return adjustedTime;
}
}
}
return t;
}
Change the given times from one timezone to another |
public static Timestamp changeTimezone(ConnectionImpl conn,
Calendar sessionCalendar,
Calendar targetCalendar,
Timestamp tstamp,
TimeZone fromTz,
TimeZone toTz,
boolean rollForward) {
if ((conn != null)) {
if (conn.getUseTimezone()) {
// Convert the timestamp from GMT to the server's timezone
Calendar fromCal = Calendar.getInstance(fromTz);
fromCal.setTime(tstamp);
int fromOffset = fromCal.get(Calendar.ZONE_OFFSET)
+ fromCal.get(Calendar.DST_OFFSET);
Calendar toCal = Calendar.getInstance(toTz);
toCal.setTime(tstamp);
int toOffset = toCal.get(Calendar.ZONE_OFFSET)
+ toCal.get(Calendar.DST_OFFSET);
int offsetDiff = fromOffset - toOffset;
long toTime = toCal.getTime().getTime();
if (rollForward || (conn.isServerTzUTC() && !conn.isClientTzUTC())) {
toTime += offsetDiff;
} else {
toTime -= offsetDiff;
}
Timestamp changedTimestamp = new Timestamp(toTime);
return changedTimestamp;
} else if (conn.getUseJDBCCompliantTimezoneShift()) {
if (targetCalendar != null) {
Timestamp adjustedTimestamp = new Timestamp(
jdbcCompliantZoneShift(sessionCalendar,
targetCalendar, tstamp));
adjustedTimestamp.setNanos(tstamp.getNanos());
return adjustedTimestamp;
}
}
}
return tstamp;
}
Change the given timestamp from one timezone to another |
static final Date fastDateCreate(int year,
int month,
int day,
Calendar targetCalendar) {
Calendar dateCal = (targetCalendar == null) ? new GregorianCalendar() : targetCalendar;
dateCal.clear();
// why-oh-why is this different than java.util.date,
// in the year part, but it still keeps the silly '0'
// for the start month????
dateCal.set(year, month - 1, day, 0, 0, 0);
dateCal.set(Calendar.MILLISECOND, 0);
long dateAsMillis = 0;
try {
dateAsMillis = dateCal.getTimeInMillis();
} catch (IllegalAccessError iae) {
// Must be on JDK-1.3.1 or older....
dateAsMillis = dateCal.getTime().getTime();
}
return new Date(dateAsMillis);
}
|
static final Date fastDateCreate(boolean useGmtConversion,
Calendar gmtCalIfNeeded,
Calendar cal,
int year,
int month,
int day) {
Calendar dateCal = cal;
if (useGmtConversion) {
if (gmtCalIfNeeded == null) {
gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
}
gmtCalIfNeeded.clear();
dateCal = gmtCalIfNeeded;
}
dateCal.clear();
dateCal.set(Calendar.MILLISECOND, 0);
// why-oh-why is this different than java.util.date,
// in the year part, but it still keeps the silly '0'
// for the start month????
dateCal.set(year, month - 1, day, 0, 0, 0);
long dateAsMillis = 0;
try {
dateAsMillis = dateCal.getTimeInMillis();
} catch (IllegalAccessError iae) {
// Must be on JDK-1.3.1 or older....
dateAsMillis = dateCal.getTime().getTime();
}
return new Date(dateAsMillis);
}
|
static final Time fastTimeCreate(Calendar cal,
int hour,
int minute,
int second,
ExceptionInterceptor exceptionInterceptor) throws SQLException {
if (hour < 0 || hour > 24) {
throw SQLError.createSQLException("Illegal hour value '" + hour + "' for java.sql.Time type in value '"
+ timeFormattedString(hour, minute, second) + ".",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor);
}
if (minute < 0 || minute > 59) {
throw SQLError.createSQLException("Illegal minute value '" + minute + "'" + "' for java.sql.Time type in value '"
+ timeFormattedString(hour, minute, second) + ".",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor);
}
if (second < 0 || second > 59) {
throw SQLError.createSQLException("Illegal minute value '" + second + "'" + "' for java.sql.Time type in value '"
+ timeFormattedString(hour, minute, second) + ".",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor);
}
cal.clear();
// Set 'date' to epoch of Jan 1, 1970
cal.set(1970, 0, 1, hour, minute, second);
long timeAsMillis = 0;
try {
timeAsMillis = cal.getTimeInMillis();
} catch (IllegalAccessError iae) {
// Must be on JDK-1.3.1 or older....
timeAsMillis = cal.getTime().getTime();
}
return new Time(timeAsMillis);
}
|
static final Time fastTimeCreate(int hour,
int minute,
int second,
Calendar targetCalendar,
ExceptionInterceptor exceptionInterceptor) throws SQLException {
if (hour < 0 || hour > 23) {
throw SQLError.createSQLException("Illegal hour value '" + hour + "' for java.sql.Time type in value '"
+ timeFormattedString(hour, minute, second) + ".",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor);
}
if (minute < 0 || minute > 59) {
throw SQLError.createSQLException("Illegal minute value '" + minute + "'" + "' for java.sql.Time type in value '"
+ timeFormattedString(hour, minute, second) + ".",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor);
}
if (second < 0 || second > 59) {
throw SQLError.createSQLException("Illegal minute value '" + second + "'" + "' for java.sql.Time type in value '"
+ timeFormattedString(hour, minute, second) + ".",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, exceptionInterceptor);
}
Calendar cal = (targetCalendar == null) ? new GregorianCalendar() : targetCalendar;
cal.clear();
// Set 'date' to epoch of Jan 1, 1970
cal.set(1970, 0, 1, hour, minute, second);
long timeAsMillis = 0;
try {
timeAsMillis = cal.getTimeInMillis();
} catch (IllegalAccessError iae) {
// Must be on JDK-1.3.1 or older....
timeAsMillis = cal.getTime().getTime();
}
return new Time(timeAsMillis);
}
|
static final Timestamp fastTimestampCreate(TimeZone tz,
int year,
int month,
int day,
int hour,
int minute,
int seconds,
int secondsPart) {
Calendar cal = (tz == null) ? new GregorianCalendar() : new GregorianCalendar(tz);
cal.clear();
// why-oh-why is this different than java.util.date,
// in the year part, but it still keeps the silly '0'
// for the start month????
cal.set(year, month - 1, day, hour, minute, seconds);
long tsAsMillis = 0;
try {
tsAsMillis = cal.getTimeInMillis();
} catch (IllegalAccessError iae) {
// Must be on JDK-1.3.1 or older....
tsAsMillis = cal.getTime().getTime();
}
Timestamp ts = new Timestamp(tsAsMillis);
ts.setNanos(secondsPart);
return ts;
}
|
static final Timestamp fastTimestampCreate(boolean useGmtConversion,
Calendar gmtCalIfNeeded,
Calendar cal,
int year,
int month,
int day,
int hour,
int minute,
int seconds,
int secondsPart) {
cal.clear();
// why-oh-why is this different than java.util.date,
// in the year part, but it still keeps the silly '0'
// for the start month????
cal.set(year, month - 1, day, hour, minute, seconds);
int offsetDiff = 0;
if (useGmtConversion) {
int fromOffset = cal.get(Calendar.ZONE_OFFSET)
+ cal.get(Calendar.DST_OFFSET);
if (gmtCalIfNeeded == null) {
gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
}
gmtCalIfNeeded.clear();
gmtCalIfNeeded.setTimeInMillis(cal.getTimeInMillis());
int toOffset = gmtCalIfNeeded.get(Calendar.ZONE_OFFSET)
+ gmtCalIfNeeded.get(Calendar.DST_OFFSET);
offsetDiff = fromOffset - toOffset;
}
if (secondsPart != 0) {
cal.set(Calendar.MILLISECOND, secondsPart / 1000000);
}
long tsAsMillis = 0;
try {
tsAsMillis = cal.getTimeInMillis();
} catch (IllegalAccessError iae) {
// Must be on JDK-1.3.1 or older....
tsAsMillis = cal.getTime().getTime();
}
Timestamp ts = new Timestamp(tsAsMillis + offsetDiff);
ts.setNanos(secondsPart);
return ts;
}
|
public static String getCanoncialTimezone(String timezoneStr,
ExceptionInterceptor exceptionInterceptor) throws SQLException {
if (timezoneStr == null) {
return null;
}
timezoneStr = timezoneStr.trim();
// handle '+/-hh:mm' form ...
if (timezoneStr.length() > 2) {
if ((timezoneStr.charAt(0) == '+' || timezoneStr.charAt(0) == '-') &&
Character.isDigit(timezoneStr.charAt(1))) {
return "GMT" + timezoneStr;
}
}
// Fix windows Daylight/Standard shift JDK doesn't map these (doh)
int daylightIndex = StringUtils.indexOfIgnoreCase(timezoneStr,
"DAYLIGHT");
if (daylightIndex != -1) {
StringBuffer timezoneBuf = new StringBuffer();
timezoneBuf.append(timezoneStr.substring(0, daylightIndex));
timezoneBuf.append("Standard");
timezoneBuf.append(timezoneStr.substring(daylightIndex
+ "DAYLIGHT".length(), timezoneStr.length()));
timezoneStr = timezoneBuf.toString();
}
String canonicalTz = (String) TIMEZONE_MAPPINGS.get(timezoneStr);
// if we didn't find it, try abbreviated timezones
if (canonicalTz == null) {
String[] abbreviatedTimezone = (String[]) ABBREVIATED_TIMEZONES
.get(timezoneStr);
if (abbreviatedTimezone != null) {
// If there's only one mapping use that
if (abbreviatedTimezone.length == 1) {
canonicalTz = abbreviatedTimezone[0];
} else {
StringBuffer possibleTimezones = new StringBuffer(128);
possibleTimezones.append(abbreviatedTimezone[0]);
for (int i = 1; i < abbreviatedTimezone.length; i++) {
possibleTimezones.append(", ");
possibleTimezones.append(abbreviatedTimezone[i]);
}
throw SQLError.createSQLException(Messages.getString("TimeUtil.TooGenericTimezoneId",
new Object[] {timezoneStr, possibleTimezones}), SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, exceptionInterceptor);
}
}
}
return canonicalTz;
}
Returns the 'official' Java timezone name for the given timezone |