← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14971: extend calendar with more functionality, get number of days in week, days in month, days in year, ...

 

------------------------------------------------------------
revno: 14971
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-04-24 22:56:52 +0545
message:
  extend calendar with more functionality, get number of days in week, days in month, days in year, get number of months (important for ethiopean calendar with 13 months)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/NepalCalendar.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/AbstractCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java	2014-04-24 16:21:52 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java	2014-04-24 17:11:52 +0000
@@ -44,4 +44,16 @@
     {
         return fromIso( new DateUnit( year, month, day ) );
     }
+
+    @Override
+    public int getMonthsInYear()
+    {
+        return 12;
+    }
+
+    @Override
+    public int getDaysInWeek()
+    {
+        return 7;
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-04-24 16:21:52 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-04-24 17:11:52 +0000
@@ -40,4 +40,12 @@
     DateUnit fromIso( int year, int month, int day );
 
     DateUnit fromIso( DateUnit dateUnit );
+
+    int getMonthsInYear();
+
+    int getDaysInWeek();
+
+    int getDaysInYear( int year );
+
+    int getDaysInMonth( int year, int month );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java	2014-04-24 15:46:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java	2014-04-24 17:11:52 +0000
@@ -61,4 +61,32 @@
 
         return DateUnit.fromDateTime( dateTime );
     }
+
+    @Override
+    public int getMonthsInYear()
+    {
+        DateTime dateTime = new DateTime( 1, 1, 1, 0, 0, chronology );
+        return dateTime.monthOfYear().getMaximumValue();
+    }
+
+    @Override
+    public int getDaysInWeek()
+    {
+        DateTime dateTime = new DateTime( 1, 1, 1, 0, 0, chronology );
+        return dateTime.dayOfWeek().getMaximumValue();
+    }
+
+    @Override
+    public int getDaysInYear( int year )
+    {
+        DateTime dateTime = new DateTime( year, 1, 1, 0, 0, chronology );
+        return (int) dateTime.year().toInterval().toDuration().getStandardDays();
+    }
+
+    @Override
+    public int getDaysInMonth( int year, int month )
+    {
+        DateTime dateTime = new DateTime( year, month, 1, 0, 0, chronology );
+        return (int) dateTime.monthOfYear().toInterval().toDuration().getStandardDays();
+    }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/NepalCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/NepalCalendar.java	2014-04-24 15:47:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/NepalCalendar.java	2014-04-24 17:11:52 +0000
@@ -57,23 +57,14 @@
 
         int totalDays = 0;
 
-        for ( int i = startNepal.getYear(); i < dateUnit.getYear(); i++ )
+        for ( int year = startNepal.getYear(); year < dateUnit.getYear(); year++ )
         {
-            // if year total index is uninitialized, calculate and set in array
-            if ( conversionMap.get( i )[0] == 0 )
-            {
-                for ( int j = 1; j <= 12; j++ )
-                {
-                    conversionMap.get( i )[0] += conversionMap.get( i )[j];
-                }
-            }
-
-            totalDays += conversionMap.get( i )[0];
+            totalDays += getYearTotal( year );
         }
 
-        for ( int i = startNepal.getMonth(); i < dateUnit.getMonth(); i++ )
+        for ( int month = startNepal.getMonth(); month < dateUnit.getMonth(); month++ )
         {
-            totalDays += conversionMap.get( dateUnit.getYear() )[i];
+            totalDays += conversionMap.get( dateUnit.getYear() )[month];
         }
 
         totalDays += dateUnit.getDay() - startNepal.getDay();
@@ -128,6 +119,32 @@
         return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
     }
 
+    @Override
+    public int getDaysInYear( int year )
+    {
+        return getYearTotal( year );
+    }
+
+    @Override
+    public int getDaysInMonth( int year, int month )
+    {
+        return conversionMap.get( year )[month];
+    }
+
+    private int getYearTotal( int year )
+    {
+        // if year total index is uninitialized, calculate and set in array
+        if ( conversionMap.get( year )[0] == 0 )
+        {
+            for ( int j = 1; j <= 12; j++ )
+            {
+                conversionMap.get( year )[0] += conversionMap.get( year )[j];
+            }
+        }
+
+        return conversionMap.get( year )[0];
+    }
+
     //------------------------------------------------------------------------------------------------------------
     // Conversion map for Nepali calendar
     //