Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TimeFormat and Timestamp #1676

Merged
merged 8 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/TimeFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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;

public enum TimeFormat
{
/** Formats time as {@code 18:49} or {@code 6:49 PM} */
TIME_SHORT("t"),
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
/** 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 16 June 2021 6:49 PM} */
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
DATE_TIME_SHORT("f"),
/** Formats date and time as {@code Wednesday, 16 June 2021 18:49} or {@code Wednesday, 16 June 2021 6:49 PM} */
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
DATE_TIME_LONG("F"),
/** Formats date and time as relative {@code 18 minutes ago} or {@code 2 days ago} */
RELATIVE("R"),
;

public static final TimeFormat DEFAULT = DATE_TIME_SHORT;
public static final Pattern MARKDOWN = Pattern.compile("<t:(?<time>-?\\d{1,17})(?::(?<format>[tTdDfFR]))?>");

private final String flag;

TimeFormat(String flag)
{
this.flag = flag;
}

@Nonnull
public String getFlag()
{
return flag;
}

@Nonnull
public static TimeFormat fromKey(@Nonnull String key)
{
for (TimeFormat format : values())
{
if (format.flag.equals(key))
return format;
}
return DEFAULT;
}

@Nonnull
public static Timestamp parse(@Nonnull String markdown)
{
Checks.notNull(markdown, "Markdown");
Matcher matcher = MARKDOWN.matcher(markdown);
if (!matcher.find())
throw new IllegalArgumentException("Invalid markdown format! Provided: " + markdown);
String format = matcher.group("format");
return new Timestamp(format == null ? DEFAULT : fromKey(format), Long.parseLong(matcher.group("time")) * 1000);
}

@Nonnull
public String format(@Nonnull TemporalAccessor temporal)
{
Checks.notNull(temporal, "Temporal");
long timestamp = Instant.from(temporal).toEpochMilli();
return format(timestamp);
}

@Nonnull
public String format(long timestamp)
{
return "<t:" + timestamp / 1000 + ":" + flag + ">";
}

@Nonnull
public Timestamp atInstant(@Nonnull Instant instant)
{
Checks.notNull(instant, "Instant");
return new Timestamp(this, instant.toEpochMilli());
}

@Nonnull
public Timestamp atTimestamp(long timestamp)
{
return new Timestamp(this, timestamp);
}

@Nonnull
public Timestamp now()
{
return new Timestamp(this, System.currentTimeMillis());
}

@Nonnull
public Timestamp after(@Nonnull Duration duration)
{
return now().plus(duration);
}

@Nonnull
public Timestamp after(long millis)
{
return now().plus(millis);
}

@Nonnull
public Timestamp before(@Nonnull Duration duration)
{
return now().minus(duration);
}

@Nonnull
public Timestamp before(long millis)
{
return now().minus(millis);
}
}
85 changes: 85 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/Timestamp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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;

public class Timestamp
{
private final TimeFormat format;
private final long timestamp;

public Timestamp(TimeFormat format, long timestamp)
{
Checks.notNull(format, "TimeFormat");
this.format = format;
this.timestamp = timestamp;
}

@Nonnull
public TimeFormat getFormat()
{
return format;
}

public long getTimestamp()
{
return timestamp;
}

@Nonnull
public Instant toInstant()
{
return Instant.ofEpochMilli(timestamp);
}

@Nonnull
public Timestamp plus(long millis)
{
return new Timestamp(format, timestamp + millis);
}

@Nonnull
public Timestamp plus(@Nonnull Duration duration)
{
Checks.notNull(duration, "Duration");
return plus(duration.toMillis());
}

@Nonnull
public Timestamp minus(long millis)
{
return new Timestamp(format, timestamp - millis);
}

@Nonnull
public Timestamp minus(@Nonnull Duration duration)
{
Checks.notNull(duration, "Duration");
return plus(duration.toMillis());
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String toString()
{
return "<t:" + timestamp / 1000 + ":" + format.getFlag() + ">";
}
}