Skip to content

Commit

Permalink
Compute quarters using adjusters for more uniform implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Sep 10, 2022
1 parent 796ccfc commit 918b1f4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from Standard.Base import all

polyglot java import org.enso.base.Time_Utils
polyglot java import org.enso.base.time.Date_Period_Utils
polyglot java import java.time.temporal.TemporalAdjuster
polyglot java import java.time.temporal.TemporalAdjusters

## Represents a period of time longer on the scale of days (longer than a day).
Expand All @@ -17,20 +19,18 @@ type Date_Period

## PRIVATE
adjust_start : (Date | Date_Time) -> (Date | Date_Time)
adjust_start self date = case self of
Year ->
(Time_Utils.utils_for date).apply_adjuster date TemporalAdjusters.firstDayOfYear
Quarter ->
(Time_Utils.utils_for date).quarter_start date
Month ->
(Time_Utils.utils_for date).apply_adjuster date TemporalAdjusters.firstDayOfMonth
adjust_start self date =
adjuster = case self of
Year -> TemporalAdjusters.firstDayOfYear
Quarter -> Date_Period_Utils.quarter_start
Month -> TemporalAdjusters.firstDayOfMonth
(Time_Utils.utils_for date).apply_adjuster date adjuster

## PRIVATE
adjust_end : (Date | Date_Time) -> (Date | Date_Time)
adjust_end self date = case self of
Year ->
(Time_Utils.utils_for date).apply_adjuster date TemporalAdjusters.lastDayOfYear
Quarter ->
(Time_Utils.utils_for date).quarter_end date
Month ->
(Time_Utils.utils_for date).apply_adjuster date TemporalAdjusters.lastDayOfMonth
adjust_end self date =
adjuster = case self of
Year -> TemporalAdjusters.lastDayOfYear
Quarter -> Date_Period_Utils.quarter_end
Month -> TemporalAdjusters.lastDayOfMonth
(Time_Utils.utils_for date).apply_adjuster date adjuster
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());
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
public class Date_Time_Utils implements TimeUtilsBase {
public static final Date_Time_Utils INSTANCE = new Date_Time_Utils();

public ZonedDateTime quarter_start(ZonedDateTime date) {
return (ZonedDateTime) Date_Period_Utils.quarter_start(date);
}

public ZonedDateTime quarter_end(ZonedDateTime date) {
return (ZonedDateTime) Date_Period_Utils.quarter_end(date);
}

public ZonedDateTime start_of_time_period(ZonedDateTime date, TemporalUnit unit) {
return date.truncatedTo(unit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
public class Date_Utils implements TimeUtilsBase {
public static final Date_Utils INSTANCE = new Date_Utils();

public LocalDate quarter_start(LocalDate date) {
return (LocalDate) Date_Period_Utils.quarter_start(date);
}

public LocalDate quarter_end(LocalDate date) {
return (LocalDate) Date_Period_Utils.quarter_end(date);
}

public LocalDate apply_adjuster(LocalDate date, TemporalAdjuster adjuster) {
return date.with(adjuster);
}
Expand Down

0 comments on commit 918b1f4

Please sign in to comment.