This commit is contained in:
Mariotaku Lee 2016-01-29 12:13:38 +08:00
parent c822772b86
commit 8aad854fd8
4 changed files with 46 additions and 2 deletions

View File

@ -47,7 +47,6 @@ public class TwitterDateConverter extends StringBasedTypeConverter<Date> {
private static final String[] MONTH_NAMES = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", private static final String[] MONTH_NAMES = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"}; "Aug", "Sep", "Oct", "Nov", "Dec"};
private static final long ONE_MINUTE = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC");
private static final Locale LOCALE = Locale.ENGLISH; private static final Locale LOCALE = Locale.ENGLISH;
private final DateFormat mDateFormat; private final DateFormat mDateFormat;
@ -99,7 +98,7 @@ public class TwitterDateConverter extends StringBasedTypeConverter<Date> {
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeSegs[0])); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeSegs[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(timeSegs[1])); calendar.set(Calendar.MINUTE, Integer.parseInt(timeSegs[1]));
calendar.set(Calendar.SECOND, Integer.parseInt(timeSegs[2])); calendar.set(Calendar.SECOND, Integer.parseInt(timeSegs[2]));
calendar.set(Calendar.ZONE_OFFSET, SimpleTimeZone.getTimeZone("GMT" + segs[4]).getRawOffset()); calendar.setTimeZone(SimpleTimeZone.getTimeZone(getTimezoneText(segs[4])));
final Date date = calendar.getTime(); final Date date = calendar.getTime();
if (!WEEK_NAMES[calendar.get(Calendar.DAY_OF_WEEK) - 1].equals(segs[0])) { if (!WEEK_NAMES[calendar.get(Calendar.DAY_OF_WEEK) - 1].equals(segs[0])) {
BugReporter.error("Week mismatch " + string + " => " + date); BugReporter.error("Week mismatch " + string + " => " + date);
@ -108,6 +107,11 @@ public class TwitterDateConverter extends StringBasedTypeConverter<Date> {
return date; return date;
} }
private String getTimezoneText(String seg) {
if (seg.startsWith("GMT") || seg.startsWith("UTC")) return seg;
return "GMT" + seg;
}
@Override @Override
public String convertToString(Date date) { public String convertToString(Date date) {
final Calendar calendar = Calendar.getInstance(); final Calendar calendar = Calendar.getInstance();

View File

@ -29,6 +29,7 @@ android {
buildConfigField 'boolean', 'ENABLE_MEDIA_VIEWER', 'Boolean.parseBoolean("true")' buildConfigField 'boolean', 'ENABLE_MEDIA_VIEWER', 'Boolean.parseBoolean("true")'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7 sourceCompatibility JavaVersion.VERSION_1_7
@ -131,6 +132,10 @@ dependencies {
provided 'javax.annotation:jsr250-api:1.0' provided 'javax.annotation:jsr250-api:1.0'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
} }
task svgToDrawable(type: SvgDrawableTask) { task svgToDrawable(type: SvgDrawableTask) {

View File

@ -0,0 +1,35 @@
package org.mariotaku.twidere.api.twitter.util;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
/**
* Created by mariotaku on 16/1/29.
*/
public class TwitterDateConverterTest {
private final TwitterDateConverter converter = new TwitterDateConverter();
private final SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy", Locale.ENGLISH);
@Test
public void testGetFromString() throws Exception {
testDate("Fri Jan 29 04:12:49 +0100 2016");
testDate("Thu Jan 28 11:08:47 +0000 2016");
testDate("Sat Oct 03 16:05:32 +0000 2015");
testDate("Tue Jan 26 18:30:19 +0100 2016");
}
private void testDate(String s) throws ParseException {
assertEquals(converter.getFromString(s), format.parse(s));
}
@Test
public void testConvertToString() throws Exception {
}
}