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

Deprecate CurrentTimeUTC #870

Merged
merged 2 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 13 additions & 12 deletions src/freenet/client/async/ClientRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import freenet.node.SendableRequest;
import freenet.node.useralerts.SimpleUserAlert;
import freenet.node.useralerts.UserAlert;
import freenet.support.CurrentTimeUTC;
import freenet.support.Logger;
import freenet.support.Logger.LogLevel;
import freenet.support.io.ResumeFailedException;
Expand Down Expand Up @@ -125,10 +124,12 @@ public int hashCode() {
protected int totalBlocks;
/** Number of blocks we have successfully completed a fetch/put for. */
protected int successfulBlocks;
/**
* ATTENTION: This may be null for very old databases.
* @see #getLatestSuccess() Explanation of the content and especially the default value. */
protected Date latestSuccess = CurrentTimeUTC.get();
/**
* ATTENTION: This may be null for very old databases.
*
* @see #getLatestSuccess() Explanation of the content and especially the default value.
*/
protected Date latestSuccess = new Date();
/** Number of blocks which have failed. */
protected int failedBlocks;
/** Number of blocks which have failed fatally. */
Expand Down Expand Up @@ -174,8 +175,8 @@ public Date getLatestFailure() {
protected synchronized void resetBlocks() {
totalBlocks = 0;
successfulBlocks = 0;
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
latestSuccess = CurrentTimeUTC.get();
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
latestSuccess = new Date();
failedBlocks = 0;
fatallyFailedBlocks = 0;
latestFailure = null;
Expand Down Expand Up @@ -242,7 +243,7 @@ public void completedBlock(boolean dontNotify, ClientContext context) {
synchronized(this) {
if(cancelled) return;
successfulBlocks++;
latestSuccess = CurrentTimeUTC.get();
latestSuccess = new Date();
}
if(dontNotify) return;
notifyClients(context);
Expand All @@ -254,7 +255,7 @@ public void completedBlock(boolean dontNotify, ClientContext context) {
public void failedBlock(boolean dontNotify, ClientContext context) {
synchronized(this) {
failedBlocks++;
latestFailure = CurrentTimeUTC.get();
latestFailure = new Date();
}
if(!dontNotify)
notifyClients(context);
Expand All @@ -269,7 +270,7 @@ public void failedBlock(ClientContext context) {
public void fatallyFailedBlock(ClientContext context) {
synchronized(this) {
fatallyFailedBlocks++;
latestFailure = CurrentTimeUTC.get();
latestFailure = new Date();
}
notifyClients(context);
}
Expand Down Expand Up @@ -331,8 +332,8 @@ protected void clearCountersOnRestart() {
this.minSuccessBlocks = 0;
this.sentToNetwork = false;
this.successfulBlocks = 0;
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
this.latestSuccess = CurrentTimeUTC.get();
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
this.latestSuccess = new Date();
this.totalBlocks = 0;
}

Expand Down
3 changes: 1 addition & 2 deletions src/freenet/client/events/SplitfileProgressEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Date;

import freenet.client.async.ClientRequester;
import freenet.support.CurrentTimeUTC;
import freenet.support.LogThresholdCallback;
import freenet.support.Logger;
import freenet.support.Logger.LogLevel;
Expand Down Expand Up @@ -61,7 +60,7 @@ protected SplitfileProgressEvent() {
totalBlocks = 0;
succeedBlocks = 0;
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
latestSuccess = CurrentTimeUTC.get();
latestSuccess = new Date();
failedBlocks = 0;
fatallyFailedBlocks = 0;
latestFailure = null;
Expand Down
3 changes: 1 addition & 2 deletions src/freenet/clients/fcp/ClientGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import freenet.crypt.HashResult;
import freenet.keys.FreenetURI;
import freenet.node.NodeClientCore;
import freenet.support.CurrentTimeUTC;
import freenet.support.LogThresholdCallback;
import freenet.support.Logger;
import freenet.support.Logger.LogLevel;
Expand Down Expand Up @@ -931,7 +930,7 @@ synchronized RequestStatus getStatus() {
boolean totalFinalized = false;
int total = 0, min = 0, fetched = 0, fatal = 0, failed = 0;
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
Date latestSuccess = CurrentTimeUTC.get();
Date latestSuccess = new Date();
Date latestFailure = null;

if(progressPending != null) {
Expand Down
3 changes: 1 addition & 2 deletions src/freenet/clients/fcp/ClientPut.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import freenet.keys.FreenetURI;
import freenet.node.NodeClientCore;
import freenet.support.Base64;
import freenet.support.CurrentTimeUTC;
import freenet.support.IllegalBase64Exception;
import freenet.support.LogThresholdCallback;
import freenet.support.Logger;
Expand Down Expand Up @@ -525,7 +524,7 @@ RequestStatus getStatus() {

int total=0, min=0, fetched=0, fatal=0, failed=0;
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
Date latestSuccess = CurrentTimeUTC.get();
Date latestSuccess = new Date();
Date latestFailure = null;
boolean totalFinalized = false;

Expand Down
3 changes: 1 addition & 2 deletions src/freenet/clients/fcp/ClientPutDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import freenet.clients.fcp.RequestIdentifier.RequestType;
import freenet.keys.FreenetURI;
import freenet.node.NodeClientCore;
import freenet.support.CurrentTimeUTC;
import freenet.support.LogThresholdCallback;
import freenet.support.Logger;
import freenet.support.Logger.LogLevel;
Expand Down Expand Up @@ -380,7 +379,7 @@ RequestStatus getStatus() {

int total=0, min=0, fetched=0, fatal=0, failed=0;
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
Date latestSuccess = CurrentTimeUTC.get();
Date latestSuccess = new Date();
Date latestFailure = null;
boolean totalFinalized = false;

Expand Down
5 changes: 2 additions & 3 deletions src/freenet/clients/fcp/RequestStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import freenet.clients.fcp.ClientRequest.Persistence;
import freenet.keys.FreenetURI;
import freenet.l10n.NodeL10n;
import freenet.support.CurrentTimeUTC;

/** The status of a request. Cached copy i.e. can be accessed outside the database thread
* even for a persistent request.
Expand Down Expand Up @@ -41,7 +40,7 @@ public abstract class RequestStatus implements Cloneable {
/** The download or upload has finished.
* @param success Did it succeed? */
synchronized void setFinished(boolean success) {
this.latestSuccess = CurrentTimeUTC.get();
this.latestSuccess = new Date();
this.hasFinished = true;
this.hasSucceeded = success;
this.hasStarted = true;
Expand All @@ -50,7 +49,7 @@ synchronized void setFinished(boolean success) {

synchronized void restart(boolean started) {
// See ClientRequester.getLatestSuccess() for why this defaults to current time.
this.latestSuccess = CurrentTimeUTC.get();
this.latestSuccess = new Date();
this.hasFinished = false;
this.hasSucceeded = false;
this.hasStarted = started;
Expand Down
3 changes: 1 addition & 2 deletions src/freenet/clients/http/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Date;
import java.util.HashSet;

import freenet.support.CurrentTimeUTC;
import freenet.support.TimeUtil;

/**
Expand Down Expand Up @@ -271,7 +270,7 @@ <TheSeeker> The following grammar uses the notation, and tokens DIGIT (decimal d
}

public static Date validateExpirationDate(Date expirationDate) {
if(CurrentTimeUTC.get().after(expirationDate))
if(new Date().after(expirationDate))
throw new IllegalArgumentException("Illegal expiration date, is in past: " + expirationDate);

return expirationDate;
Expand Down
7 changes: 3 additions & 4 deletions src/freenet/clients/http/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Set;
import java.util.UUID;

import freenet.support.CurrentTimeUTC;
import freenet.support.LRUMap;
import freenet.support.Logger;
import freenet.support.StringValidityChecker;
Expand Down Expand Up @@ -252,7 +251,7 @@ public String getCookieNamespace() {
public synchronized Session createSession(String userID, ToadletContext context) {
// We must synchronize around the fetching of the time and mSessionsByID.push() because mSessionsByID is no sorting data structure: It's a plain
// LRUMap so to ensure that it stays sorted the operation "getTime(); push();" must be atomic.
long time = CurrentTimeUTC.getInMillis();
long time = System.currentTimeMillis();

removeExpiredSessions(time);

Expand All @@ -279,7 +278,7 @@ public synchronized boolean sessionExists(ToadletContext context) {
if(sessionID == null)
return false;

removeExpiredSessions(CurrentTimeUTC.getInMillis());
removeExpiredSessions(System.currentTimeMillis());

return mSessionsByID.containsKey(sessionID);
}
Expand All @@ -299,7 +298,7 @@ public synchronized Session useSession(ToadletContext context) {

// We must synchronize around the fetching of the time and mSessionsByID.push() because mSessionsByID is no sorting data structure: It's a plain
// LRUMap so to ensure that it stays sorted the operation "getTime(); push();" must be atomic.
long time = CurrentTimeUTC.getInMillis();
long time = System.currentTimeMillis();

removeExpiredSessions(time);

Expand Down
49 changes: 37 additions & 12 deletions src/freenet/support/CurrentTimeUTC.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,62 @@

/**
* A helper class, which returns the current time and date in UTC timezone.
*
* @deprecated use the Java 8 Date-Time APIs instead
* @see java.time.Instant
* @see java.time.LocalDate
* @see java.time.ZoneOffset#UTC
*/
@Deprecated
public class CurrentTimeUTC {

/**
* Get the current date.
* @deprecated use {@link Date#Date() new Date()} or {@link java.time.Instant}
*/
@Deprecated
public static Date get() {
return new Date();
}

/**
* Get the current time in milliseconds.
*
* In the current implementation, this just returns {@link System#currentTimeMillis()}.
* You should however use CurrentTimeUTC#getInMillis() instead because
* the JavaDoc of {@link System#currentTimeMillis()} does not explicitly state what time zone it returns.
* Therefore, by using this wrapper function, your code clearly states that it uses UTC time.
* Get the current time in milliseconds since epoch.
* @return the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
* @deprecated use {@link System#currentTimeMillis()}
*/
@Deprecated
public static long getInMillis() {
return System.currentTimeMillis();
}

public static int getYear() {
return LocalDate.now(ZoneOffset.UTC).getYear();
/**
* Get the current calendar year.
* @deprecated use {@link LocalDate#getYear() LocalDate.now(ZoneOffset.UTC).getYear()}
*/
bertm marked this conversation as resolved.
Show resolved Hide resolved
@Deprecated
public static int getYear() {
return LocalDate.now(ZoneOffset.UTC).getYear();
}

public static int getMonth() {
// Previously, this method used java.util.GregorianCalendar and Calendar.MONTH, which is zero-based.
/**
* Get the zero-indexed current calendar month.
* @return the zero-indexed month number, where 0 indicates January and 11 indicates December
* @deprecated use {@link LocalDate#getMonthValue() LocalDate.now(ZoneOffset.UTC).getMonthValue() - 1}
*/
@Deprecated
public static int getMonth() {
// Previously, this method used java.util.GregorianCalendar and Calendar.MONTH, which is zero-based.
// Newer java.time API returns months values starting from one,
// and subtraction is required to preserve backward compatibility.
return LocalDate.now(ZoneOffset.UTC).getMonthValue() - 1;
}

public static int getDayOfMonth() {
return LocalDate.now(ZoneOffset.UTC).getDayOfMonth();
/**
* Get the current day of the month.
* @deprecated use {@link LocalDate#getDayOfMonth() LocalDate.now(ZoneOffset.UTC).getDayOfMonth()}
*/
bertm marked this conversation as resolved.
Show resolved Hide resolved
@Deprecated
public static int getDayOfMonth() {
return LocalDate.now(ZoneOffset.UTC).getDayOfMonth();
}
}
4 changes: 2 additions & 2 deletions src/freenet/support/LRUCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ private final class Entry {

public Entry(final Value myValue) {
mValue = myValue;
mExpirationDate = (mExpirationDelay < Long.MAX_VALUE) ? (CurrentTimeUTC.getInMillis() + mExpirationDelay) : (Long.MAX_VALUE);
mExpirationDate = (mExpirationDelay < Long.MAX_VALUE) ? (System.currentTimeMillis() + mExpirationDelay) : (Long.MAX_VALUE);
}

public boolean expired(final long time) {
return mExpirationDate < time;
}

public boolean expired() {
return expired(CurrentTimeUTC.getInMillis());
return expired(System.currentTimeMillis());
}

public Value getValue() {
Expand Down
10 changes: 4 additions & 6 deletions test/freenet/clients/http/CookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.junit.Before;
import org.junit.Test;

import freenet.support.CurrentTimeUTC;

public class CookieTest {

static final String VALID_PATH = "/Freetalk";
Expand All @@ -28,7 +26,7 @@ public class CookieTest {
@Before
public void setUp() throws Exception {
validPath = new URI(VALID_PATH);
validExpiresDate = new Date(CurrentTimeUTC.getInMillis()+60*60*1000);
validExpiresDate = new Date(System.currentTimeMillis() + 60 * 60 * 1000);
cookie = new Cookie(validPath, VALID_NAME, VALID_VALUE, validExpiresDate);
}

Expand Down Expand Up @@ -81,18 +79,18 @@ public void testCookieURIStringStringDate() throws URISyntaxException {
// TODO: Test for more invalid characters in value;

try {
new Cookie(validPath, VALID_NAME, VALID_VALUE, new Date(CurrentTimeUTC.getInMillis()-1));
new Cookie(validPath, VALID_NAME, VALID_VALUE, new Date(System.currentTimeMillis() - 1));
fail("Constructor allows construction of expired cookies.");
} catch(RuntimeException e) {}
}

@Test
public void testEqualsObject() throws URISyntaxException {
assertEquals(cookie, cookie);
assertEquals(cookie, new Cookie(validPath, VALID_NAME, VALID_VALUE, new Date(CurrentTimeUTC.getInMillis()+60*1000)));
assertEquals(cookie, new Cookie(validPath, VALID_NAME, VALID_VALUE, new Date(System.currentTimeMillis() + 60 * 1000)));

// Value is not checked in equals().
assertEquals(cookie, new Cookie(validPath, VALID_NAME, "", new Date(CurrentTimeUTC.getInMillis()+60*1000)));
assertEquals(cookie, new Cookie(validPath, VALID_NAME, "", new Date(System.currentTimeMillis() + 60 * 1000)));

assertFalse(cookie.equals(new Cookie(new URI(VALID_PATH.toLowerCase()), VALID_NAME, VALID_VALUE, validExpiresDate)));
assertEquals(cookie, new Cookie(validPath, VALID_NAME.toLowerCase(), VALID_VALUE, validExpiresDate));
Expand Down
Loading