Skip to content

Commit 0eda72d

Browse files
committed
Add some Duration methods added in Java 9
1 parent 0a3a265 commit 0eda72d

File tree

7 files changed

+275
-6
lines changed

7 files changed

+275
-6
lines changed

.envrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use flake
2+
layout node
3+
eval "$shellHook"

.github/workflows/scala.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Set up JDK 8
1313
uses: olafurpg/setup-scala@v13
1414
with:
15-
java-version: 1.8
15+
java-version: 1.9
1616
- name: Cache scala dependencies
1717
uses: coursier/cache-action@v6
1818
# - name: Checking your code format
@@ -37,7 +37,7 @@ jobs:
3737
- name: Setup Scala and Java
3838
uses: olafurpg/setup-scala@v13
3939
with:
40-
java-version: 1.8
40+
java-version: 1.9
4141
- name: Cache scala dependencies
4242
uses: coursier/cache-action@v6
4343
- name: Run tests

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ tzdb/js/src/main/resources/
3939
.bloop/
4040
.metals/
4141
project/metals.sbt
42+
.direnv/
43+
.bsp/

core/shared/src/main/scala/org/threeten/bp/Duration.scala

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import java.util.regex.Pattern
4242
import org.threeten.bp.LocalTime.SECONDS_PER_DAY
4343
import org.threeten.bp.LocalTime.SECONDS_PER_HOUR
4444
import org.threeten.bp.LocalTime.SECONDS_PER_MINUTE
45+
import org.threeten.bp.LocalTime.HOURS_PER_DAY
4546
import org.threeten.bp.format.DateTimeParseException
4647
import org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND
4748
import org.threeten.bp.temporal.ChronoUnit
@@ -925,7 +926,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
925926
def multipliedBy(multiplicand: Long): Duration =
926927
if (multiplicand == 0) Duration.ZERO
927928
else if (multiplicand == 1) this
928-
else Duration.create(toSeconds.multiply(BigDecimal.valueOf(multiplicand)))
929+
else Duration.create(toSecondsBD.multiply(BigDecimal.valueOf(multiplicand)))
929930

930931
/**
931932
* Returns a copy of this duration divided by the specified value.
@@ -944,7 +945,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
944945
def dividedBy(divisor: Long): Duration =
945946
if (divisor == 0) throw new ArithmeticException("Cannot divide by zero")
946947
else if (divisor == 1) this
947-
else Duration.create(toSeconds.divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN))
948+
else Duration.create(toSecondsBD.divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN))
948949

949950
/**
950951
* Converts this duration to the total length in seconds and fractional nanoseconds expressed as a
@@ -953,9 +954,23 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
953954
* @return
954955
* the total length of the duration in seconds, with a scale of 9, not null
955956
*/
956-
private def toSeconds: BigDecimal =
957+
private def toSecondsBD: BigDecimal =
957958
BigDecimal.valueOf(seconds).add(BigDecimal.valueOf(nanos.toLong, 9))
958959

960+
/**
961+
* Gets the number of seconds in this duration.
962+
*
963+
* This returns the total number of whole seconds in the duration.
964+
*
965+
* This instance is immutable and unaffected by this method call.
966+
*
967+
* @return
968+
* the whole seconds part of the length of the duration, positive or negative
969+
*
970+
* @since 9
971+
*/
972+
def toSeconds: Long = seconds
973+
959974
/**
960975
* Returns a copy of this duration with the length negated.
961976
*
@@ -1065,6 +1080,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
10651080
*/
10661081
def toDays: Long = seconds / SECONDS_PER_DAY
10671082

1083+
/**
1084+
* Extracts the number of days in the duration.
1085+
*
1086+
* This returns the total number of days in the duration by dividing the number of seconds by
1087+
* 86400. This is based on the standard definition of a day as 24 hours.
1088+
*
1089+
* This instance is immutable and unaffected by this method call.
1090+
*
1091+
* @return
1092+
* the number of days in the duration, may be negative
1093+
* @since 9
1094+
*/
1095+
def toDaysPart: Long = seconds / SECONDS_PER_DAY
1096+
10681097
/**
10691098
* Gets the number of hours in this duration.
10701099
*
@@ -1078,6 +1107,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
10781107
*/
10791108
def toHours: Long = seconds / SECONDS_PER_HOUR
10801109

1110+
/**
1111+
* Extracts the number of hours part in the duration.
1112+
*
1113+
* This returns the number of remaining hours when dividing toHours() by hours in a day. This is
1114+
* based on the standard definition of a day as 24 hours.
1115+
*
1116+
* This instance is immutable and unaffected by this method call.
1117+
*
1118+
* @return
1119+
* the number of hours part in the duration, may be negative
1120+
* @since 9
1121+
*/
1122+
def toHoursPart: Int = (toHours / HOURS_PER_DAY).toInt
1123+
10811124
/**
10821125
* Gets the number of minutes in this duration.
10831126
*

flake.lock

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
inputs = {
3+
typelevel-nix.url = "github:typelevel/typelevel-nix";
4+
nixpkgs.follows = "typelevel-nix/nixpkgs";
5+
flake-utils.follows = "typelevel-nix/flake-utils";
6+
};
7+
8+
outputs = { self, nixpkgs, flake-utils, typelevel-nix }:
9+
flake-utils.lib.eachDefaultSystem (system:
10+
let
11+
pkgs-x86_64 = import nixpkgs { system = "x86_64-darwin"; };
12+
scala-cli-overlay = final: prev: { scala-cli = pkgs-x86_64.scala-cli; };
13+
pkgs = import nixpkgs {
14+
inherit system;
15+
overlays = [ typelevel-nix.overlay scala-cli-overlay ];
16+
};
17+
in {
18+
devShell = pkgs.devshell.mkShell {
19+
imports = [ typelevel-nix.typelevelShell ];
20+
packages = [
21+
pkgs.nodePackages.vscode-langservers-extracted
22+
];
23+
typelevelShell = {
24+
nodejs.enable = true;
25+
jdk.package = pkgs.jdk17;
26+
};
27+
};
28+
}
29+
30+
);
31+
}

0 commit comments

Comments
 (0)