Skip to content

Commit 9ab921a

Browse files
authored
Merge pull request #416 from cquiroz/buildtest
Duration methods
2 parents f5fd248 + d2da535 commit 9ab921a

File tree

9 files changed

+421
-10
lines changed

9 files changed

+421
-10
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-2/org/threeten/bp/temporal/IsoFields.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ object IsoFields {
234234
if (resolverStyle eq ResolverStyle.LENIENT) {
235235
val qoy: Long = qoyLong
236236
date = LocalDate.of(y, 1, 1)
237-
date = date.plusMonths(Math.multiplyExact(Math.subtractExact(qoy, 1), 3))
237+
date = date.plusMonths(Math.multiplyExact(Math.subtractExact(qoy, 1), 3L))
238238
date = date.plusDays(Math.subtractExact(doq, 1))
239239
} else {
240240
val qoy: Int = QUARTER_OF_YEAR.range.checkValidIntValue(qoyLong, QUARTER_OF_YEAR)

core/shared/src/main/scala-3/org/threeten/bp/temporal/IsoFields.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ object IsoFields {
324324
if (resolverStyle eq ResolverStyle.LENIENT) {
325325
val qoy: Long = qoyLong
326326
date = LocalDate.of(y, 1, 1)
327-
date = date.plusMonths(Math.multiplyExact(Math.subtractExact(qoy, 1), 3))
327+
date = date.plusMonths(Math.multiplyExact(Math.subtractExact(qoy, 1), 3L))
328328
date = date.plusDays(Math.subtractExact(doq, 1))
329329
} else {
330330
val qoy: Int = Field.QUARTER_OF_YEAR.range.checkValidIntValue(qoyLong, Field.QUARTER_OF_YEAR)

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

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ 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
46+
import org.threeten.bp.LocalTime.MINUTES_PER_HOUR
4547
import org.threeten.bp.format.DateTimeParseException
4648
import org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND
4749
import org.threeten.bp.temporal.ChronoUnit
@@ -925,7 +927,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
925927
def multipliedBy(multiplicand: Long): Duration =
926928
if (multiplicand == 0) Duration.ZERO
927929
else if (multiplicand == 1) this
928-
else Duration.create(toSeconds.multiply(BigDecimal.valueOf(multiplicand)))
930+
else Duration.create(toSecondsBD.multiply(BigDecimal.valueOf(multiplicand)))
929931

930932
/**
931933
* Returns a copy of this duration divided by the specified value.
@@ -944,7 +946,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
944946
def dividedBy(divisor: Long): Duration =
945947
if (divisor == 0) throw new ArithmeticException("Cannot divide by zero")
946948
else if (divisor == 1) this
947-
else Duration.create(toSeconds.divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN))
949+
else Duration.create(toSecondsBD.divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN))
948950

949951
/**
950952
* Converts this duration to the total length in seconds and fractional nanoseconds expressed as a
@@ -953,9 +955,37 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
953955
* @return
954956
* the total length of the duration in seconds, with a scale of 9, not null
955957
*/
956-
private def toSeconds: BigDecimal =
958+
private def toSecondsBD: BigDecimal =
957959
BigDecimal.valueOf(seconds).add(BigDecimal.valueOf(nanos.toLong, 9))
958960

961+
/**
962+
* Gets the number of seconds in this duration.
963+
*
964+
* This returns the total number of whole seconds in the duration.
965+
*
966+
* This instance is immutable and unaffected by this method call.
967+
*
968+
* @return
969+
* the whole seconds part of the length of the duration, positive or negative
970+
*
971+
* @since 9
972+
*/
973+
def toSeconds: Long = seconds
974+
975+
/**
976+
* Extracts the number of seconds part in the duration.
977+
*
978+
* This returns the remaining seconds when dividing toSeconds() by seconds in a minute. This is
979+
* based on the standard definition of a minute as 60 seconds.
980+
*
981+
* This instance is immutable and unaffected by this method call.
982+
*
983+
* @return
984+
* the number of seconds parts in the duration, may be negative
985+
* @since 9
986+
*/
987+
def toSecondsPart: Int = (toSeconds % SECONDS_PER_MINUTE).toInt
988+
959989
/**
960990
* Returns a copy of this duration with the length negated.
961991
*
@@ -1065,6 +1095,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
10651095
*/
10661096
def toDays: Long = seconds / SECONDS_PER_DAY
10671097

1098+
/**
1099+
* Extracts the number of days in the duration.
1100+
*
1101+
* This returns the total number of days in the duration by dividing the number of seconds by
1102+
* 86400. This is based on the standard definition of a day as 24 hours.
1103+
*
1104+
* This instance is immutable and unaffected by this method call.
1105+
*
1106+
* @return
1107+
* the number of days in the duration, may be negative
1108+
* @since 9
1109+
*/
1110+
def toDaysPart: Long = seconds / SECONDS_PER_DAY
1111+
10681112
/**
10691113
* Gets the number of hours in this duration.
10701114
*
@@ -1078,6 +1122,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
10781122
*/
10791123
def toHours: Long = seconds / SECONDS_PER_HOUR
10801124

1125+
/**
1126+
* Extracts the number of hours part in the duration.
1127+
*
1128+
* This returns the number of remaining hours when dividing toHours() by hours in a day. This is
1129+
* based on the standard definition of a day as 24 hours.
1130+
*
1131+
* This instance is immutable and unaffected by this method call.
1132+
*
1133+
* @return
1134+
* the number of hours part in the duration, may be negative
1135+
* @since 9
1136+
*/
1137+
def toHoursPart: Int = (toHours % HOURS_PER_DAY).toInt
1138+
10811139
/**
10821140
* Gets the number of minutes in this duration.
10831141
*
@@ -1091,6 +1149,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
10911149
*/
10921150
def toMinutes: Long = seconds / SECONDS_PER_MINUTE
10931151

1152+
/**
1153+
* Extracts the number of minutes part in the duration.
1154+
*
1155+
* This returns the number of remaining minutes when dividing toMinutes() by minutes in an hour.
1156+
* This is based on the standard definition of an hour as 60 minutes.
1157+
*
1158+
* This instance is immutable and unaffected by this method call.
1159+
*
1160+
* @return
1161+
* the number of minutes parts in the duration, may be negative
1162+
* @since 9
1163+
*/
1164+
def toMinutesPart: Int = (toMinutes % MINUTES_PER_HOUR).toInt
1165+
10941166
/**
10951167
* Converts this duration to the total length in milliseconds.
10961168
*
@@ -1111,6 +1183,22 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11111183
Math.addExact(result, nanos.toLong / Duration.NANOS_PER_MILLI)
11121184
}
11131185

1186+
/**
1187+
* Extracts the number of milliseconds part of the duration.
1188+
*
1189+
* This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000. The
1190+
* length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds
1191+
* part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total
1192+
* duration is defined by calling getNano() and getSeconds().
1193+
*
1194+
* This instance is immutable and unaffected by this method call.
1195+
*
1196+
* @return
1197+
* the number of milliseconds part of the duration.
1198+
* @since 9
1199+
*/
1200+
def toMillisPart: Int = (nanos / Duration.NANOS_PER_MILLI).toInt
1201+
11141202
/**
11151203
* Converts this duration to the total length in nanoseconds expressed as a {@code long}.
11161204
*
@@ -1127,6 +1215,21 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11271215
Math.addExact(result, nanos.toLong)
11281216
}
11291217

1218+
/**
1219+
* Get the nanoseconds part within seconds of the duration.
1220+
*
1221+
* The length of the duration is stored using two fields - seconds and nanoseconds. The
1222+
* nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in
1223+
* seconds. The total duration is defined by calling getNano() and getSeconds().
1224+
*
1225+
* This instance is immutable and unaffected by this method call.
1226+
*
1227+
* @return
1228+
* the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
1229+
* @since 9
1230+
*/
1231+
def toNanosPart: Int = nanos
1232+
11301233
/**
11311234
* Compares this duration to the specified {@code Duration}.
11321235
*

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)