mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-23 08:34:11 -08:00
Added timezone support. Only LOCAL and GMT are currently supported, but you can extend the native functions in runtime to add support for other timezones.
This commit is contained in:
parent
c148965a35
commit
c9819f3a86
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package org.teavm.classlib.java.util;
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
import org.teavm.jso.JSBody;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TimeZone represents a time zone offset, and also figures out daylight savings.
|
* TimeZone represents a time zone offset, and also figures out daylight savings.
|
||||||
* Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.
|
* Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.
|
||||||
|
@ -62,10 +64,20 @@ public abstract class TTimeZone {
|
||||||
return new String[] {"GMT", i};
|
return new String[] {"GMT", i};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@JSBody(params={},
|
||||||
|
script="return $rt_getTimezoneId(name,year,month,day,timeOfDayMillis)"
|
||||||
|
)
|
||||||
private static native String getTimezoneId();
|
private static native String getTimezoneId();
|
||||||
|
|
||||||
|
@JSBody(params={"name","year","month","day","timeOfDayMillis"},
|
||||||
|
script="return $rt_getTimezoneOffset(name,year,month,day,timeOfDayMillis)")
|
||||||
private static native int getTimezoneOffset(String name, int year, int month, int day, int timeOfDayMillis);
|
private static native int getTimezoneOffset(String name, int year, int month, int day, int timeOfDayMillis);
|
||||||
|
|
||||||
|
@JSBody(params="name",
|
||||||
|
script="return $rt_getTimezoneRawOffset(name)")
|
||||||
private static native int getTimezoneRawOffset(String name);
|
private static native int getTimezoneRawOffset(String name);
|
||||||
|
|
||||||
|
@JSBody(params={"name","millis"}, script="return $rt_isTimezoneDST(name,millis)")
|
||||||
private static native boolean isTimezoneDST(String name, long millis);
|
private static native boolean isTimezoneDST(String name, long millis);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -529,7 +529,49 @@ function $rt_nativeThread() {
|
||||||
function $rt_invalidPointer() {
|
function $rt_invalidPointer() {
|
||||||
throw new Error("Invalid recorded state");
|
throw new Error("Invalid recorded state");
|
||||||
}
|
}
|
||||||
|
function $rt_getTimezoneId(){
|
||||||
|
return "LOCAL";
|
||||||
|
}
|
||||||
|
function $rt_getTimezoneOffset(name, year, month, day, timeOfDayMillis){
|
||||||
|
if ($rt_getTimezoneId()===name){
|
||||||
|
var hours = Math.floor(timeOfDayMillis/1000/60/60);
|
||||||
|
var minutes = Math.floor(timeOfDayMillis/1000/60)%60;
|
||||||
|
var seconds = Math.floor(timeOfDayMillis/1000)%60;
|
||||||
|
var millis = timeOfDayMillis % 1000;
|
||||||
|
return -(new Date(year, month, day, hours, minutes, seconds, millis).getTimezoneOffset()*1000*60);
|
||||||
|
} else if ("UTC"===name || "GMT"===name){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unsupported Timezone: "+name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function $rt_getTimezoneRawOffset(name){
|
||||||
|
if ($rt_getTimezoneId()===name){
|
||||||
|
var millis = new Date().getTime();
|
||||||
|
var i=0;
|
||||||
|
var addDays = 1000 * 60 *60 * 24 * 200; // Check 200 days later
|
||||||
|
while ($rt_isTimezoneDST(name, millis) && i++<4){
|
||||||
|
millis += addDays;
|
||||||
|
}
|
||||||
|
return -(new Date(millis).getTimezoneOffset()*1000*60);
|
||||||
|
} else if (name==='GMT' || name==='UTC'){
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unsupported Timezone: "+name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function $rt_isTimezoneDST(name, millis){
|
||||||
|
if ($rt_getTimezoneId()===name){
|
||||||
|
var jan = new Date(this.getFullYear(), 0, 1);
|
||||||
|
var jul = new Date(this.getFullYear(), 6, 1);
|
||||||
|
var maxOff = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
|
||||||
|
return new Date(millis).getTimezoneOffset()<maxOff;
|
||||||
|
} else if (name==='GMT' || name=='UTC'){
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unsupported Timezone: "+name);
|
||||||
|
}
|
||||||
|
}
|
||||||
function $dbg_repr(obj) {
|
function $dbg_repr(obj) {
|
||||||
return obj.toString ? obj.toString() : "";
|
return obj.toString ? obj.toString() : "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,13 +60,13 @@ public class DateFormatTest {
|
||||||
private Date getDateWithZoneOffset(long milliseconds) {
|
private Date getDateWithZoneOffset(long milliseconds) {
|
||||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
calendar.setTimeInMillis(milliseconds);
|
calendar.setTimeInMillis(milliseconds);
|
||||||
milliseconds -= calendar.get(Calendar.ZONE_OFFSET);
|
milliseconds -= calendar.get(Calendar.ZONE_OFFSET)-calendar.get(Calendar.DST_OFFSET);
|
||||||
return new Date(milliseconds);
|
return new Date(milliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getTimeWithoutZoneOffset(Date date) {
|
private long getTimeWithoutZoneOffset(Date date) {
|
||||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
calendar.setTime(date);
|
calendar.setTime(date);
|
||||||
return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET);
|
return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,12 +137,13 @@ public class SimpleDateFormatTest {
|
||||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
calendar.setTimeInMillis(milliseconds);
|
calendar.setTimeInMillis(milliseconds);
|
||||||
milliseconds -= calendar.get(Calendar.ZONE_OFFSET);
|
milliseconds -= calendar.get(Calendar.ZONE_OFFSET);
|
||||||
|
milliseconds -= calendar.get(Calendar.DST_OFFSET);
|
||||||
return new Date(milliseconds);
|
return new Date(milliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getTimeWithoutZoneOffset(Date date) {
|
private long getTimeWithoutZoneOffset(Date date) {
|
||||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
calendar.setTime(date);
|
calendar.setTime(date);
|
||||||
return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET);
|
return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 shannah.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author shannah
|
||||||
|
*/
|
||||||
|
public class TimeZoneTest {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user