-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compute quarters using adjusters for more uniform implementation
- Loading branch information
Showing
4 changed files
with
27 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 13 additions & 19 deletions
32
std-bits/base/src/main/java/org/enso/base/time/Date_Period_Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
package org.enso.base.time; | ||
|
||
import java.time.temporal.ChronoField; | ||
import java.time.temporal.Temporal; | ||
import java.time.temporal.TemporalAdjusters; | ||
import java.time.YearMonth; | ||
import java.time.temporal.*; | ||
|
||
public class Date_Period_Utils implements TimeUtilsBase { | ||
public static Temporal quarter_start(Temporal temporal) { | ||
int month = temporal.get(ChronoField.MONTH_OF_YEAR); | ||
int quarter = (month - 1) / 3; | ||
int firstMonth = quarter * 3 + 1; | ||
return temporal | ||
.with(ChronoField.MONTH_OF_YEAR, firstMonth) | ||
.with(TemporalAdjusters.firstDayOfMonth()); | ||
} | ||
|
||
public static Temporal quarter_end(Temporal temporal) { | ||
int month = temporal.get(ChronoField.MONTH_OF_YEAR); | ||
int quarter = (month - 1) / 3; | ||
int lastMonth = quarter * 3 + 3; | ||
return temporal | ||
.with(ChronoField.MONTH_OF_YEAR, lastMonth) | ||
.with(TemporalAdjusters.lastDayOfMonth()); | ||
} | ||
public static TemporalAdjuster quarter_start = (Temporal temporal) -> { | ||
int currentQuarter = temporal.get(IsoFields.QUARTER_OF_YEAR); | ||
int month = (currentQuarter - 1) * 3 + 1; | ||
return temporal.with(ChronoField.MONTH_OF_YEAR, month).with(TemporalAdjusters.firstDayOfMonth()); | ||
}; | ||
|
||
public static TemporalAdjuster quarter_end = (Temporal temporal) -> { | ||
int currentQuarter = YearMonth.from(temporal).get(IsoFields.QUARTER_OF_YEAR); | ||
int month = (currentQuarter - 1) * 3 + 3; | ||
return temporal.with(ChronoField.MONTH_OF_YEAR, month).with(TemporalAdjusters.lastDayOfMonth()); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters