Fixes several bugs in Calendar implementation

This commit is contained in:
konsoletyper 2014-05-25 17:03:41 +04:00
parent 403e4a226e
commit 13eb35e5a8
4 changed files with 74 additions and 9 deletions

View File

@ -556,13 +556,8 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
@Override @Override
public int hashCode() { public int hashCode() {
if (hashCode == 0) { if (hashCode == 0) {
hashCode ^= 734262231;
for (char c : characters) { for (char c : characters) {
hashCode = (hashCode << 4) | (hashCode >>> 28); hashCode = 31 * hashCode + c;
hashCode ^= 347236277 ^ c;
if (hashCode == 0) {
++hashCode;
}
} }
} }
return hashCode; return hashCode;

View File

@ -48,6 +48,7 @@ public class DateNativeGenerator implements Generator, DependencyPlugin {
case "getHours": case "getHours":
case "getMinutes": case "getMinutes":
case "getSeconds": case "getSeconds":
case "getTimezoneOffset":
generateGetMethod(context, writer, methodRef.getName()); generateGetMethod(context, writer, methodRef.getName());
break; break;
case "setFullYear": case "setFullYear":

View File

@ -166,8 +166,8 @@ public abstract class TCalendar implements TSerializable, TCloneable, TComparabl
String country = locale.getCountry(); String country = locale.getCountry();
if (country.isEmpty()) { if (country.isEmpty()) {
String subtags = CLDRHelper.getLikelySubtags(locale.getLanguage()); String subtags = CLDRHelper.getLikelySubtags(locale.getLanguage());
int index = subtags.lastIndexOf('-'); int index = subtags.lastIndexOf('_');
country = index > 0 ? subtags.substring(subtags.lastIndexOf('-') + 1) : ""; country = index > 0 ? subtags.substring(index + 1) : "";
} }
return country; return country;
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.samples; package org.teavm.samples;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Locale; import java.util.Locale;
@ -36,12 +37,15 @@ public class DateTime {
private static Window window = (Window)JS.getGlobal(); private static Window window = (Window)JS.getGlobal();
private static HTMLDocument document = window.getDocument(); private static HTMLDocument document = window.getDocument();
private static HTMLSelectElement localeElem = (HTMLSelectElement)document.getElementById("locale"); private static HTMLSelectElement localeElem = (HTMLSelectElement)document.getElementById("locale");
private static Date currentDate; private static HTMLSelectElement fieldElem = (HTMLSelectElement)document.getElementById("field");
private static Date currentDate = new Date();
private static Locale[] locales; private static Locale[] locales;
private static Locale currentLocale; private static Locale currentLocale;
private static int currentField;
public static void main(String[] args) { public static void main(String[] args) {
fillLocales(); fillLocales();
bindFieldEvent();
window.setInterval(new TimerHandler() { window.setInterval(new TimerHandler() {
@Override @Override
public void onTimer() { public void onTimer() {
@ -49,6 +53,7 @@ public class DateTime {
} }
}, 250); }, 250);
updateCurrentLocale(); updateCurrentLocale();
updateCurrentField();
} }
private static void fillLocales() { private static void fillLocales() {
@ -67,6 +72,14 @@ public class DateTime {
}); });
} }
private static void bindFieldEvent() {
fieldElem.addEventListener("change", new EventListener() {
@Override public void handleEvent(Event evt) {
updateCurrentField();
}
});
}
private static void updateCurrentLocale() { private static void updateCurrentLocale() {
currentLocale = locales[localeElem.getSelectedIndex()]; currentLocale = locales[localeElem.getSelectedIndex()];
GregorianCalendar calendar = new GregorianCalendar(currentLocale); GregorianCalendar calendar = new GregorianCalendar(currentLocale);
@ -83,10 +96,66 @@ public class DateTime {
private static void setCurrentTime(Date date) { private static void setCurrentTime(Date date) {
currentDate = date; currentDate = date;
updateCurrentTimeText(); updateCurrentTimeText();
updateFieldText();
} }
private static void updateCurrentTimeText() { private static void updateCurrentTimeText() {
HTMLInputElement timeElem = (HTMLInputElement)document.getElementById("current-time"); HTMLInputElement timeElem = (HTMLInputElement)document.getElementById("current-time");
timeElem.setValue(currentDate.toString()); timeElem.setValue(currentDate.toString());
} }
private static void updateCurrentField() {
switch (fieldElem.getValue()) {
case "era":
currentField = Calendar.ERA;
break;
case "year":
currentField = Calendar.YEAR;
break;
case "month":
currentField = Calendar.MONTH;
break;
case "week-of-year":
currentField = Calendar.WEEK_OF_YEAR;
break;
case "week-of-month":
currentField = Calendar.WEEK_OF_MONTH;
break;
case "date":
currentField = Calendar.DATE;
break;
case "day-of-year":
currentField = Calendar.DAY_OF_YEAR;
break;
case "day-of-week":
currentField = Calendar.DAY_OF_WEEK;
break;
case "am-pm":
currentField = Calendar.AM_PM;
break;
case "hour":
currentField = Calendar.HOUR;
break;
case "hour-of-day":
currentField = Calendar.HOUR_OF_DAY;
break;
case "minute":
currentField = Calendar.MINUTE;
break;
case "second":
currentField = Calendar.SECOND;
break;
case "zone-offset":
currentField = Calendar.ZONE_OFFSET;
break;
}
updateFieldText();
}
private static void updateFieldText() {
HTMLInputElement fieldValueElem = (HTMLInputElement)document.getElementById("field-value");
Calendar calendar = new GregorianCalendar(currentLocale);
calendar.setTime(currentDate);
fieldValueElem.setValue(String.valueOf(calendar.get(currentField)));
}
} }