dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #29993
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15208: rewrote logic for calculating nepali intervals. Working for: year, month, day
------------------------------------------------------------
revno: 15208
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-05-09 23:24:47 +0200
message:
rewrote logic for calculating nepali intervals. Working for: year, month, day
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.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/ChronologyBasedCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java 2014-05-09 18:53:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/ChronologyBasedCalendar.java 2014-05-09 21:24:47 +0000
@@ -81,7 +81,7 @@
private DateInterval toYearIsoInterval( DateUnit dateUnit, int offset, int length )
{
- DateTime from = dateUnit.toDateTime( chronology ).withMonthOfYear( 1 ).withDayOfMonth( 1 );
+ DateTime from = dateUnit.toDateTime( chronology );
if ( offset > 0 )
{
@@ -106,7 +106,7 @@
private DateInterval toMonthIsoInterval( DateUnit dateUnit, int offset, int length )
{
- DateTime from = dateUnit.toDateTime( chronology ).withDayOfMonth( 1 );
+ DateTime from = dateUnit.toDateTime( chronology );
if ( offset > 0 )
{
@@ -131,7 +131,7 @@
private DateInterval toWeekIsoInterval( DateUnit dateUnit, int offset, int length )
{
- DateTime from = dateUnit.toDateTime( chronology ).withDayOfWeek( 1 );
+ DateTime from = dateUnit.toDateTime( chronology );
if ( offset > 0 )
{
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java 2014-05-05 08:08:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java 2014-05-09 21:24:47 +0000
@@ -70,6 +70,14 @@
{
}
+ public DateUnit( DateUnit dateUnit )
+ {
+ this.year = dateUnit.getYear();
+ this.month = dateUnit.getMonth();
+ this.day = dateUnit.getDay();
+ this.dayOfWeek = dateUnit.getDayOfWeek();
+ }
+
public DateUnit( int year, int month, int day )
{
this.year = year;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java 2014-05-09 18:53:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java 2014-05-09 21:24:47 +0000
@@ -94,43 +94,7 @@
DateTime start = startIso.toDateTime();
DateTime end = dateUnit.toDateTime();
- int days = Days.daysBetween( start, end ).getDays();
-
- int curYear = startNepal.getYear();
- int curMonth = startNepal.getMonth();
- int curDay = startNepal.getDay();
- int dayOfWeek = startNepal.getDayOfWeek();
-
- while ( days != 0 )
- {
- // days in month
- int dm = conversionMap.get( curYear )[curMonth];
-
- curDay++;
-
- if ( curDay > dm )
- {
- curMonth++;
- curDay = 1;
- }
-
- if ( curMonth > 12 )
- {
- curYear++;
- curMonth = 1;
- }
-
- dayOfWeek++;
-
- if ( dayOfWeek > 7 )
- {
- dayOfWeek = 1;
- }
-
- days--;
- }
-
- return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
+ return plusDays( startNepal, Days.daysBetween( start, end ).getDays() );
}
@Override
@@ -153,8 +117,20 @@
private DateInterval toYearIsoInterval( DateUnit dateUnit, int offset, int length )
{
- DateUnit from = new DateUnit( dateUnit.getYear(), 1, 1 );
- DateUnit to = new DateUnit( dateUnit.getYear(), monthsInYear(), daysInMonth( dateUnit.getYear(), monthsInYear() ) );
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusYears( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusYears( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusYears( to, length );
+ to = minusDays( to, length );
from = toIso( from );
to = toIso( to );
@@ -164,8 +140,20 @@
private DateInterval toMonthIsoInterval( DateUnit dateUnit, int offset, int length )
{
- DateUnit from = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), 1 );
- DateUnit to = new DateUnit( dateUnit.getYear(), dateUnit.getMonth(), daysInMonth( dateUnit.getYear(), dateUnit.getMonth() ) );
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusMonths( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusMonths( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusMonths( to, length );
+ to = minusDays( to, 1 );
from = toIso( from );
to = toIso( to );
@@ -175,17 +163,29 @@
private DateInterval toWeekIsoInterval( DateUnit dateUnit, int offset, int length )
{
- DateTime dateTime = toIso( dateUnit ).toDateTime();
-
- DateTime from = dateTime.weekOfWeekyear().toInterval().getStart();
- DateTime to = dateTime.weekOfWeekyear().toInterval().getEnd().minusDays( 1 );
-
- return new DateInterval( DateUnit.fromDateTime( from ), DateUnit.fromDateTime( to ), DateIntervalType.ISO8601_WEEK );
+ return null;
}
private DateInterval toDayIsoInterval( DateUnit dateUnit, int offset, int length )
{
- return null;
+ DateUnit from = new DateUnit( dateUnit );
+
+ if ( offset > 0 )
+ {
+ from = plusDays( from, offset );
+ }
+ else if ( offset < 0 )
+ {
+ from = minusDays( from, -offset );
+ }
+
+ DateUnit to = new DateUnit( from );
+ to = plusDays( to, length );
+
+ from = toIso( from );
+ to = toIso( to );
+
+ return new DateInterval( from, to, DateIntervalType.ISO8601_DAY );
}
@Override
@@ -291,6 +291,156 @@
return conversionMap.get( year )[0];
}
+ private DateUnit minusYears( DateUnit dateUnit, int years )
+ {
+ DateUnit result = new DateUnit( dateUnit.getYear() - years, dateUnit.getMonth(), dateUnit.getDay(), dateUnit.getDayOfWeek() );
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ private DateUnit minusMonths( DateUnit dateUnit, int months )
+ {
+ DateUnit result = new DateUnit( dateUnit );
+
+ while ( months != 0 )
+ {
+ result.setMonth( result.getMonth() - 1 );
+
+ if ( result.getMonth() < 1 )
+ {
+ result.setMonth( monthsInYear() );
+ result.setYear( result.getYear() - 1 );
+ }
+
+ months--;
+ }
+
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ private DateUnit minusDays( DateUnit dateUnit, int days )
+ {
+ int curYear = dateUnit.getYear();
+ int curMonth = dateUnit.getMonth();
+ int curDay = dateUnit.getDay();
+ int dayOfWeek = dateUnit.getDayOfWeek();
+
+ while ( days != 0 )
+ {
+ curDay--;
+
+ if ( curDay == 0 )
+ {
+ curMonth--;
+
+ if ( curMonth == 0 )
+ {
+ curYear--;
+ curMonth = 12;
+ }
+
+ curDay = conversionMap.get( curYear )[curMonth];
+ }
+
+ dayOfWeek--;
+
+ if ( dayOfWeek == 0 )
+ {
+ dayOfWeek = 7;
+ }
+
+ days--;
+ }
+
+ return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
+ }
+
+ private DateUnit plusYears( DateUnit dateUnit, int years )
+ {
+ DateUnit result = new DateUnit( dateUnit.getYear() + years, dateUnit.getMonth(), dateUnit.getDay(), dateUnit.getDayOfWeek() );
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ private DateUnit plusMonths( DateUnit dateUnit, int months )
+ {
+ DateUnit result = new DateUnit( dateUnit );
+
+ while ( months != 0 )
+ {
+ result.setMonth( result.getMonth() + 1 );
+
+ if ( result.getMonth() > monthsInYear() )
+ {
+ result.setMonth( 1 );
+ result.setYear( result.getYear() + 1 );
+ }
+
+ months--;
+ }
+
+ updateDateUnit( result );
+
+ return result;
+ }
+
+ private DateUnit plusDays( DateUnit dateUnit, int days )
+ {
+ int curYear = dateUnit.getYear();
+ int curMonth = dateUnit.getMonth();
+ int curDay = dateUnit.getDay();
+ int dayOfWeek = dateUnit.getDayOfWeek();
+
+ while ( days != 0 )
+ {
+ // days in month
+ int dm = conversionMap.get( curYear )[curMonth];
+
+ curDay++;
+
+ if ( curDay > dm )
+ {
+ curMonth++;
+ curDay = 1;
+ }
+
+ if ( curMonth > 12 )
+ {
+ curYear++;
+ curMonth = 1;
+ }
+
+ dayOfWeek++;
+
+ if ( dayOfWeek > 7 )
+ {
+ dayOfWeek = 1;
+ }
+
+ days--;
+ }
+
+ return new DateUnit( curYear, curMonth, curDay, dayOfWeek );
+ }
+
+ // check if day is more than current maximum for month, don't overflow, just set to maximum
+ // set day of week
+ private void updateDateUnit( DateUnit result )
+ {
+ int dm = conversionMap.get( result.getYear() )[result.getMonth()];
+
+ if ( result.getDay() > dm )
+ {
+ result.setDay( dm );
+ }
+
+ result.setDayOfWeek( weekday( result ) );
+ }
+
//------------------------------------------------------------------------------------------------------------
// Conversion map for Nepali calendar
//