-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init commit * add test * give up * extend date * remove deprecated method usage * remove unnecessary methods * hmm * fix comparisons * update hashcode * Fix serialisation equality issue. Authored-by: Moderocky <admin@moderocky.com> --------- Co-authored-by: Moderocky <admin@moderocky.com>
- Loading branch information
Showing
8 changed files
with
162 additions
and
83 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
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
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 |
---|---|---|
@@ -1,124 +1,135 @@ | ||
package ch.njol.skript.util; | ||
|
||
import java.util.TimeZone; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.SkriptConfig; | ||
import ch.njol.yggdrasil.YggdrasilSerializable; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.TimeZone; | ||
|
||
public class Date extends java.util.Date implements YggdrasilSerializable { | ||
|
||
/** | ||
* @author Peter Güttinger | ||
*/ | ||
public class Date implements Comparable<Date>, YggdrasilSerializable { | ||
|
||
/** | ||
* Timestamp. Should always be in computer time/UTC/GMT+0. | ||
*/ | ||
private long timestamp; | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* Get a new Date with the current time | ||
* | ||
* @return New date with the current time | ||
*/ | ||
public static Date now() { | ||
return new Date(System.currentTimeMillis()); | ||
return new Date(); | ||
} | ||
|
||
public Timespan difference(final Date other) { | ||
return new Timespan(Math.abs(timestamp - other.timestamp)); | ||
|
||
/** | ||
* 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()); | ||
} | ||
|
||
@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; | ||
|
||
/** | ||
* Creates a new Date with the current time. | ||
*/ | ||
public Date() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return SkriptConfig.formatDate(timestamp); | ||
|
||
/** | ||
* Creates a new Date with the provided timestamp. | ||
* | ||
* @param timestamp The timestamp in milliseconds. | ||
*/ | ||
public Date(long timestamp) { | ||
super(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.getAs(Timespan.TimePeriod.MILLISECOND); | ||
public void add(Timespan other) { | ||
setTime(getTime() + other.getAs(Timespan.TimePeriod.MILLISECOND)); | ||
} | ||
|
||
/** | ||
* Subtract a {@link Timespan} from this date | ||
* | ||
* @param span Timespan to subtract | ||
* @param other Timespan to subtract | ||
*/ | ||
public void subtract(Timespan other) { | ||
setTime(getTime() - other.getAs(Timespan.TimePeriod.MILLISECOND)); | ||
} | ||
|
||
/** | ||
* 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 void subtract(final Timespan span) { | ||
timestamp -= span.getAs(Timespan.TimePeriod.MILLISECOND); | ||
public Timespan difference(Date other) { | ||
return new Timespan(Math.abs(getTime() - other.getTime())); | ||
} | ||
|
||
/** | ||
* 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.getAs(Timespan.TimePeriod.MILLISECOND)); | ||
public Date plus(Timespan other) { | ||
return new Date(getTime() + 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.getAs(Timespan.TimePeriod.MILLISECOND)); | ||
public Date minus(Timespan other) { | ||
return new Date(getTime() - other.getAs(Timespan.TimePeriod.MILLISECOND)); | ||
} | ||
|
||
/** | ||
* @deprecated Use {@link #getTime()} instead. | ||
*/ | ||
@Deprecated(forRemoval = true) | ||
public long getTimestamp() { | ||
return getTime(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + (int) (timestamp ^ (timestamp >>> 32)); | ||
return result; | ||
return 31 + Long.hashCode(getTime()); | ||
} | ||
|
||
@Override | ||
public boolean equals(final @Nullable Object obj) { | ||
if (this == obj) | ||
return true; | ||
public boolean equals(@Nullable Object obj) { | ||
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; | ||
if (this == obj) | ||
return true; | ||
return getTime() == other.getTime(); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return SkriptConfig.formatDate(getTime()); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/org/skriptlang/skript/test/tests/utils/DateTest.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,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); | ||
} | ||
|
||
} |