Skip to content

Commit

Permalink
Merge branch 'move-class-CurrentTimeUTC' into master
Browse files Browse the repository at this point in the history
= Move class CurrentTimeUTC from fred to Freetalk

The fred developers recently revamped the class, and then decided
to deprecate the class in fred, hence Freetalk (and WoT) will have to
contain it so it can be removed there.

This branch only moves the class as it was before the modifications.
I did not include these modifications yet because there is no unit test
and I have no time to write one and also not to review the changes.

A bugtracker issue has been filed to document that the modifications
will have to be applied:
https://bugs.freenetproject.org/view.php?id=7205
  • Loading branch information
xor-freenet committed Oct 23, 2023
2 parents 7cfbcbe + b3747e5 commit 0d123a1
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/FetchFailedMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.Date;

import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;

/**
* When a message (list) fetch fails we need to mark the message (list) as fetched to prevent the failed message (list) from getting into the
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/MessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import freenet.keys.FreenetURI;
import freenet.node.PrioRunnable;
import freenet.pluginmanager.PluginRespirator;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;
import freenet.support.TrivialTicker;
import freenet.support.io.NativeThread;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/Persistent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.db4o.ext.ObjectInfo;
import com.db4o.query.Query;

import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;
/**
* ATTENTION: This class is duplicated in the Web Of Trust plugin. Backport any changes!
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/SubscribedBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.db4o.ext.ExtObjectContainer;
import com.db4o.query.Query;

import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;
import freenet.support.codeshortification.IfNotEquals;
import freenet.support.codeshortification.IfNull;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/WoT/WoTIdentityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import freenet.node.PrioRunnable;
import freenet.pluginmanager.PluginNotFoundException;
import freenet.support.Base64;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Executor;
import freenet.support.IllegalBase64Exception;
import freenet.support.LRUCache;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/WoT/WoTMessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import freenet.keys.FreenetURI;
import freenet.node.RequestClient;
import freenet.pluginmanager.PluginRespirator;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;

public final class WoTMessageManager extends MessageManager {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/tasks/NewBoardTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import plugins.Freetalk.exceptions.NoSuchBoardException;
import plugins.Freetalk.ui.web.WebInterface;
import plugins.Freetalk.ui.web.WebPage;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;
import freenet.support.codeshortification.IfNull;

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/tasks/PersistentTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import plugins.Freetalk.exceptions.NoSuchIdentityException;
import plugins.Freetalk.ui.web.WebInterface;
import plugins.Freetalk.ui.web.WebPage;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.codeshortification.IfNull;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/tasks/PersistentTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.db4o.query.Query;

import freenet.node.PrioRunnable;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;
import freenet.support.TrivialTicker;
import freenet.support.codeshortification.IfNull;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/tasks/SubscribeToAllBoardsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import plugins.Freetalk.OwnIdentity;
import plugins.Freetalk.ui.web.WebInterface;
import plugins.Freetalk.ui.web.WebPage;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.codeshortification.IfNull;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/tasks/WoT/IntroduceIdentityTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import plugins.Freetalk.ui.web.IntroduceIdentityPage;
import plugins.Freetalk.ui.web.WebInterface;
import plugins.Freetalk.ui.web.WebPage;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;
import freenet.support.codeshortification.IfNotEquals;

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/Freetalk/ui/NNTP/FreetalkNNTPHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import plugins.Freetalk.exceptions.NoSuchBoardException;
import plugins.Freetalk.exceptions.NoSuchIdentityException;
import plugins.Freetalk.exceptions.NoSuchMessageException;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;
import freenet.support.Logger;

/**
Expand Down
63 changes: 63 additions & 0 deletions src/plugins/Freetalk/util/CurrentTimeUTC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* This code is part of Freenet. It is distributed under the GNU General
* Public License, version 2 (or at your option any later version). See
* http://www.gnu.org/ for further details of the GPL. */
package plugins.Freetalk.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;


/**
* A wrapper class around a GregorianCalendar which always returns the current time.
* This is useful for working around the pitfall of class Calendar: It only returns the current time when you first use a get*() function,
* in any get*() calls after the first call, the time value of the first call is returned. One would have to call Calendar.clear() before each
* get to obtain the current time and this class takes care of that for you.
*
* Further, this class is synchronized so you do not need to worry about synchronization of a Calendar anymore.
*/
public class CurrentTimeUTC {

private static final GregorianCalendar mCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));

public static Date get() {
synchronized(mCalendar) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
return mCalendar.getTime();
}
}

/**
* Get the current time in milliseconds.
*
* In the current implementation, this just returns System.currentTimeMilis(). You should however use CurrenTimeUTC.getInMillis() instead because
* the JavaDoc of System.currentTimeMilis() does not explicitly state what time zone it returns. Therefore, by using this wrapper function, your code
* clearly states that it uses UTC time.
*/
public static long getInMillis() {
return System.currentTimeMillis();
}

public static int getYear() {
synchronized(mCalendar) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
return mCalendar.get(Calendar.YEAR);
}
}

public static int getMonth() {
synchronized(mCalendar) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
return mCalendar.get(Calendar.MONTH);
}
}

public static int getDayOfMonth() {
synchronized(mCalendar) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
return mCalendar.get(Calendar.DAY_OF_MONTH);
}
}

}
2 changes: 1 addition & 1 deletion test/plugins/Freetalk/WoT/WoTMessageManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.db4o.query.Query;

import freenet.keys.FreenetURI;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;


public class WoTMessageManagerTest extends DatabaseBasedTest {
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/Freetalk/WoT/WoTMessageXMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import plugins.Freetalk.Version;
import plugins.Freetalk.util.MimeType;
import freenet.keys.FreenetURI;
import freenet.support.CurrentTimeUTC;
import plugins.Freetalk.util.CurrentTimeUTC;


public class WoTMessageXMLTest extends DatabaseBasedTest {
Expand Down

0 comments on commit 0d123a1

Please sign in to comment.