← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19828: bugfixes for DateTimeUnit.toJodaDateTime when using different chronology than ISO, also allows fo...

 

------------------------------------------------------------
revno: 19828
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-08-24 14:03:59 +0700
message:
  bugfixes for DateTimeUnit.toJodaDateTime when using different chronology than ISO, also allows for used chronology to be specified
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateTimeUnit.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/impl/EthiopianCalendarTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateTimeUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateTimeUnit.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateTimeUnit.java	2015-08-24 07:03:59 +0000
@@ -28,11 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.Date;
-import java.util.TimeZone;
-
-import javax.validation.constraints.NotNull;
-
+import com.google.common.base.MoreObjects;
 import org.joda.time.Chronology;
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
@@ -40,7 +36,9 @@
 import org.joda.time.LocalDateTime;
 import org.joda.time.chrono.ISOChronology;
 
-import com.google.common.base.MoreObjects;
+import javax.validation.constraints.NotNull;
+import java.util.Date;
+import java.util.TimeZone;
 
 /**
  * Class representing a specific calendar date.
@@ -281,35 +279,34 @@
     }
 
     /**
-     * Converts dateUnit to Joda-Time DateTime
+     * Converts dateUnit to Joda-Time DateTime using ISO chronology
      *
      * @return Populated DateTime object
      */
     public DateTime toJodaDateTime()
     {
+        return toJodaDateTime( ISOChronology.getInstance() );
+    }
+
+    /**
+     * Converts dateUnit to Joda-Time DateTime with a specific chronology.
+     *
+     * @param chronology Chronology to use
+     * @return Populated DateTime object
+     */
+    public DateTime toJodaDateTime( Chronology chronology )
+    {
         try
         {
-            return new DateTime( year, month, day, hour, minute, second, millis, ISOChronology.getInstance( DateTimeZone.forTimeZone( timeZone ) ) );
+            return new DateTime( year, month, day, hour, minute, second, millis, chronology.withZone( DateTimeZone.forTimeZone( timeZone ) ) );
         }
         catch ( IllegalInstantException ex )
         {
             LocalDateTime localDateTime = new LocalDateTime( year, month, day, hour, minute, second, millis,
-                ISOChronology.getInstance( DateTimeZone.forTimeZone( timeZone ) ) );
+                chronology.withZone( DateTimeZone.forTimeZone( timeZone ) ) );
 
             return localDateTime.toLocalDate().toDateTimeAtStartOfDay();
         }
-
-    }
-
-    /**
-     * Converts dateUnit to Joda-Time DateTime with a specific chronology.
-     *
-     * @param chronology Chronology to use
-     * @return Populated DateTime object
-     */
-    public DateTime toJodaDateTime( Chronology chronology )
-    {
-        return toJodaDateTime().withChronology( chronology );
     }
 
     /**

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/impl/EthiopianCalendarTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/impl/EthiopianCalendarTest.java	2015-08-24 03:56:46 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/impl/EthiopianCalendarTest.java	2015-08-24 07:03:59 +0000
@@ -30,9 +30,15 @@
 
 import org.hisp.dhis.calendar.Calendar;
 import org.hisp.dhis.calendar.DateTimeUnit;
+import org.hisp.dhis.period.Cal;
+import org.hisp.dhis.period.DailyPeriodType;
+import org.hisp.dhis.period.Period;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.util.Date;
+import java.util.List;
+
 import static org.junit.Assert.assertEquals;
 
 /**
@@ -73,4 +79,14 @@
         assertEquals( 35, month12 );
         assertEquals( 35, month13 );
     }
+
+    @Test
+    public void testGenerateDailyPeriods()
+    {
+        Date startDate = new Cal( 1975, 1, 1, true ).time();
+        Date endDate = new Cal( 2025, 1, 2, true ).time();
+
+        List<Period> days = new DailyPeriodType().generatePeriods( calendar, startDate, endDate );
+        assertEquals( 18264, days.size() );
+    }
 }