← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19852: move 13 month fixes from *PeriodTypes to EthiopeanCalendar, fixed to 12 months (12*30), month 13 ...

 

------------------------------------------------------------
revno: 19852
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-08-26 13:03:30 +0700
message:
  move 13 month fixes from *PeriodTypes to EthiopeanCalendar, fixed to 12 months (12*30), month 13 not allowed for now
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/EthiopianCalendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.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/impl/EthiopianCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/EthiopianCalendar.java	2015-08-24 08:48:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/EthiopianCalendar.java	2015-08-26 06:03:30 +0000
@@ -64,14 +64,25 @@
     @Override
     public DateTimeUnit toIso( DateTimeUnit dateTimeUnit )
     {
-        dateTimeUnit = normalize( dateTimeUnit );
+        if ( dateTimeUnit.getMonth() > 12 )
+        {
+            throw new RuntimeException( "Illegal month, must be between 1 and 12, was given " + dateTimeUnit.getMonth() );
+        }
+
         return super.toIso( dateTimeUnit );
     }
 
     @Override
     public DateTimeUnit fromIso( Date date )
     {
-        return super.fromIso( date );
+        DateTimeUnit dateTimeUnit = super.fromIso( date );
+
+        if ( dateTimeUnit.getMonth() > 12 )
+        {
+            throw new RuntimeException( "Illegal month, must be between 1 and 12, was given " + dateTimeUnit.getMonth() );
+        }
+
+        return dateTimeUnit;
     }
 
     @Override
@@ -81,27 +92,48 @@
     }
 
     @Override
+    public DateTimeUnit plusDays( DateTimeUnit dateTimeUnit, int days )
+    {
+        dateTimeUnit = super.plusDays( dateTimeUnit, days );
+
+        if ( dateTimeUnit.getMonth() > 12 )
+        {
+            dateTimeUnit.setYear( dateTimeUnit.getYear() + 1 );
+            dateTimeUnit.setMonth( 1 );
+            dateTimeUnit.setDay( 1 );
+        }
+
+        return dateTimeUnit;
+    }
+
+    @Override
+    public DateTimeUnit plusMonths( DateTimeUnit dateTimeUnit, int months )
+    {
+        dateTimeUnit = super.plusMonths( dateTimeUnit, months );
+
+        if ( dateTimeUnit.getMonth() > 12 )
+        {
+            dateTimeUnit.setYear( dateTimeUnit.getYear() + 1 );
+            dateTimeUnit.setMonth( 1 );
+        }
+
+        return dateTimeUnit;
+    }
+
+    @Override
+    public int daysInYear( int year )
+    {
+        return 12 * 30;
+    }
+
+    @Override
     public int daysInMonth( int year, int month )
     {
-        if ( month < 12 )
-        {
-            return 30;
-        }
-
-        return 30 + super.daysInMonth( year, 13 );
-    }
-
-    private DateTimeUnit normalize( DateTimeUnit dateTimeUnit )
-    {
-        if ( dateTimeUnit.getMonth() < 12 || dateTimeUnit.getDay() <= 30 )
-        {
-            return dateTimeUnit;
-        }
-
-        dateTimeUnit = new DateTimeUnit( dateTimeUnit );
-        dateTimeUnit.setDay( dateTimeUnit.getDay() - 30 );
-        dateTimeUnit.setMonth( 13 );
-
-        return dateTimeUnit;
+        if ( month > 12 )
+        {
+            throw new RuntimeException( "Illegal month, must be between 1 and 12, was given " + month );
+        }
+
+        return 30;
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java	2015-08-24 04:40:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java	2015-08-26 06:03:30 +0000
@@ -188,9 +188,10 @@
     {
         List<Period> periods = new ArrayList<>();
 
-        Period period = createPeriod( startDate );
+        Period period = createPeriod( startDate, calendar );
+        Period endPeriod = createPeriod( endDate, calendar );
 
-        while ( period.getStartDate().before( endDate ) )
+        while ( period.getStartDate().before( endPeriod.getEndDate() ) )
         {
             periods.add( period );
             period = getNextPeriod( period, calendar );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java	2015-06-11 21:37:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java	2015-08-26 06:03:30 +0000
@@ -50,7 +50,7 @@
     private static final long serialVersionUID = 5371766471215556241L;
 
     public static final String ISO_FORMAT = "yyyyMMdd";
-    
+
     private static final String ISO8601_DURATION = "P1D";
 
     /**
@@ -166,11 +166,11 @@
     {
         return ISO_FORMAT;
     }
-    
+
     @Override
-    public String getIso8601Duration() 
+    public String getIso8601Duration()
     {
-        return ISO8601_DURATION; 
+        return ISO8601_DURATION;
     }
 
 

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2015-08-24 08:48:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java	2015-08-26 06:03:30 +0000
@@ -78,12 +78,6 @@
         DateTimeUnit start = new DateTimeUnit( dateTimeUnit );
         start.setDay( 1 );
 
-        if ( start.getMonth() > 12 )
-        {
-            start.setYear( start.getYear() + 1 );
-            start.setMonth( 1 );
-        }
-
         DateTimeUnit end = new DateTimeUnit( dateTimeUnit );
         end.setDay( calendar.daysInMonth( end.getYear(), end.getMonth() ) );
 
@@ -106,7 +100,7 @@
         DateTimeUnit dateTimeUnit = calendar.fromIso( DateTimeUnit.fromJdkDate( period.getStartDate() ) );
         dateTimeUnit = calendar.plusMonths( dateTimeUnit, 1 );
 
-        return createPeriod( calendar.toIso( dateTimeUnit ), calendar );
+        return createPeriod( dateTimeUnit, calendar );
     }
 
     @Override

=== 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 08:48:49 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/calendar/impl/EthiopianCalendarTest.java	2015-08-26 06:03:30 +0000
@@ -36,7 +36,6 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.QuarterlyPeriodType;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import java.util.Date;
@@ -67,20 +66,17 @@
         assertEquals( 11, startOfYear.getDay() );
     }
 
+    @Test( expected = RuntimeException.class )
+    public void testDaysInMonth13()
+    {
+        calendar.daysInMonth( 2007, 13 );
+    }
+
     @Test
-    public void testDaysInMonth()
+    public void testDaysInYear()
     {
-        int month12 = calendar.daysInMonth( 2007, 12 );
-        int month13 = calendar.daysInMonth( 2007, 13 );
-
-        assertEquals( 36, month12 );
-        assertEquals( 36, month13 );
-
-        month12 = calendar.daysInMonth( 2004, 12 );
-        month13 = calendar.daysInMonth( 2004, 13 );
-
-        assertEquals( 35, month12 );
-        assertEquals( 35, month13 );
+        int daysInYear = calendar.daysInYear( 2006 );
+        assertEquals( 12 * 30, daysInYear );
     }
 
     @Test
@@ -90,7 +86,7 @@
         Date endDate = new Cal( 2025, 1, 2, true ).time();
 
         List<Period> days = new DailyPeriodType().generatePeriods( calendar, startDate, endDate );
-        assertEquals( 18264, days.size() );
+        assertEquals( 18001, days.size() );
     }
 
     @Test
@@ -104,7 +100,6 @@
     }
 
     @Test
-    @Ignore
     public void testGenerateMonthlyPeriods()
     {
         Date startDate = new Cal( 1975, 1, 1, true ).time();