From 8fdcf82aa75abe296fe78ae79c62f7f9130e1763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Tue, 29 Jun 2021 19:19:22 +0200 Subject: [PATCH] Add TimeFormat and Timestamp (#1676) --- .../api/interactions/components/Button.java | 9 + .../net/dv8tion/jda/api/utils/TimeFormat.java | 327 ++++++++++++++++++ .../net/dv8tion/jda/api/utils/Timestamp.java | 155 +++++++++ 3 files changed, 491 insertions(+) create mode 100644 src/main/java/net/dv8tion/jda/api/utils/TimeFormat.java create mode 100644 src/main/java/net/dv8tion/jda/api/utils/Timestamp.java diff --git a/src/main/java/net/dv8tion/jda/api/interactions/components/Button.java b/src/main/java/net/dv8tion/jda/api/interactions/components/Button.java index b206eea2ca..815e2614f0 100644 --- a/src/main/java/net/dv8tion/jda/api/interactions/components/Button.java +++ b/src/main/java/net/dv8tion/jda/api/interactions/components/Button.java @@ -20,6 +20,7 @@ import net.dv8tion.jda.internal.interactions.ButtonImpl; import net.dv8tion.jda.internal.utils.Checks; +import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -117,6 +118,7 @@ public interface Button extends Component * @return New disabled button instance */ @Nonnull + @CheckReturnValue default Button asDisabled() { return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), true, getEmoji()); @@ -128,6 +130,7 @@ default Button asDisabled() * @return New enabled button instance */ @Nonnull + @CheckReturnValue default Button asEnabled() { return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), false, getEmoji()); @@ -142,6 +145,7 @@ default Button asEnabled() * @return New enabled/disabled button instance */ @Nonnull + @CheckReturnValue default Button withDisabled(boolean disabled) { return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), disabled, getEmoji()); @@ -156,6 +160,7 @@ default Button withDisabled(boolean disabled) * @return New button with emoji */ @Nonnull + @CheckReturnValue default Button withEmoji(@Nullable Emoji emoji) { return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), isDisabled(), emoji); @@ -173,6 +178,7 @@ default Button withEmoji(@Nullable Emoji emoji) * @return New button with the changed label */ @Nonnull + @CheckReturnValue default Button withLabel(@Nonnull String label) { Checks.notEmpty(label, "Label"); @@ -192,6 +198,7 @@ default Button withLabel(@Nonnull String label) * @return New button with the changed id */ @Nonnull + @CheckReturnValue default Button withId(@Nonnull String id) { Checks.notEmpty(id, "ID"); @@ -211,6 +218,7 @@ default Button withId(@Nonnull String id) * @return New button with the changed url */ @Nonnull + @CheckReturnValue default Button withUrl(@Nonnull String url) { Checks.notEmpty(url, "URL"); @@ -232,6 +240,7 @@ default Button withUrl(@Nonnull String url) * @return New button with the changed style */ @Nonnull + @CheckReturnValue default Button withStyle(@Nonnull ButtonStyle style) { Checks.notNull(style, "Style"); diff --git a/src/main/java/net/dv8tion/jda/api/utils/TimeFormat.java b/src/main/java/net/dv8tion/jda/api/utils/TimeFormat.java new file mode 100644 index 0000000000..41621268c5 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/api/utils/TimeFormat.java @@ -0,0 +1,327 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.api.utils; + +import net.dv8tion.jda.internal.utils.Checks; + +import javax.annotation.Nonnull; +import java.time.Duration; +import java.time.Instant; +import java.time.temporal.TemporalAccessor; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Utility enum used to provide different markdown styles for timestamps. + *
These can be used to represent a unix epoch timestamp in different formats. + * + *

These timestamps are rendered by the individual receiving discord client in a local timezone and language format. + * Each timestamp can be displayed with different {@link TimeFormat TimeFormats}. + * + *

Example

+ *
{@code
+ * channel.sendMessage("Current Time: " + TimeFormat.RELATIVE.now()).queue();
+ * channel.sendMessage("Uptime: " + TimeFormat.RELATIVE.format(getStartTime())).queue();
+ * }
+ */ +public enum TimeFormat +{ + /** Formats time as {@code 18:49} or {@code 6:49 PM} */ + TIME_SHORT("t"), + /** Formats time as {@code 18:49:26} or {@code 6:49:26 PM} */ + TIME_LONG("T"), + /** Formats date as {@code 16/06/2021} or {@code 06/16/2021} */ + DATE_SHORT("d"), + /** Formats date as {@code 16 June 2021} */ + DATE_LONG("D"), + /** Formats date and time as {@code 16 June 2021 18:49} or {@code June 16, 2021 6:49 PM} */ + DATE_TIME_SHORT("f"), + /** Formats date and time as {@code Wednesday, 16 June 2021 18:49} or {@code Wednesday, June 16, 2021 6:49 PM} */ + DATE_TIME_LONG("F"), + /** Formats date and time as relative {@code 18 minutes ago} or {@code 2 days ago} */ + RELATIVE("R"), + ; + + /** + * The default time format used when no style is provided. + */ + public static final TimeFormat DEFAULT = DATE_TIME_SHORT; + + /** + * {@link Pattern} used for {@link #parse(String)}. + * + *

Groups

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Javadoc is stupid, this is not a required tag
IndexNameDescription
0N/AThe entire timestamp markdown
1timeThe timestamp value as a unix epoch in second precision
2styleThe style used for displaying the timestamp (single letter flag)
+ * + * @see #parse(String) + */ + public static final Pattern MARKDOWN = Pattern.compile("-?\\d{1,17})(?::(?