diff --git a/src/main/java/ch/njol/skript/command/ScriptCommand.java b/src/main/java/ch/njol/skript/command/ScriptCommand.java index eca22e78a68..e175d91e36f 100644 --- a/src/main/java/ch/njol/skript/command/ScriptCommand.java +++ b/src/main/java/ch/njol/skript/command/ScriptCommand.java @@ -573,11 +573,11 @@ public void setRemainingMilliseconds(UUID uuid, Event event, long milliseconds) public long getElapsedMilliseconds(UUID uuid, Event event) { Date lastUsage = getLastUsage(uuid, event); - return lastUsage == null ? 0 : new Date().getTimestamp() - lastUsage.getTimestamp(); + return lastUsage == null ? 0 : Date.now().getTime() - lastUsage.getTime(); } public void setElapsedMilliSeconds(UUID uuid, Event event, long milliseconds) { - Date date = new Date(); + Date date = Date.now(); date.subtract(new Timespan(milliseconds)); setLastUsage(uuid, event, date); } diff --git a/src/main/java/ch/njol/skript/conditions/CondDate.java b/src/main/java/ch/njol/skript/conditions/CondDate.java index ba2da40b81b..3843f6dee3e 100644 --- a/src/main/java/ch/njol/skript/conditions/CondDate.java +++ b/src/main/java/ch/njol/skript/conditions/CondDate.java @@ -73,7 +73,7 @@ public boolean check(final Event e) { final long now = System.currentTimeMillis(); return date.check(e, date -> delta.check(e, - timespan -> now - date.getTimestamp() >= timespan.getMilliSeconds() + timespan -> now - date.getTime() >= timespan.getAs(Timespan.TimePeriod.MILLISECOND) ), isNegated()); } diff --git a/src/main/java/ch/njol/skript/expressions/ExprFormatDate.java b/src/main/java/ch/njol/skript/expressions/ExprFormatDate.java index 63a755cdd29..32440c79cdd 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprFormatDate.java +++ b/src/main/java/ch/njol/skript/expressions/ExprFormatDate.java @@ -118,7 +118,7 @@ protected String[] get(Event e, Date[] source) { return get(source, new Getter() { @Override public String get(Date date) { - return format.format(new java.util.Date(date.getTimestamp())); + return format.format(date); } }); } diff --git a/src/main/java/ch/njol/skript/expressions/ExprUnixTicks.java b/src/main/java/ch/njol/skript/expressions/ExprUnixTicks.java index 205e8a1ac7e..c7606dd302b 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprUnixTicks.java +++ b/src/main/java/ch/njol/skript/expressions/ExprUnixTicks.java @@ -40,7 +40,7 @@ public class ExprUnixTicks extends SimplePropertyExpression { @Override @Nullable public Number convert(Date f) { - return f.getTimestamp() / 1000.0; + return f.getTime() / 1000.0; } @Override diff --git a/src/main/java/ch/njol/skript/util/Date.java b/src/main/java/ch/njol/skript/util/Date.java index 50e2fd88fe9..f527fd74584 100644 --- a/src/main/java/ch/njol/skript/util/Date.java +++ b/src/main/java/ch/njol/skript/util/Date.java @@ -1,142 +1,142 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.util; +import ch.njol.skript.SkriptConfig; +import ch.njol.yggdrasil.YggdrasilSerializable; +import org.jetbrains.annotations.Nullable; + import java.util.TimeZone; -import org.jetbrains.annotations.Nullable; +public class Date extends java.util.Date implements YggdrasilSerializable { -import ch.njol.skript.SkriptConfig; -import ch.njol.yggdrasil.YggdrasilSerializable; + /** + * Get a new Date with the current time + * + * @return New date with the current time + */ + public static Date now() { + return new Date(); + } + + /** + * Converts a {@link java.util.Date} to a {@link Date}. + * + * @param date The {@link java.util.Date} to convert. + * @return The converted date. + */ + public static Date fromJavaDate(java.util.Date date) { + if (date instanceof Date ours) + return ours; + return new Date(date.getTime()); + } -/** - * @author Peter Güttinger - */ -public class Date implements Comparable, YggdrasilSerializable { - /** * Timestamp. Should always be in computer time/UTC/GMT+0. */ private long timestamp; - + + /** + * Creates a new Date with the current time. + */ public Date() { - this(System.currentTimeMillis()); - } - - public Date(final long timestamp) { - this.timestamp = timestamp; - } - - public Date(final long timestamp, final TimeZone zone) { - final long offset = zone.getOffset(timestamp); - this.timestamp = timestamp - offset; + super(System.currentTimeMillis()); } - + /** - * Get a new Date with the current time + * Creates a new Date with the provided timestamp. * - * @return New date with the current time + * @param timestamp The timestamp in milliseconds. */ - public static Date now() { - return new Date(System.currentTimeMillis()); - } - - public Timespan difference(final Date other) { - return new Timespan(Math.abs(timestamp - other.timestamp)); - } - - @Override - public int compareTo(final @Nullable Date other) { - final long d = other == null ? timestamp : timestamp - other.timestamp; - return d < 0 ? -1 : d > 0 ? 1 : 0; + public Date(long timestamp) { + super(timestamp); } - - @Override - public String toString() { - return SkriptConfig.formatDate(timestamp); - } - + /** - * Get the timestamp of this date + * Creates a new Date with the provided timestamp and timezone. * - * @return The timestamp in milliseconds + * @param timestamp The timestamp in milliseconds. + * @param zone The timezone to use. */ - public long getTimestamp() { - return timestamp; + public Date(long timestamp, TimeZone zone) { + super(timestamp - zone.getOffset(timestamp)); } - + /** * Add a {@link Timespan} to this date * - * @param span Timespan to add + * @param other Timespan to add */ - public void add(final Timespan span) { - timestamp += span.getMilliSeconds(); + public void add(Timespan other) { + timestamp += other.getAs(Timespan.TimePeriod.MILLISECOND); + setTime(timestamp); } - + /** * Subtract a {@link Timespan} from this date * - * @param span Timespan to subtract + * @param other Timespan to subtract */ - public void subtract(final Timespan span) { - timestamp -= span.getMilliSeconds(); + public void subtract(Timespan other) { + timestamp -= other.getAs(Timespan.TimePeriod.MILLISECOND); + setTime(timestamp); } - + + /** + * Returns the difference between this date and another date as a {@link Timespan}. + * + * @param other The other date. + * @return The difference between the provided dates as a {@link Timespan}. + */ + public Timespan difference(Date other) { + return new Timespan(Math.abs(timestamp - other.timestamp)); + } + /** * Get a new instance of this Date with the added timespan * - * @param span Timespan to add to this Date + * @param other Timespan to add to this Date * @return New Date with the added timespan */ - public Date plus(Timespan span) { - return new Date(timestamp + span.getMilliSeconds()); + public Date plus(Timespan other) { + return new Date(timestamp + other.getAs(Timespan.TimePeriod.MILLISECOND)); } /** * Get a new instance of this Date with the subtracted timespan * - * @param span Timespan to subtract from this Date + * @param other Timespan to subtract from this Date * @return New Date with the subtracted timespan */ - public Date minus(Timespan span) { - return new Date(timestamp - span.getMilliSeconds()); + public Date minus(Timespan other) { + return new Date(timestamp - other.getAs(Timespan.TimePeriod.MILLISECOND)); } - + + /** + * @deprecated Use {@link #getTime()} instead. + */ + @Deprecated(forRemoval = true) + public long getTimestamp() { + return timestamp; + } + @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (int) (timestamp ^ (timestamp >>> 32)); - return result; + return 31 + Long.hashCode(timestamp); } - + @Override - public boolean equals(final @Nullable Object obj) { + public boolean equals(@Nullable Object obj) { if (this == obj) return true; if (obj == null) return false; - if (!(obj instanceof Date)) + if (!(obj instanceof java.util.Date other)) return false; - final Date other = (Date) obj; - return timestamp == other.timestamp; + return getTime() == other.getTime(); } - + + @Override + public String toString() { + return SkriptConfig.formatDate(timestamp); + } + } diff --git a/src/test/java/org/skriptlang/skript/test/tests/utils/DateTest.java b/src/test/java/org/skriptlang/skript/test/tests/utils/DateTest.java new file mode 100644 index 00000000000..2f8f8f1d8b7 --- /dev/null +++ b/src/test/java/org/skriptlang/skript/test/tests/utils/DateTest.java @@ -0,0 +1,29 @@ +package org.skriptlang.skript.test.tests.utils; + +import ch.njol.skript.util.Date; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class DateTest { + + @Test + public void testNow() { + assertEquals(System.currentTimeMillis(), Date.now().getTime()); + } + + @Test + public void testFromJavaDate() { + java.util.Date javaDate = new java.util.Date(); + Date date = Date.fromJavaDate(javaDate); + assertEquals(javaDate.getTime(), date.getTime()); + } + + @Test + public void testEquals() { + Date date1 = new Date(1000); + java.util.Date date2 = new java.util.Date(1000); + assertEquals(date1, date2); + } + +}