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 19 commits into
base: dev/feature
Choose a base branch
from
Open

UpDate #7175

Show file tree
Hide file tree
Changes from 6 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
173 changes: 95 additions & 78 deletions src/main/java/ch/njol/skript/util/Date.java
Original file line number Diff line number Diff line change
@@ -1,142 +1,159 @@
/**
* 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 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;

/**
* @author Peter Güttinger
*/
public class Date implements Comparable<Date>, 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
/**
* 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) {

/**
* Creates a new Date with the provided timestamp.
*
* @param timestamp The timestamp in milliseconds.
*/
public Date(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
* Creates a new Date with the provided timestamp and timezone.
*
* @return New date with the current time
* @param timestamp The timestamp in milliseconds.
* @param zone The timezone to use.
*/
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;
}

@Override
public String toString() {
return SkriptConfig.formatDate(timestamp);
public Date(long timestamp, TimeZone zone) {
long offset = zone.getOffset(timestamp);
this.timestamp = timestamp - offset;
}

/**
* Get the timestamp of this date
* Returns the difference between this date and another date as a {@link Timespan}.
*
* @return The timestamp in milliseconds
* @param other The other date.
* @return The difference between the provided dates as a {@link Timespan}.
*/
public long getTimestamp() {
return timestamp;
public Timespan difference(Date other) {
return new Timespan(Math.abs(timestamp - other.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);
}

/**
* 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);
}

/**
* 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));
}

/**
* Get the timestamp of this date
*
* @return The timestamp in milliseconds
*/
public long getTimestamp() {
return timestamp;
}

/**
* Convert this Date to a {@link java.util.Date}.
*
* @return The converted Date.
*/
public java.util.Date toJavaDate() {
return new java.util.Date(timestamp);
}

@Override
public int hashCode() {
final int prime = 31;
int prime = 31;
int result = 1;
result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
result = prime * result + Long.hashCode(timestamp);
return result;
}

@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 int compareTo(@Nullable Date other) {
long delta = other == null ? timestamp : timestamp - other.timestamp;
return delta < 0 ? -1 : delta > 0 ? 1 : 0;
}

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

}
32 changes: 32 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,32 @@
package org.skriptlang.skript.test.tests.utils;

import ch.njol.skript.util.Date;
import org.junit.Test;

import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

public class DateTest {

@Test
public void testNow() {
Date now = Date.now();
assertEquals(System.currentTimeMillis(), now.getTimestamp());
}

@Test
public void testFromJavaDate() {
java.util.Date javaDate = new java.util.Date();
Date date = Date.fromJavaDate(javaDate);
assertEquals(javaDate.getTime(), date.getTimestamp());
}

@Test
public void testToJavaDate() {
Date date = Date.now();
java.util.Date javaDate = date.toJavaDate();
assertEquals(date.getTimestamp(), javaDate.getTime());
}

}
Loading