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

UpDate #7175

Open
wants to merge 18 commits into
base: dev/feature
Choose a base branch
from
Open

UpDate #7175

Show file tree
Hide file tree
Changes from 15 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
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/command/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/conditions/CondDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected String[] get(Event e, Date[] source) {
return get(source, new Getter<String, Date>() {
@Override
public String get(Date date) {
return format.format(new java.util.Date(date.getTimestamp()));
return format.format(date);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ExprUnixTicks extends SimplePropertyExpression<Date, Number> {
@Override
@Nullable
public Number convert(Date f) {
return f.getTimestamp() / 1000.0;
return f.getTime() / 1000.0;
}

@Override
Expand Down
172 changes: 85 additions & 87 deletions src/main/java/ch/njol/skript/util/Date.java
Original file line number Diff line number Diff line change
@@ -1,142 +1,140 @@
/**
* 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 <http://www.gnu.org/licenses/>.
*
* 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) {
return new Date(date.getTime());
}

Efnilite marked this conversation as resolved.
Show resolved Hide resolved
/**
* @author Peter Güttinger
*/
public class Date implements Comparable<Date>, 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 Date other))
return false;
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
final Date other = (Date) obj;
return timestamp == other.timestamp;
}


@Override
public String toString() {
return SkriptConfig.formatDate(timestamp);
}

}
30 changes: 30 additions & 0 deletions src/test/java/org/skriptlang/skript/test/tests/utils/DateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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() {
Date now = Date.now();
assertEquals(System.currentTimeMillis(), now.getTime());
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
}

@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);
Date date2 = new Date(1000);
assertEquals(date1, date2);
}

}
Loading