You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ke...@apache.org on 2011/04/10 20:03:03 UTC
svn commit: r1090859 - in /incubator/isis/trunk:
applib/src/main/java/org/apache/isis/applib/
applib/src/main/java/org/apache/isis/applib/clock/
applib/src/main/java/org/apache/isis/applib/fixtures/
applib/src/main/java/org/apache/isis/applib/value/ ap...
Author: kevin
Date: Sun Apr 10 18:03:02 2011
New Revision: 1090859
URL: http://svn.apache.org/viewvc?rev=1090859&view=rev
Log:
Converted applib.DateTime from java.util.Date to jodatime.DateTime.
Added:
incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/Defaults.java (with props)
Modified:
incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/clock/Clock.java
incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/fixtures/FixtureClock.java
incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Date.java
incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/DateTime.java
incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Time.java
incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/DateTimeTest.java
incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/TimeTest.java
incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateTimeValueSemanticsProviderTest.java
incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/TimeValueSemanticsProviderTest.java
Added: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/Defaults.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/Defaults.java?rev=1090859&view=auto
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/Defaults.java (added)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/Defaults.java Sun Apr 10 18:03:02 2011
@@ -0,0 +1,18 @@
+package org.apache.isis.applib;
+
+import org.joda.time.DateTimeZone;
+
+public class Defaults {
+ // {{ ApplibTimeTime
+ static DateTimeZone applibTimeZone = DateTimeZone.UTC;
+
+ public static DateTimeZone getApplibTimeZone() {
+ return applibTimeZone;
+ }
+
+ public static void setApplibTimeZone(final DateTimeZone applibTimeZone) {
+ Defaults.applibTimeZone = applibTimeZone;
+ }
+ // }}
+
+}
Propchange: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/Defaults.java
------------------------------------------------------------------------------
svn:executable = *
Modified: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/clock/Clock.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/clock/Clock.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/clock/Clock.java (original)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/clock/Clock.java Sun Apr 10 18:03:02 2011
@@ -22,7 +22,10 @@ package org.apache.isis.applib.clock;
import java.util.Calendar;
import java.util.Date;
+import org.joda.time.DateTime;
+
import org.apache.isis.applib.ApplicationException;
+import org.apache.isis.applib.Defaults;
import org.apache.isis.applib.fixtures.FixtureClock;
/**
@@ -77,20 +80,28 @@ public abstract class Clock {
/**
* Convenience method returning the current {@link #getTime() time} according to this Clock as a mutable
- * {@link Calendar}.
+ * {@link Calendar}. Consider replacing with {@link #getTimeAsDateTime()
+
*/
+ @Deprecated
public static Calendar getTimeAsCalendar() {
return getInstance().timeAsCalendar();
}
/**
* Convenience method returning the current {@link #getTime() time} according to this Clock as a (nominally
- * im)mutable {@link Date}.
+ * im)mutable {@link Date}. You should now use {@link #getTimeAsDateTime()}
+ *
*/
+ @Deprecated
public static Date getTimeAsDate() {
return new Date(getTime());
}
+ public static DateTime getTimeAsDateTime() {
+ return new DateTime(getTime(), Defaults.getApplibTimeZone());
+ }
+
private static void ensureReplaceable() {
if (!isReplaceable && instance != null) {
throw new ApplicationException("Clock already set up");
@@ -137,4 +148,5 @@ final class SystemClock extends Clock {
protected long time() {
return System.currentTimeMillis();
}
+
}
Modified: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/fixtures/FixtureClock.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/fixtures/FixtureClock.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/fixtures/FixtureClock.java (original)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/fixtures/FixtureClock.java Sun Apr 10 18:03:02 2011
@@ -200,6 +200,7 @@ public class FixtureClock extends Clock
@Override
public String toString() {
- return (calendar == null ? "System" : "Explicitly set") + ": " + Clock.getTimeAsCalendar().getTime().toString();
+ return (calendar == null ? "System" : "Explicitly set") + ": " + Clock.getTimeAsDateTime().toString();
}
+
}
Modified: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Date.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Date.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Date.java (original)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Date.java Sun Apr 10 18:03:02 2011
@@ -23,10 +23,10 @@ import java.util.Calendar;
import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
-import org.joda.time.DateTimeZone;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
+import org.apache.isis.applib.Defaults;
import org.apache.isis.applib.annotation.Value;
import org.apache.isis.applib.clock.Clock;
@@ -56,7 +56,7 @@ public class Date extends Magnitude<Date
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
- date = new DateTime(time.getTime(), DateTimeZone.UTC);
+ date = new DateTime(time.getTime(), Defaults.getApplibTimeZone());
}
/**
@@ -67,12 +67,11 @@ public class Date extends Magnitude<Date
date = newDateTime(year, month, day);
}
-
/**
* Create a Date object based on the specified Java date object. The time portion of the Java date is disposed of.
*/
public Date(final java.util.Date date) {
- this.date = new DateTime(date.getTime(), DateTimeZone.UTC);
+ this.date = new DateTime(date.getTime(), Defaults.getApplibTimeZone());
}
public Date(final long millisSinceEpoch) {
@@ -84,7 +83,7 @@ public class Date extends Magnitude<Date
}
private DateTime newDateTime(int year, int month, int day) {
- return new DateTime(year, month, day, 0, 0, 0, 0, DateTimeZone.UTC);
+ return new DateTime(year, month, day, 0, 0, 0, 0, Defaults.getApplibTimeZone());
}
protected Date createDate(final DateTime date) {
@@ -106,7 +105,8 @@ public class Date extends Magnitude<Date
throw new IllegalArgumentException("Month must be in the range 1 - 12 inclusive");
}
final DateTime newDate = newDateTime(year, month, 1);
- final int lastDayOfMonth = newDate.dayOfMonth().getMaximumValue();;
+ final int lastDayOfMonth = newDate.dayOfMonth().getMaximumValue();
+ ;
if ((day < 1) || (day > lastDayOfMonth)) {
throw new IllegalArgumentException("Day must be in the range 1 - " + lastDayOfMonth + " inclusive: " + day);
}
@@ -121,11 +121,12 @@ public class Date extends Magnitude<Date
java.util.Date javaDate = date.toDate();
return javaDate;
}
+
/**
*
* @return the milliseconds from 1970-01-01T00:00:00Z
*/
- public long getMillisSinceEpoch(){
+ public long getMillisSinceEpoch() {
return date.getMillis();
}
@@ -172,7 +173,7 @@ public class Date extends Magnitude<Date
* @author Joshua Cassidy
*/
public int getDayOfWeek() {
- return date.getDayOfWeek()-1; // Mon - Sun == 1 - 7
+ return date.getDayOfWeek() - 1; // Mon - Sun == 1 - 7
}
/**
@@ -206,7 +207,7 @@ public class Date extends Magnitude<Date
}
private boolean sameAs(final Date as, final DateTimeFieldType field) {
-
+
return date.get(field) == as.date.get(field);
}
Modified: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/DateTime.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/DateTime.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/DateTime.java (original)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/DateTime.java Sun Apr 10 18:03:02 2011
@@ -21,51 +21,84 @@ package org.apache.isis.applib.value;
import java.util.Calendar;
import java.util.Date;
+import java.util.TimeZone;
+import org.joda.time.DateTimeZone;
+import org.joda.time.Period;
+
+import org.apache.isis.applib.Defaults;
import org.apache.isis.applib.annotation.Value;
import org.apache.isis.applib.clock.Clock;
/**
- * Value object representing a date and time value.
+ * Value object representing a date and time value. By default, the time is initialised to the current time, unless
+ * otherwise specified.
*/
@Value(semanticsProviderName = "org.apache.isis.core.progmodel.facets.value.datetime.DateTimeValueSemanticsProvider")
public class DateTime extends Magnitude<DateTime> {
private static final long serialVersionUID = 1L;
- private final Date date;
+ private final org.joda.time.DateTime dateTime;
/**
* Create a Time object for storing a timeStamp set to the current time.
*/
public DateTime() {
- final Calendar cal = Calendar.getInstance();
- final java.util.Date d = new java.util.Date(Clock.getTime());
- cal.setTime(d);
- cal.set(Calendar.MILLISECOND, 0);
- date = cal.getTime();
+ org.joda.time.DateTime d = new org.joda.time.DateTime(Clock.getTime(), Defaults.getApplibTimeZone());
+ // dateTime = d.secondOfMinute().setCopy(0);
+ dateTime = d;
}
+ /**
+ * Create a DateTime from the provided java.util.Date, assuming that the date is in UTC. If not, see {@link
+ * DateTime#}
+ *
+ * @param date
+ */
public DateTime(final Date date) {
- this.date = date;
+ this.dateTime = new org.joda.time.DateTime(date, Defaults.getApplibTimeZone());
+ }
+
+ public DateTime(final long millisSinceEpoch) {
+ this.dateTime = new org.joda.time.DateTime(millisSinceEpoch, Defaults.getApplibTimeZone());
+ }
+
+ public DateTime(final Date date, TimeZone timeZone) {
+ DateTimeZone tz = DateTimeZone.forTimeZone(timeZone);
+ this.dateTime = new org.joda.time.DateTime(date, tz);
+ }
+
+ public DateTime(final org.joda.time.DateTime dateTime) {
+ this.dateTime = new org.joda.time.DateTime(dateTime);
}
+ /**
+ * Creates a DateTime on the specified day/month/year, with the current time
+ */
public DateTime(final int year, final int month, final int day) {
- this(year, month, day, 0, 0);
+ Time time = new Time();
+ // this(year, month, day, time.getHour(), time.getMinute(), 0);
+
+ // replace below with something more like the above..
+ checkTime(year, month, day, 0, 0);
+ dateTime =
+ new org.joda.time.DateTime(year, month, day, time.getHour(), time.getMinute(), time.getSecond(), 0,
+ Defaults.getApplibTimeZone());
}
/**
- * Create a Date object set to the specified day, month and year.
+ * Create a Date object set to the specified day, month, year, hour, minute.
*/
public DateTime(final int year, final int month, final int day, final int hour, final int minute) {
this(year, month, day, hour, minute, 0);
}
+ /**
+ * Create a Date object set to the specified day, month, year, hour, minute, and second.
+ */
public DateTime(final int year, final int month, final int day, final int hour, final int minute, final int second) {
checkTime(year, month, day, hour, minute);
- final Calendar cal = Calendar.getInstance();
- cal.set(year, month - 1, day, hour, minute, second);
- cal.set(Calendar.MILLISECOND, 0);
- date = cal.getTime();
+ dateTime = new org.joda.time.DateTime(year, month, day, hour, minute, second, 0, Defaults.getApplibTimeZone());
}
private void checkTime(final int year, final int month, final int day, final int hour, final int minute) {
@@ -73,11 +106,8 @@ public class DateTime extends Magnitude<
throw new IllegalArgumentException("Month must be in the range 1 - 12 inclusive " + month);
}
- final Calendar cal = Calendar.getInstance();
-
- cal.set(year, month - 1, 0);
-
- final int lastDayOfMonth = cal.getMaximum(Calendar.DAY_OF_MONTH);
+ org.joda.time.DateTime dateTime = new org.joda.time.DateTime(year, month, 1, 0, 0, 0, 0);
+ final int lastDayOfMonth = dateTime.dayOfMonth().getMaximumValue();
if ((day < 1) || (day > lastDayOfMonth)) {
throw new IllegalArgumentException("Day must be in the range 1 - " + lastDayOfMonth + " inclusive " + day);
@@ -93,61 +123,88 @@ public class DateTime extends Magnitude<
}
/**
- * Add the specified days, years and months to this date value.
+ * Add the specified time period to this date value.
*/
public DateTime add(final int years, final int months, final int days, final int hours, final int minutes) {
- final Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MINUTE, minutes);
- cal.add(Calendar.HOUR_OF_DAY, hours);
- cal.add(Calendar.DAY_OF_MONTH, days);
- cal.add(Calendar.MONTH, months);
- cal.add(Calendar.YEAR, years);
- return createDateTime(cal.getTime());
+ Period period = new Period(years, months, 0, days, hours, minutes, 0, 0);
+ org.joda.time.DateTime dateTime = this.dateTime.plus(period);
+ return new DateTime(dateTime);
+ }
+
+ /**
+ * Add the specified days, years and months to this date value.
+ */
+ public DateTime add(int years, int months, int days) {
+ return add(years, months, days, 0, 0);
}
public Calendar calendarValue() {
- final Calendar c = Calendar.getInstance();
- c.setTime(date);
- return c;
+ return dateTime.toGregorianCalendar();
}
protected DateTime createDateTime(final Date date) {
return new DateTime(date);
}
+ /**
+ * Be careful - the TimeZone of the java.util.Date is based on the system default.
+ */
public java.util.Date dateValue() {
- return new Date(date.getTime());
+ return new Date(dateTime.getMillis());
}
- public int getDay() {
- final Calendar c = Calendar.getInstance();
- c.setTime(date);
- return c.get(Calendar.DAY_OF_MONTH);
+ public int getSeconds() {
+ return dateTime.getSecondOfMinute();
+ }
+
+ public int getMinute() {
+ return dateTime.getMinuteOfHour();
}
public int getHour() {
- final Calendar c = Calendar.getInstance();
- c.setTime(date);
- return c.get(Calendar.HOUR_OF_DAY);
+ return dateTime.getHourOfDay();
}
- public int getMinute() {
- final Calendar c = Calendar.getInstance();
- c.setTime(date);
- return c.get(Calendar.MINUTE);
+ public int getDay() {
+ return dateTime.getDayOfMonth();
}
public int getMonth() {
- final Calendar c = Calendar.getInstance();
- c.setTime(date);
- return c.get(Calendar.MONTH) + 1;
+ return dateTime.getMonthOfYear();
}
public int getYear() {
- final Calendar c = Calendar.getInstance();
- c.setTime(date);
- return c.get(Calendar.YEAR);
+ return dateTime.getYear();
+ }
+
+ /**
+ * A DateTime that is at the start of the current week. Time is preserved.
+ */
+ public DateTime startOfWeek() {
+ return new DateTime(dateTime.withDayOfWeek(1));
+ }
+
+ /**
+ * A DateTime that represents the start of the current month. Time is preserved.
+ */
+ public DateTime startOfMonth() {
+ return new DateTime(dateTime.withDayOfMonth(1));
+ }
+
+ /**
+ * This DateTime, but on the first day of the year. Time is preserved.
+ */
+ public DateTime startOfYear() {
+ return new DateTime(dateTime.withDayOfYear(1));
+ }
+
+ /**
+ * Day of year (1 to 365 [366 in leap years]) for Gregorian calendar.
+ *
+ * @return
+ */
+ public int getDayOfYear() {
+ return dateTime.getDayOfYear();
}
/**
@@ -155,7 +212,7 @@ public class DateTime extends Magnitude<
*/
@Override
public boolean isEqualTo(final DateTime timeStamp) {
- return this.date.equals((timeStamp).date);
+ return this.dateTime.equals((timeStamp).dateTime);
}
/**
@@ -163,11 +220,50 @@ public class DateTime extends Magnitude<
*/
@Override
public boolean isLessThan(final DateTime timeStamp) {
- return date.before((timeStamp).date);
+ return dateTime.isBefore((timeStamp).dateTime);
+ }
+
+ public boolean isSameDayAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : getDayOfYear() == dateTime2.getDayOfYear();
+ }
+
+ public boolean sameDayOfWeekAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : dateTime.getDayOfWeek() == dateTime2.dateTime.getDayOfWeek();
}
+ public boolean sameDayOfMonthAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : dateTime.getDayOfMonth() == dateTime2.dateTime.getDayOfMonth();
+ }
+
+ public boolean sameDayOfYearAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : dateTime.getDayOfYear() == dateTime2.dateTime.getDayOfYear();
+ }
+
+ public boolean sameWeekAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : dateTime.getWeekOfWeekyear() == dateTime2.dateTime.getWeekOfWeekyear();
+ }
+
+ public boolean sameMonthAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : getMonth() == dateTime2.getMonth();
+ }
+
+ public boolean sameYearAs(DateTime dateTime2) {
+ return dateTime2 == null ? false : getYear() == dateTime2.getYear();
+ }
+
+ @Deprecated
+ /**
+ * See millisSinceEpoch()
+ */
public long longValue() {
- return date.getTime();
+ return millisSinceEpoch();
+ }
+
+ /**
+ * Gets the milliseconds since the Java epoch of 1970-01-01T00:00:00Z
+ */
+ public long millisSinceEpoch() {
+ return dateTime.getMillis();
}
@Override
@@ -182,11 +278,11 @@ public class DateTime extends Magnitude<
return false;
}
final DateTime other = (DateTime) obj;
- if (date == null) {
- if (other.date != null) {
+ if (dateTime == null) {
+ if (other.dateTime != null) {
return false;
}
- } else if (!date.equals(other.date)) {
+ } else if (!dateTime.equals(other.dateTime)) {
return false;
}
return true;
@@ -196,12 +292,14 @@ public class DateTime extends Magnitude<
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((date == null) ? 0 : date.hashCode());
+ result = prime * result + ((dateTime == null) ? 0 : dateTime.hashCode());
return result;
}
@Override
public String toString() {
- return getYear() + "-" + getMonth() + "-" + getDay() + " " + getHour() + ":" + getMinute();
+ // return getYear() + "-" + getMonth() + "-" + getDay() + " " + getHour() + ":" + getMinute();
+ return String.format("%04d-%02d-%02d %02d:%02d", getYear(), getMonth(), getDay(), getHour(), getMinute());
}
+
}
Modified: incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Time.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Time.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Time.java (original)
+++ incubator/isis/trunk/applib/src/main/java/org/apache/isis/applib/value/Time.java Sun Apr 10 18:03:02 2011
@@ -19,13 +19,12 @@
package org.apache.isis.applib.value;
-import java.util.Calendar;
-
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
+import org.apache.isis.applib.Defaults;
import org.apache.isis.applib.annotation.Value;
import org.apache.isis.applib.clock.Clock;
@@ -50,7 +49,6 @@ public class Time extends Magnitude<Time
public static final int MINUTE = 60;
public static final int HOUR = 60 * MINUTE;
public static final int DAY = 24 * HOUR;
-
private final DateTime time;
@@ -58,26 +56,28 @@ public class Time extends Magnitude<Time
* Create a Time object set to the current time.
*/
public Time() {
- Calendar calendar = Clock.getTimeAsCalendar();
- final int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
- final int minuteOfHour = calendar.get(Calendar.MINUTE);
- time = newDateTime(hourOfDay, minuteOfHour);
+ DateTime dateTime = Clock.getTimeAsDateTime();
+ time = dateTime.withDate(1971, 1, 1);
}
- private DateTime newDateTime(int hourOfDay, int minuteOfHour) {
- return new DateTime(1971, 1, 1, hourOfDay, minuteOfHour, 0, 0, DateTimeZone.UTC);
+ private DateTime newDateTime(final int hourOfDay, final int minuteOfHour, final int secondsOfMinute) {
+ return new DateTime(1971, 1, 1, hourOfDay, minuteOfHour, secondsOfMinute, 0, Defaults.getApplibTimeZone());
}
/**
* Create a Time object for storing a time with the time set to the specified hours and minutes.
*/
public Time(final int hour, final int minute) {
- time = time(hour, minute);
+ this(hour, minute, 0);
+ }
+
+ public Time(int hour, int minute, int second) {
+ time = time(hour, minute, second);
}
- private DateTime time(final int hour, final int minute) {
- checkTime(hour, minute, 0);
- return newDateTime(hour, minute);
+ private DateTime time(final int hour, final int minute, final int seconds) {
+ checkTime(hour, minute, seconds);
+ return newDateTime(hour, minute, seconds);
}
/**
@@ -85,7 +85,7 @@ public class Time extends Magnitude<Time
*/
public Time(final java.sql.Date date) {
- this.time = new DateTime(date.getTime(), DateTimeZone.UTC);
+ this.time = new DateTime(date.getTime(), Defaults.getApplibTimeZone());
}
/**
@@ -100,22 +100,20 @@ public class Time extends Magnitude<Time
this.time = DateTime.secondOfMinute().setCopy(0);
}
-
/**
* Create a Time object for storing a time with the time set to the specified time of the Joda Time DateTime object.
*/
public Time(final DateTime dateTime) {
- this.time = newDateTime(dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
+ this.time = newDateTime(dateTime.getHourOfDay(), dateTime.getMinuteOfHour(), dateTime.getSecondOfMinute());
}
/**
* Create a new Time object from the millisSinceEpoch, using UTC.
*/
public Time(long millisSinceEpoch) {
- this.time = new DateTime(millisSinceEpoch, DateTimeZone.UTC);
+ this.time = new DateTime(millisSinceEpoch, Defaults.getApplibTimeZone());
}
-
/**
* Add the specified hours and minutes to this time value, returned as a new Time object.
*/
@@ -150,6 +148,10 @@ public class Time extends Magnitude<Time
return time.getMinuteOfHour();
}
+ public int getSecond() {
+ return time.getSecondOfMinute();
+ }
+
/**
* returns true if the time of this object has the same value as the specified time
*/
@@ -178,7 +180,7 @@ public class Time extends Magnitude<Time
* The number of seconds since midnight.
*/
public long secondsSinceMidnight() {
- return time.getMillisOfDay() / 1000;
+ return milliSecondsSinceMidnight() / 1000;
}
public long milliSecondsSinceMidnight() {
@@ -187,14 +189,12 @@ public class Time extends Magnitude<Time
public String titleString() {
return (time == null) ? "" : DateTimeFormat.shortTime().print(time);
- // final DateFormat timeInstance = DateFormat.getTimeInstance(DateFormat.SHORT);
- // return (date == null) ? "" : timeInstance.format(date);
}
@Override
public String toString() {
- return getHour() + ":" + getMinute();
-
+ // return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
+ return String.format("%02d:%02d", getHour(), getMinute());
}
public boolean sameHourAs(final Time time) {
@@ -249,5 +249,4 @@ public class Time extends Magnitude<Time
return time1;
}
-
}
Modified: incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/DateTimeTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/DateTimeTest.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/DateTimeTest.java (original)
+++ incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/DateTimeTest.java Sun Apr 10 18:03:02 2011
@@ -25,18 +25,23 @@ import static org.junit.Assert.assertTru
import org.junit.After;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
// TODO review all date based classes - should we use factory and service to create and work with date type values?
public class DateTimeTest {
private DateTime actual;
+ private int year, month, day, hour, minute;
@Before
public void setUp() throws Exception {
TestClock.initialize();
- actual = new DateTime(2000, 3, 14, 10, 45);
+ year = 2000;
+ month = 3;
+ day = 14;
+ hour = 10;
+ minute = 45;
+ actual = new DateTime(year, month, day, hour, minute);
}
@After
@@ -66,106 +71,96 @@ public class DateTimeTest {
assertEquals("minute", 59, dt.getMinute());
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testAdd() {
- // DateTime resultDateTime = actual.add(1, 2, 3);
- // assertEquals(17, resultDateTime.getDay());
- // assertEquals(5, resultDateTime.getMonth());
- // assertEquals(2001, resultDateTime.getYear());
+ DateTime resultDateTime = actual.add(1, 2, 3);
+ assertEquals(17, resultDateTime.getDay());
+ assertEquals(5, resultDateTime.getMonth());
+ assertEquals(2001, resultDateTime.getYear());
}
- @Ignore("TODO: may not work")
@Test
public void testEquals() throws Exception {
assertTrue(actual.equals(actual));
- assertTrue(new DateTime(2003, 8, 17).equals(new DateTime()));
- assertTrue(actual.equals(new DateTime(2000, 3, 14, 10, 45)));
+ assertEquals(actual, new DateTime(2000, 3, 14, 10, 45));
+ assertTrue(new DateTime(2003, 8, 17).isSameDayAs(new DateTime()));
}
- @Ignore("TODO: may not work")
@Test
public void testIsLestThan() throws Exception {
assertFalse(new DateTime(2003, 8, 17).isLessThan(new DateTime(2003, 8, 17)));
assertTrue(new DateTime(2003, 8, 16).isLessThan(new DateTime(2003, 8, 17)));
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testSameDayOfWeekAs() throws Exception {
- // assertTrue(new DateTime(2000, 2, 17).sameDayOfWeekAs(new DateTime(2003, 8, 7))); // Thursday
- // assertFalse(new DateTime(2000, 2, 15).sameDayOfWeekAs(new DateTime(2003, 8, 17))); // Tues &
+ assertTrue(new DateTime(2000, 2, 17).sameDayOfWeekAs(new DateTime(2003, 8, 7))); // Thursday
+ assertFalse(new DateTime(2000, 2, 15).sameDayOfWeekAs(new DateTime(2003, 8, 17))); // Tues &
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testSameDayOfMonthAs() throws Exception {
- // assertTrue(new DateTime(2000, 2, 17).sameDayOfMonthAs(new DateTime(2003, 8, 17)));
- // assertFalse(new DateTime(2000, 2, 15).sameDayOfMonthAs(new DateTime(2003, 8, 17)));
+ assertTrue(new DateTime(2000, 2, 17).sameDayOfMonthAs(new DateTime(2003, 8, 17)));
+ assertFalse(new DateTime(2000, 2, 15).sameDayOfMonthAs(new DateTime(2003, 8, 17)));
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testSameDayOfYearAs() throws Exception {
- // assertTrue(new DateTime(2001, 8, 17).sameDayOfYearAs(new DateTime(2003, 8, 17)));
- // assertTrue(new DateTime(1999, 3, 1).sameDayOfYearAs(new DateTime(2000, 2, 29))); // leap year
- // assertFalse(new DateTime(2001, 3, 1).sameDayOfYearAs(new DateTime(2000, 3, 2)));
+ assertTrue(new DateTime(2001, 8, 17).sameDayOfYearAs(new DateTime(2003, 8, 17)));
+ assertTrue(new DateTime(1999, 3, 1).sameDayOfYearAs(new DateTime(2000, 2, 29))); // leap year
+ assertFalse(new DateTime(2001, 3, 1).sameDayOfYearAs(new DateTime(2000, 3, 2)));
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testSameWeekAs() throws Exception {
- // assertFalse(new DateTime(2000, 2, 15).sameWeekAs(new DateTime(2000, 2, 12))); // Tue, week 7 and Sat,
- // // week 6
- // assertTrue(new DateTime(2001, 2, 16).sameWeekAs(new DateTime(2002, 2, 11))); // Tue, week 7, and Thu,
- // // week 7
+ assertFalse(new DateTime(2000, 2, 15).sameWeekAs(new DateTime(2000, 2, 12))); // Tue, week 7 and Sat,
+ // week 6
+ assertTrue(new DateTime(2001, 2, 16).sameWeekAs(new DateTime(2002, 2, 11))); // Tue, week 7, and Thu,
+ // week 7
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testSameMonthAs() throws Exception {
- // assertTrue(new DateTime(2000, 8, 15).sameMonthAs(new DateTime(2003, 8, 17)));
- // assertFalse(new DateTime(2003, 2, 17).sameMonthAs(new DateTime(2003, 8, 17)));
+ assertTrue(new DateTime(2000, 8, 15).sameMonthAs(new DateTime(2003, 8, 17)));
+ assertFalse(new DateTime(2003, 2, 17).sameMonthAs(new DateTime(2003, 8, 17)));
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testSameYearAs() throws Exception {
- // assertTrue(new DateTime(2003, 2, 15).sameYearAs(new DateTime(2003, 8, 17)));
- // assertFalse(new DateTime(2000, 2, 15).sameYearAs(new DateTime(2003, 8, 17)));
+ assertTrue(new DateTime(2003, 2, 15).sameYearAs(new DateTime(2003, 8, 17)));
+ assertFalse(new DateTime(2000, 2, 15).sameYearAs(new DateTime(2003, 8, 17)));
}
- @Ignore("TODO: may not work")
@Test
public void testDateTimeValue() {
- final DateTime date = new DateTime(1970, 1, 1);
+ final DateTime date = new DateTime(1970, 1, 1, 0, 0, 0);
assertEquals(1970, date.getYear());
assertEquals(1, date.getMonth());
assertEquals(1, date.getDay());
- final long time = date.dateValue().getTime();
+ final long time = date.millisSinceEpoch();
assertEquals(1000 * 60 * 60 * 24 * 0, time);
+
+ final long jtime = date.dateValue().getTime();
+ assertEquals(1000 * 60 * 60 * 24 * 0, jtime);
+
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testStartOfYear() {
- // assertEquals(new DateTime(2000, 1, 1), actual.startOfYear());
+ assertEquals(new DateTime(2000, 1, 1, hour, minute), actual.startOfYear());
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testStartOfMonth() {
- // assertEquals(new DateTime(2000, 3, 1), actual.startOfMonth());
+ assertEquals(new DateTime(2000, 3, 1, hour, minute), actual.startOfMonth());
}
- @Ignore("TODO: need to reinstate functionality")
@Test
public void testStartOfWeek() {
- // assertEquals(new DateTime(2000, 3, 13), actual.startOfWeek());
- // assertEquals(new DateTime(2000, 2, 28), new DateTime(2000, 3, 2).startOfWeek());
+ assertEquals(new DateTime(2000, 3, 13, hour, minute), actual.startOfWeek());
+ assertEquals(new DateTime(2000, 2, 28), new DateTime(2000, 3, 2).startOfWeek());
}
- @Ignore("TODO: may not work")
@Test
public void testNewWithTodaysDateTime() {
final DateTime actual = new DateTime();
@@ -173,10 +168,9 @@ public class DateTimeTest {
assertEquals(expected, actual);
}
- @Ignore("TODO: may not work")
@Test
public void testToString() {
- assertEquals("2000-3-14", actual.toString());
+ assertEquals("2000-03-14 10:45", actual.toString());
}
}
Modified: incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/TimeTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/TimeTest.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/TimeTest.java (original)
+++ incubator/isis/trunk/applib/src/test/java/org/apache/isis/applib/value/TimeTest.java Sun Apr 10 18:03:02 2011
@@ -92,7 +92,7 @@ public class TimeTest {
@Test
public void testNewWithCurrentTime() {
- final Time expected = new Time(21, 30);
+ final Time expected = new Time(21, 30, 25);
final Time actual = new Time(); // Clock actually has 21:30:25
assertEquals(expected, actual);
}
Modified: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateTimeValueSemanticsProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateTimeValueSemanticsProviderTest.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateTimeValueSemanticsProviderTest.java (original)
+++ incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/DateTimeValueSemanticsProviderTest.java Sun Apr 10 18:03:02 2011
@@ -17,7 +17,6 @@
* under the License.
*/
-
package org.apache.isis.core.progmodel.facets.value;
import static org.junit.Assert.assertEquals;
@@ -42,10 +41,12 @@ public class DateTimeValueSemanticsProvi
@Before
public void setUpObjects() throws Exception {
- mockery.checking(new Expectations(){{
- allowing(mockConfiguration).getString("isis.value.format.datetime");
- will(returnValue(null));
- }});
+ mockery.checking(new Expectations() {
+ {
+ allowing(mockConfiguration).getString("isis.value.format.datetime");
+ will(returnValue(null));
+ }
+ });
TestClock.initialize();
holder = new FacetHolderImpl();
@@ -75,14 +76,8 @@ public class DateTimeValueSemanticsProvi
// assertEquals("21-May-2007 10:30", adapter.titleString(null));
}
- private void assertEntry(
- final String entry,
- final int year,
- final int month,
- final int day,
- final int hour,
- final int minute,
- final int second) {
+ private void assertEntry(final String entry, final int year, final int month, final int day, final int hour,
+ final int minute, final int second) {
final Object object = adapter.parseTextEntry(null, entry);
assertEquals(new DateTime(year, month, day, hour, minute, second), object);
}
@@ -103,6 +98,11 @@ public class DateTimeValueSemanticsProvi
}
@Test
+ public void testEntryWithLongISOFormatAndSeconds() {
+ assertEntry("2007-05-21 10:30:40", 2007, 5, 21, 10, 30, 40);
+ }
+
+ @Test
public void testEmptyClears() {
assertNull(adapter.parseTextEntry(null, ""));
}
@@ -148,4 +148,3 @@ public class DateTimeValueSemanticsProvi
}
}
-
Modified: incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/TimeValueSemanticsProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/TimeValueSemanticsProviderTest.java?rev=1090859&r1=1090858&r2=1090859&view=diff
==============================================================================
--- incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/TimeValueSemanticsProviderTest.java (original)
+++ incubator/isis/trunk/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/value/TimeValueSemanticsProviderTest.java Sun Apr 10 18:03:02 2011
@@ -17,7 +17,6 @@
* under the License.
*/
-
package org.apache.isis.core.progmodel.facets.value;
import static org.junit.Assert.assertEquals;
@@ -44,10 +43,12 @@ public class TimeValueSemanticsProviderT
@Before
public void setUpObjects() throws Exception {
- mockery.checking(new Expectations(){{
- allowing(mockConfiguration).getString("isis.value.format.time");
- will(returnValue(null));
- }});
+ mockery.checking(new Expectations() {
+ {
+ allowing(mockConfiguration).getString("isis.value.format.time");
+ will(returnValue(null));
+ }
+ });
TestClock.initialize();
setupSpecification(Time.class);
@@ -72,7 +73,7 @@ public class TimeValueSemanticsProviderT
public void testParseEntryOfHoursAfterNow() throws Exception {
// TimeValueSemanticsProvider adapter = new TimeValueSemanticsProvider();
final Object parsed = adapter.parseTextEntry(null, "+5H");
- assertEquals(new Time(2, 30), parsed);
+ assertEquals(new Time(2, 30, 25), parsed);
}
@Test
@@ -86,7 +87,7 @@ public class TimeValueSemanticsProviderT
public void testParseEntryOfHoursBeforeToNow() throws Exception {
// TimeValueSemanticsProvider adapter = new TimeValueSemanticsProvider();
final Object parsed = adapter.parseTextEntry(null, "-5H");
- assertEquals(new Time(16, 30), parsed);
+ assertEquals(new Time(16, 30, 25), parsed);
}
@Test
@@ -108,8 +109,8 @@ public class TimeValueSemanticsProviderT
try {
adapter.fromEncodedString("two ten");
fail();
- } catch (final EncodingException expected) {}
+ } catch (final EncodingException expected) {
+ }
}
}
-