forked from TheCurle/Camelot
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20610d6
commit 0293478
Showing
6 changed files
with
142 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Formats used by Camelot | ||
This page explains various formats used by Camelot when displaying or parsing inputs (i.e. durations). | ||
|
||
## Durations | ||
|
||
Durations are composed of sequences of numbers and unit specifiers without a space in between. The number will multiply the specified unit. | ||
Camelot supports the following units: | ||
- `n`: nanoseconds | ||
- `l`: milliseconds (1000000 nanoseconds) | ||
- `s`: seconds (1000 milliseconds) | ||
- `m`: minutes (60 seconds) | ||
- `h`: hours (60 minutes) | ||
- `d`: days (24 hours) | ||
- `w`: weeks (7 days) | ||
- `M`: months (30 days) | ||
- `y`: years (365 days) | ||
|
||
Unit specifiers are **case-sensitive** and unrecognized specifiers will be considered _minutes_. | ||
::: info | ||
While nanoseconds and milliseconds are supported by the parser, most commands will not support that level of precision and will round them to the next second. | ||
::: | ||
|
||
If a component of a duration is prefixed by a minus (`-`), it will be subtracted from the duration instead of added. | ||
::: info | ||
The format is not a maths equation. Parenthesis are not supported and the minus only applies to the first component after it. | ||
`-12m4s` subtracts 12 minutes and *adds* 4 seconds, while `-12m-4s` subtracts 12 minutes and 4 seconds. | ||
::: | ||
|
||
### Example durations | ||
- `12m45s`: 12 minutes and 45 seconds | ||
- `2y4M`: 2 years (730 days) and 4 months (120 days); 850 days in total | ||
- `1w2d`: one week and 2 days | ||
- `1d4h25m2s`: 1 day, 4 hours, 25 minutes and 2 seconds | ||
- `2w-4d`: 2 weeks minus 4 days; 10 days in total |
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 |
---|---|---|
|
@@ -26,3 +26,5 @@ j2html_version=1.6.0 | |
angus_mail_version=2.0.1 | ||
json_version=20231013 | ||
groovy_version=4.0.21 | ||
|
||
junit_version=5.10.0 |
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
41 changes: 41 additions & 0 deletions
41
src/test/java/net/neoforged/camelot/test/DateUtilsTest.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,41 @@ | ||
package net.neoforged.camelot.test; | ||
|
||
import net.neoforged.camelot.util.DateUtils; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DateUtilsTest { | ||
@Test | ||
void testSimpleParse() { | ||
Assertions.assertThat(DateUtils.decode("45m")) | ||
.hasMinutes(45); | ||
Assertions.assertThat(DateUtils.decode("2h")) | ||
.hasHours(2); | ||
Assertions.assertThat(DateUtils.decode("2M")) | ||
.hasDays(2 * 30); | ||
Assertions.assertThat(DateUtils.decode("1y")) | ||
.hasDays(365); | ||
} | ||
|
||
@Test | ||
void testMinuteFallback() { | ||
Assertions.assertThat(DateUtils.decode("2K")) | ||
.hasMinutes(2); | ||
} | ||
|
||
@Test | ||
void testMultipleParse() { | ||
Assertions.assertThat(DateUtils.getDurationFromInput("12m45s")) | ||
.hasSeconds(12 * 60 + 45); | ||
Assertions.assertThat(DateUtils.getDurationFromInput("12y4M2w")) | ||
.hasDays(12 * 365 + 4 * 30 + 2 * 7); | ||
} | ||
|
||
@Test | ||
void testNegativeParse() { | ||
Assertions.assertThat(DateUtils.getDurationFromInput("2w-4d")) | ||
.hasDays(10); | ||
Assertions.assertThat(DateUtils.getDurationFromInput("1h4s-4m-12s")) | ||
.hasSeconds(60 * 60 + 4 - 4 * 60 - 12); | ||
} | ||
} |