-
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.
Add Period Start and End functions to Date and DateTime (#3695)
- Loading branch information
Showing
18 changed files
with
493 additions
and
25 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
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
36 changes: 36 additions & 0 deletions
36
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Date_Period.enso
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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). | ||
type Date_Period | ||
Year | ||
Quarter | ||
Month | ||
|
||
## PRIVATE | ||
This method could be replaced with matching on `Date_Period` supertype | ||
if/when that is supported. | ||
is_date_period : Boolean | ||
is_date_period self = True | ||
|
||
## PRIVATE | ||
adjust_start : (Date | Date_Time) -> (Date | Date_Time) | ||
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 = | ||
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 |
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
35 changes: 35 additions & 0 deletions
35
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Time/Time_Period.enso
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from Standard.Base import all | ||
|
||
polyglot java import org.enso.base.Time_Utils | ||
polyglot java import java.time.temporal.ChronoUnit | ||
|
||
## Represents a period of time of a day or shorter. | ||
type Time_Period | ||
Day | ||
Hour | ||
Minute | ||
Second | ||
|
||
## PRIVATE | ||
This method could be replaced with matching on `Date_Period` supertype | ||
if/when that is supported. | ||
is_date_period : Boolean | ||
is_date_period self = False | ||
|
||
## PRIVATE | ||
to_java_unit : TemporalUnit | ||
to_java_unit self = case self of | ||
Day -> ChronoUnit.DAYS | ||
Hour -> ChronoUnit.HOURS | ||
Minute -> ChronoUnit.MINUTES | ||
Second -> ChronoUnit.SECONDS | ||
|
||
## PRIVATE | ||
adjust_start : (Time_Of_Day | Date_Time) -> (Time_Of_Day | Date_Time) | ||
adjust_start self date = | ||
(Time_Utils.utils_for date).start_of_time_period date self.to_java_unit | ||
|
||
## PRIVATE | ||
adjust_end : (Time_Of_Day | Date_Time) -> (Time_Of_Day | Date_Time) | ||
adjust_end self date = | ||
(Time_Utils.utils_for date).end_of_time_period date self.to_java_unit |
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
25 changes: 25 additions & 0 deletions
25
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.enso.base.time; | ||
|
||
import java.time.YearMonth; | ||
import java.time.temporal.*; | ||
|
||
public class Date_Period_Utils implements TimeUtilsBase { | ||
|
||
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()); | ||
}; | ||
} |
21 changes: 21 additions & 0 deletions
21
std-bits/base/src/main/java/org/enso/base/time/Date_Time_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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.enso.base.time; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.time.temporal.TemporalAdjuster; | ||
import java.time.temporal.TemporalUnit; | ||
|
||
public class Date_Time_Utils implements TimeUtilsBase { | ||
public static final Date_Time_Utils INSTANCE = new Date_Time_Utils(); | ||
|
||
public ZonedDateTime start_of_time_period(ZonedDateTime date, TemporalUnit unit) { | ||
return date.truncatedTo(unit); | ||
} | ||
|
||
public ZonedDateTime end_of_time_period(ZonedDateTime date, TemporalUnit unit) { | ||
return date.truncatedTo(unit).plus(1, unit).minusNanos(1); | ||
} | ||
|
||
public ZonedDateTime apply_adjuster(ZonedDateTime date, TemporalAdjuster adjuster) { | ||
return date.with(adjuster); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
std-bits/base/src/main/java/org/enso/base/time/Date_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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.enso.base.time; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.TemporalAdjuster; | ||
|
||
public class Date_Utils implements TimeUtilsBase { | ||
public static final Date_Utils INSTANCE = new Date_Utils(); | ||
|
||
public LocalDate apply_adjuster(LocalDate date, TemporalAdjuster adjuster) { | ||
return date.with(adjuster); | ||
} | ||
} |
Oops, something went wrong.