Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reiwa #176

Merged
merged 5 commits into from
Apr 10, 2020
Merged

Reiwa #176

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
- name: Set up JDK 13
uses: olafurpg/setup-scala@v2
with:
java-version: 1.8
java-version: 13
- name: Cache Coursier
uses: actions/cache@v1
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,19 @@ object JapaneseEra {
*/
lazy val HEISEI: JapaneseEra = new JapaneseEra(2, LocalDate.of(1989, 1, 8), "Heisei")

/**
* The singleton instance for the 'Reiwa' era (2019-05-01 - current)
* which has the value 3.
*/
lazy val REIWA = new JapaneseEra(3, LocalDate.of(2019, 5, 1), "Reiwa")

/**
* The value of the additional era.
*/
private[chrono] val ADDITIONAL_VALUE: Int = 3
private[chrono] val ADDITIONAL_VALUE: Int = 4

private[chrono] lazy val KNOWN_ERAS: AtomicReference[Array[JapaneseEra]] = new AtomicReference(
Array(MEIJI, TAISHO, SHOWA, HEISEI)
Array(MEIJI, TAISHO, SHOWA, HEISEI, REIWA)
)

/** Obtains an instance of {@code JapaneseEra} from an {@code int} value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ object TTBPJapaneseEra {
*/
def registerEra(since: LocalDate, name: String): JapaneseEra = {
val known = JapaneseEra.KNOWN_ERAS.get
if (known.length > 4)
if (known.length > 5)
throw new DateTimeException("Only one additional Japanese era can be added")
require(since != null)
require(name != null)
if (!since.isAfter(JapaneseEra.HEISEI.since))
if (!since.isAfter(JapaneseEra.REIWA.since))
throw new DateTimeException(
"Invalid since date for additional Japanese era, must be after Heisei"
"Invalid since date for additional Japanese era, must be after Reiwa"
)
val era = new JapaneseEra(JapaneseEra.ADDITIONAL_VALUE, since, name)
val newArray = Arrays.copyOf(known, 5)
newArray(4) = era
val newArray = Arrays.copyOf(known, 6)
newArray(5) = era
if (!JapaneseEra.KNOWN_ERAS.compareAndSet(known, newArray))
throw new DateTimeException("Only one additional Japanese era can be added")
era
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ class TestJapaneseChronology extends AnyFunSuite with AssertionsHelper {
}

val data_japansesEras: List[(Era, Int, String)] = {
List((JapaneseEra.MEIJI, -1, "Meiji"),
(JapaneseEra.TAISHO, 0, "Taisho"),
(JapaneseEra.SHOWA, 1, "Showa"),
(JapaneseEra.HEISEI, 2, "Heisei"))
List(
(JapaneseEra.MEIJI, -1, "Meiji"),
(JapaneseEra.TAISHO, 0, "Taisho"),
(JapaneseEra.SHOWA, 1, "Showa"),
(JapaneseEra.HEISEI, 2, "Heisei"),
(JapaneseEra.REIWA, 3, "Reiwa")
)
}

test("test_Japanese_Eras") {
Expand All @@ -153,7 +156,7 @@ class TestJapaneseChronology extends AnyFunSuite with AssertionsHelper {
}

test("test_Japanese_badEras") {
val badEras: Array[Int] = Array(-1000, -998, -997, -2, 3, 4, 1000)
val badEras: Array[Int] = Array(-1000, -998, -997, -2, 4, 5, 1000)
for (badEra <- badEras)
try {
val era: Era = JapaneseChronology.INSTANCE.eraOf(badEra)
Expand All @@ -180,9 +183,9 @@ class TestJapaneseChronology extends AnyFunSuite with AssertionsHelper {
// ignore expected exception
}
val additional = TTBPJapaneseEra.registerEra(LocalDate.of(2100, 1, 1), "TestAdditional")
assertEquals(JapaneseEra.of(3), additional)
assertEquals(JapaneseEra.of(4), additional)
assertEquals(JapaneseEra.valueOf("TestAdditional"), additional)
assertEquals(JapaneseEra.values.apply(4), additional)
assertEquals(JapaneseEra.values.apply(5), additional)
try {
TTBPJapaneseEra.registerEra(LocalDate.of(2200, 1, 1), "TestAdditional2")
fail("JapaneseEra.registerEra should have failed")
Expand All @@ -202,7 +205,10 @@ class TestJapaneseChronology extends AnyFunSuite with AssertionsHelper {
(JapaneseChronology.INSTANCE.date(1926, 12, 25), "Japanese Showa 1-12-25"),
(JapaneseChronology.INSTANCE.date(1989, 1, 7), "Japanese Showa 64-01-07"),
(JapaneseChronology.INSTANCE.date(1989, 1, 8), "Japanese Heisei 1-01-08"),
(JapaneseChronology.INSTANCE.date(2012, 12, 6), "Japanese Heisei 24-12-06")
(JapaneseChronology.INSTANCE.date(2012, 12, 6), "Japanese Heisei 24-12-06"),
(JapaneseChronology.INSTANCE.date(2019, 4, 30), "Japanese Heisei 31-04-30"),
(JapaneseChronology.INSTANCE.date(2019, 5, 1), "Japanese Reiwa 1-05-01"),
(JapaneseChronology.INSTANCE.date(2020, 12, 24), "Japanese Reiwa 2-12-24")
)

test("test_toString") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,26 @@ class TestDateTimeTextPrinting extends AnyFunSuite with AssertionsHelper {
val dt: LocalDateTime = LocalDateTime.of(2010, 2, 1, 0, 0)
assertEquals(f.format(dt), "2")
}

test("formatFullDate") {
Locale.setDefault(Locale.CANADA) // Passes for both US and CANADA
val formatFullDate = DateTimeFormatter.ofPattern("MMMM dd, yyyy")
val dt = LocalDateTime.of(2018, 1, 30, 11, 2, 45, 750000)
assertResult("January 30, 2018")(dt.format(formatFullDate))
}

test("formatDayOfWeek") {
Locale.setDefault(Locale.CANADA) // Passes for US
val formatDayOfWeek = DateTimeFormatter.ofPattern("cccc MMM d") // Thursday Jan. 25
val dt = LocalDateTime.of(2018, 1, 30, 11, 2, 45, 750000)
assertResult("Tuesday Jan. 30")(dt.format(formatDayOfWeek))
}

test("formatTime_AM_PM") {
Locale.setDefault(Locale.CANADA) // Fails for both US and CANADA
val formatTime_AM_PM = DateTimeFormatter.ofPattern("h:mm a") // 10:15 PM
val dt = LocalDateTime.of(2018, 1, 30, 11, 2, 45, 750000)
assertResult("11:02 a.m.")(dt.format(formatTime_AM_PM))
}

}