Skip to content

Commit

Permalink
Hindu eras
Browse files Browse the repository at this point in the history
see issue #851
  • Loading branch information
MenoData committed Oct 8, 2019
1 parent c67e3ce commit 8523b96
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 24 deletions.
191 changes: 191 additions & 0 deletions base/src/main/java/net/time4j/calendar/hindu/HinduEra.java
@@ -0,0 +1,191 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2019 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (HinduEra.java) is part of project Time4J.
*
* Time4J is free software: You can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Time4J is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Time4J. If not, see <http://www.gnu.org/licenses/>.
* -----------------------------------------------------------------------
*/

package net.time4j.calendar.hindu;

import net.time4j.engine.CalendarEra;
import net.time4j.format.CalendarText;
import net.time4j.format.TextWidth;

import java.util.Locale;


/**
* <p>The Hindu calendar supports several eras in different regions of Indian subcontinent. </p>
*
* @author Meno Hochschild
* @since 5.5
*/
/*[deutsch]
* <p>Der Hindu-Kalender unterst&uuml;tzt mehrere &Auml;ras in verschiedenen Regionen des
* indischen Subkontinents. </p>
*
* @author Meno Hochschild
* @since 5.5
*/
public enum HinduEra
implements CalendarEra {

//~ Statische Felder/Initialisierungen --------------------------------

/**
* The onset of this ancient era (iron age) is 3179 years before Saka.
*/
/*[deutsch]
* Der Beginn dieser historischen &Auml;ra (eisernes Zeitalter) liegt 3179 Jahre vor Saka.
*/
KALI_YUGA,

/**
* The onset of this era mainly used in Northern India is 135 years before Saka.
*/
/*[deutsch]
* Der Beginn dieser vorwiegend in Nordindien verwendeten &Auml;ra liegt 135 Jahre vor Saka.
*/
VIKRAMA,

/**
* The onset of this era is in gregorian year +78.
*/
/*[deutsch]
* Der Beginn dieser &Auml;ra liegt im gregorianischen Jahr +78.
*/
SAKA,

/**
* The onset of this era mainly used in West Bengal is 515 years after Saka.
*/
/*[deutsch]
* Der Beginn dieser vorwiegend in Westbengalen verwendeten &Auml;ra liegt 515 Jahre nach Saka.
*/
BENGAL,

/**
* The onset of this era mainly used in Kerala (part of Malayalam calendar) is 900 years before Saka.
*/
/*[deutsch]
* Der Beginn dieser vorwiegend in Kerala verwendeten &Auml;ra (Bestandteil des Malayalam-Kalenders)
* liegt 900 Jahre vor Saka.
*/
KOLLAM,

/**
* The onset of this era mainly used in Nepal is 955 years before Saka.
*/
/*[deutsch]
* Der Beginn dieser vorwiegend in Nepal verwendeten &Auml;ra liegt 955 Jahre vor Saka.
*/
NEPALESE;

private static final int[] SAKA_OFFSETS = {3179, 135, 0, -515, 900, 955};

//~ Methoden ----------------------------------------------------------

/**
* <p>Equivalent to the expression {@code getDisplayName(locale, TextWidth.WIDE)}. </p>
*
* @param locale language setting
* @return descriptive text (long form, never {@code null})
* @see #getDisplayName(Locale, TextWidth)
* @since 5.5
*/
/*[deutsch]
* <p>Entspricht dem Ausdruck {@code getDisplayName(locale, TextWidth.WIDE)}. </p>
*
* @param locale language setting
* @return descriptive text (long form, never {@code null})
* @see #getDisplayName(Locale, TextWidth)
* @since 5.5
*/
public String getDisplayName(Locale locale) {

return this.getDisplayName(locale, TextWidth.WIDE);

}

/**
* <p>Gets the description text dependent on the locale and style parameters. </p>
*
* <p>The second argument controls the width of description. </p>
*
* @param locale language setting
* @param width text width
* @return descriptive text for given locale and style (never {@code null})
* @since 5.5
*/
/*[deutsch]
* <p>Liefert den sprachabh&auml;ngigen Beschreibungstext. </p>
*
* <p>&Uuml;ber das zweite Argument kann gesteuert werden, ob eine kurze
* oder eine lange Form des Beschreibungstexts ausgegeben werden soll. Das
* ist besonders sinnvoll in Benutzeroberfl&auml;chen, wo zwischen der
* Beschriftung und der detaillierten Erl&auml;uterung einer graphischen
* Komponente unterschieden wird. </p>
*
* @param locale language setting
* @param width text width
* @return descriptive text for given locale and style (never {@code null})
* @since 5.5
*/
public String getDisplayName(
Locale locale,
TextWidth width
) {

CalendarText names = CalendarText.getInstance("extra/hindu", locale);
return names.getEras(width).print(this);

}

/**
* <p>Scales given year of era to another year related to this era. </p>
*
* @param era era reference of given year
* @param yearOfEra current year reckoned in given era
* @return current year related to this era
* @throws IllegalArgumentException if given year of era is out of range
* @since 5.5
*/
/*[deutsch]
* <p>Skaliert das angegebene Jahr der &Auml;ra zu einem anderen Jahreswert bezogen auf diese &Auml;ra. </p>
*
* @param era era reference of given year
* @param yearOfEra current year reckoned in given era
* @return current year related to this era
* @throws IllegalArgumentException if given year of era is out of range
* @since 5.5
*/
public int yearOfEra(
HinduEra era,
int yearOfEra
) {

try {
return Math.subtractExact(
Math.addExact(yearOfEra, SAKA_OFFSETS[this.ordinal()]),
SAKA_OFFSETS[era.ordinal()]);
} catch (ArithmeticException ex) {
throw new IllegalArgumentException("Out of range: " + yearOfEra);
}

}

}
Expand Up @@ -84,6 +84,7 @@ public final class GenericTextProviderSPI
types.add("dangi");
types.add("ethiopic");
types.add("extra/frenchrev");
types.add("extra/hindu");
types.add("generic");
types.add("hebrew");
types.add("indian");
Expand Down Expand Up @@ -143,15 +144,21 @@ public String[] months(
boolean leapForm
) {

if (calendarType.equals("roc") || calendarType.equals("buddhist")) {
List<String> months = CalendarText.getIsoInstance(locale).getStdMonths(tw, oc).getTextForms();
return months.toArray(new String[months.size()]);
} else if (calendarType.equals("japanese")) {
return new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
} else if (calendarType.equals("dangi") || calendarType.equals("vietnam")) {
calendarType = "chinese"; // Umleitung
} else if (calendarType.equals("juche")) {
return CalendarText.getIsoInstance(locale).getStdMonths(tw, oc).getTextForms().toArray(new String[12]);
switch (calendarType) {
case "roc":
case "buddhist":
case "juche":
List<String> months = CalendarText.getIsoInstance(locale).getStdMonths(tw, oc).getTextForms();
return months.toArray(new String[months.size()]);
case "japanese":
return new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
case "dangi":
case "vietnam":
calendarType = "chinese"; // Umleitung
break;
case "extra/hindu":
calendarType = "indian"; // Umleitung
break;
}

PropertyBundle rb = getBundle(calendarType, locale);
Expand Down Expand Up @@ -223,19 +230,23 @@ public String[] eras(
TextWidth tw
) {

if (calendarType.equals("chinese") || calendarType.equals("vietnam")) {
return EMPTY_STRINGS; // special handling in era elements of East Asian calendars
} else if (calendarType.equals("japanese")) { // special handling in class Nengo !!!
if (tw == TextWidth.NARROW) {
return new String[] { "M", "T", "S", "H" };
} else {
return new String[] { "Meiji", "Taishō", "Shōwa", "Heisei" };
}
} else if (calendarType.equals("dangi") || calendarType.equals("juche")) {
String[] koreans = this.eras("korean", locale, tw);
String[] names = new String[1];
names[0] = (calendarType.equals("dangi") ? koreans[0] : koreans[1]);
return names;
switch (calendarType) {
case "chinese":
case "vietnam":
return EMPTY_STRINGS; // special handling in era elements of East Asian calendars

case "japanese": // special handling in class Nengo !!!
if (tw == TextWidth.NARROW) {
return new String[]{"M", "T", "S", "H"};
} else {
return new String[]{"Meiji", "Taishō", "Shōwa", "Heisei"};
}
case "dangi":
case "juche":
String[] koreans = this.eras("korean", locale, tw);
String[] names = new String[1];
names[0] = (calendarType.equals("dangi") ? koreans[0] : koreans[1]);
return names;
}

PropertyBundle rb = getBundle(calendarType, locale);
Expand Down Expand Up @@ -452,9 +463,13 @@ private static int countOfMonths(String ct) {

private static int countOfEras(String ct) {

if (ct.equals("extra/hindu")) {
return 6;
}

return (
(ct.equals("ethiopic") || ct.equals("generic")
|| ct.equals("roc") || ct.equals("buddhist") || ct.equals("korean")) ? 2 : 1);
|| ct.equals("roc") || ct.equals("buddhist") || ct.equals("korean")) ? 2 : 1);

}

Expand Down
27 changes: 27 additions & 0 deletions base/src/main/resources/names/extra/hindu.properties
@@ -0,0 +1,27 @@
# -------------------------------------------------------------------------
# Legend:
#
# Short keys: M=MONTH_OF_YEAR, E=ERA
# Format mode: w=WIDE, a=ABBREVIATED, s=SHORT, n=NARROW
# -------------------------------------------------------------------------

# supported list of languages
languages=

# property key format (first letter only if true - relevant for: M, E)
useShortKeys=true

# eras
E(w)_0=Kali Yuga
E(w)_1=Vikrama Samvat
E(w)_2=Saka
E(w)_3=Bengali Sambat
E(w)_4=Kollam Era
E(w)_5=Nepalese Era

E(a)_0=K.Y
E(a)_1=V.S.
E(a)_2=Saka
E(a)_3=B.S.
E(a)_4=K.E.
E(a)_5=N.E.
1 change: 0 additions & 1 deletion base/src/test/java/net/time4j/DateComparisonTest.java
Expand Up @@ -2,7 +2,6 @@

import net.time4j.base.GregorianDate;
import net.time4j.engine.TimePoint;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down
2 changes: 2 additions & 0 deletions base/src/test/java/net/time4j/calendar/CalendarSuite.java
Expand Up @@ -2,6 +2,7 @@

import net.time4j.calendar.astro.AstroSuite;
import net.time4j.calendar.frenchrev.FrenchRepublicanSuite;
import net.time4j.calendar.hindu.HinduSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
Expand Down Expand Up @@ -39,6 +40,7 @@
HijriPatternTest.class,
HijriUnitTest.class,
HijriYearTest.class,
HinduSuite.class,
HistoricCalendarTest.class,
HongkongObservatory1Test.class,
HongkongObservatory2Test.class,
Expand Down

0 comments on commit 8523b96

Please sign in to comment.