Update date tests

pull/73/head
Alex Baker 11 years ago
parent a5802cb447
commit b35f996b95

@ -27,14 +27,6 @@ public class DateUtilities {
* ============================================================ long time
* ====================================================================== */
/** Convert date into unixtime */
public static long dateToUnixtime(Date date) {
if(date == null) {
return 0;
}
return date.getTime();
}
/**
* Add the specified amount of months to the given time.<br/>
* The day of month will stay the same.<br/>
@ -44,10 +36,14 @@ public class DateUtilities {
* @return the calculated time in milliseconds
*/
public static long addCalendarMonthsToUnixtime(long time, int interval) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.add(Calendar.MONTH, interval);
return c.getTimeInMillis();
DateTime dt = new DateTime(time);
DateTime result = dt.plusMonths(interval);
// preserving java.util.date behavior
int diff = dt.getDayOfMonth() - result.getDayOfMonth();
if(diff > 0) {
result = result.plusDays(diff);
}
return result.getMillis();
}
/** Returns unixtime for current time */
@ -57,14 +53,7 @@ public class DateUtilities {
/** Returns unixtime one month from now */
public static long oneMonthFromNow() {
final DateTime now = DateTime.now();
DateTime result = now.plusMonths(1);
// preserving java.util.date behavior
int diff = now.getDayOfMonth() - result.getDayOfMonth();
if(diff > 0) {
result = result.plusDays(diff);
}
return result.getMillis();
return addCalendarMonthsToUnixtime(currentTimeMillis(), 1);
}
/** Represents a single hour */
@ -125,7 +114,6 @@ public class DateUtilities {
String month = new SimpleDateFormat("MMM").format(date);
String value;
String standardDate;
// united states, you are special
Locale locale = Locale.getDefault();
if (arrayBinaryContains(locale.getLanguage(), "ja", "ko", "zh")
|| arrayBinaryContains(locale.getCountry(), "BZ", "CA", "KE", "MN" ,"US")) {

@ -24,7 +24,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static org.tasks.date.DateTimeUtils.newDate;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
/**
* A model that is synchronized to a remote server and has a remote id
@ -110,7 +110,7 @@ abstract public class RemoteModel extends AbstractModel {
tag_date = tagData.getValue(TagData.CREATION_DATE);
}
if (tag_date == 0) {
tag_date = DateUtilities.dateToUnixtime(newDate());
tag_date = currentTimeMillis();
}
return String.format("cached::%s%s", tagData.getValue(TagData.NAME), tag_date);
}

@ -8,15 +8,22 @@ package com.todoroo.andlib.utility;
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.tasks.Snippet;
import java.util.Date;
import java.util.Locale;
import static com.todoroo.andlib.utility.DateUtilities.addCalendarMonthsToUnixtime;
import static com.todoroo.andlib.utility.DateUtilities.clearTime;
import static com.todoroo.andlib.utility.DateUtilities.getDateString;
import static com.todoroo.andlib.utility.DateUtilities.getDateStringHideYear;
import static com.todoroo.andlib.utility.DateUtilities.getStartOfDay;
import static com.todoroo.andlib.utility.DateUtilities.getTimeString;
import static com.todoroo.andlib.utility.DateUtilities.getWeekday;
import static com.todoroo.andlib.utility.DateUtilities.getWeekdayShort;
import static com.todoroo.andlib.utility.DateUtilities.isEndOfMonth;
@ -30,8 +37,18 @@ import static org.tasks.date.DateTimeUtils.newDate;
@RunWith(RobolectricTestRunner.class)
public class DateUtilitiesTest extends TodorooRobolectricTestCase {
public void set24Hour(boolean is24) {
DateUtilities.is24HourOverride = is24;
private static Locale defaultLocale;
@Before
public void before() {
defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
}
@After
public void after() {
DateUtilities.is24HourOverride = null;
Locale.setDefault(defaultLocale);
}
@Test
@ -40,13 +57,13 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
public void run() {
Date d = newDate();
set24Hour(false);
DateUtilities.is24HourOverride = false;
for (int i = 0; i < 24; i++) {
d.setHours(i);
DateUtilities.getTimeString(getContext(), d);
}
set24Hour(true);
DateUtilities.is24HourOverride = true;
for (int i = 0; i < 24; i++) {
d.setHours(i);
DateUtilities.getTimeString(getContext(), d);
@ -63,12 +80,51 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
for (int i = 0; i < 12; i++) {
d.setMonth(i);
DateUtilities.getDateString(d);
getDateString(d);
}
}
});
}
@Test
public void get24HourTime() {
DateUtilities.is24HourOverride = true;
assertEquals("9:05", getTimeString(null, newDate(2014, 1, 4, 9, 5, 36)));
assertEquals("13:00", getTimeString(null, newDate(2014, 1, 4, 13, 0, 1)));
}
@Test
public void getTime() {
DateUtilities.is24HourOverride = false;
assertEquals("9:05 AM", getTimeString(null, newDate(2014, 1, 4, 9, 5, 36)));
assertEquals("1:05 PM", getTimeString(null, newDate(2014, 1, 4, 13, 5, 36)));
}
@Test
public void getTimeWithNoMinutes() {
DateUtilities.is24HourOverride = false;
assertEquals("1 PM", getTimeString(null, newDate(2014, 1, 4, 13, 0, 59))); // derp?
}
@Test
public void getDateStringWithYear() {
assertEquals("Jan 4, 2014", getDateString(newDate(2014, 1, 4, 0, 0, 0)));
}
@Test
public void getDateStringHidingYear() {
freezeAt(newDate(2014, 1, 1)).thawAfter(new Snippet() {{
assertEquals("Jan 1", getDateStringHideYear(newDate()));
}});
}
@Test
public void getDateStringWithDifferentYear() {
freezeAt(newDate(2013, 12, 31)).thawAfter(new Snippet() {{
assertEquals("Jan 1\n2014", getDateStringHideYear(newDate(2014, 1, 1)));
}});
}
@Test
public void oneMonthFromStartOfDecember() {
DateTime now = new DateTime(2013, 12, 1, 12, 19, 45, 192);
@ -170,7 +226,7 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
@Test
public void getWeekdayShortString() {
assertEquals("Sun", getWeekdayShort(new DateTime(2013, 12, 29, 11, 12, 13, 14).toDate()));
assertEquals("Sun", getWeekdayShort(newDate(2013, 12, 29)));
assertEquals("Mon", getWeekdayShort(newDate(2013, 12, 30)));
assertEquals("Tue", getWeekdayShort(newDate(2013, 12, 31)));
assertEquals("Wed", getWeekdayShort(newDate(2014, 1, 1)));
@ -178,4 +234,20 @@ public class DateUtilitiesTest extends TodorooRobolectricTestCase {
assertEquals("Fri", getWeekdayShort(newDate(2014, 1, 3)));
assertEquals("Sat", getWeekdayShort(newDate(2014, 1, 4)));
}
@Test
public void addMonthsToTimestamp() {
assertEquals(newDate(2014, 1, 1).getTime(), addCalendarMonthsToUnixtime(newDate(2013, 12, 1).getTime(), 1));
assertEquals(newDate(2014, 12, 31).getTime(), addCalendarMonthsToUnixtime(newDate(2013, 12, 31).getTime(), 12));
}
@Test
public void addMonthsWithLessDays() {
assertEquals(newDate(2014, 3, 3).getTime(), addCalendarMonthsToUnixtime(newDate(2013, 12, 31).getTime(), 2));
}
@Test
public void addMonthsWithMoreDays() {
assertEquals(newDate(2014, 1, 30).getTime(), addCalendarMonthsToUnixtime(newDate(2013, 11, 30).getTime(), 2));
}
}

@ -27,7 +27,7 @@ public class RelativeDayTest {
@Before
public void before() {
defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);
Locale.setDefault(Locale.US);
freezeAt(now);
}
@ -66,12 +66,12 @@ public class RelativeDayTest {
@Test
public void relativeDayMoreThanOneWeek() {
checkRelativeDay(now().minusDays(8), "23 Dec", "23 Dec");
checkRelativeDay(now().minusDays(8), "Dec 23", "Dec 23");
}
@Test
public void relativeDayNextYear() {
checkRelativeDay(now().plusDays(8), "8 Jan\n2014", "8 Jan\n2014");
checkRelativeDay(now().plusDays(8), "Jan 8\n2014", "Jan 8\n2014");
}
private void checkRelativeDay(DateTime now, String full, String abbreviated) {

@ -3,12 +3,12 @@ package org.tasks;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import static org.joda.time.DateTime.now;
import java.util.Date;
public class Freeze {
public static Freeze freezeClock() {
return freezeAt(now());
public static Freeze freezeAt(Date date) {
return freezeAt(date.getTime());
}
public static Freeze freezeAt(DateTime dateTime) {

Loading…
Cancel
Save