From 374dbdc0924b4bfe884800b24b8164e6531048fe Mon Sep 17 00:00:00 2001 From: xor Date: Fri, 27 Feb 2009 13:52:20 +0000 Subject: [PATCH 1/6] Add a new class which will be used by Freetalk & WoT for obtaining the UTC time easily - Unfortunately what java provides for that is a mess :| git-svn-id: file:///home/toad/git-migration/temprepository/trunk/freenet@25849 67a373e5-eb02-0410-a15c-ee090a768436 --- src/freenet/support/CurrentTimeUTC.java | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/freenet/support/CurrentTimeUTC.java diff --git a/src/freenet/support/CurrentTimeUTC.java b/src/freenet/support/CurrentTimeUTC.java new file mode 100644 index 00000000..ae783cc7 --- /dev/null +++ b/src/freenet/support/CurrentTimeUTC.java @@ -0,0 +1,59 @@ +/* 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 freenet.support; + +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.clear(); + return mCalendar.getTime(); + } + } + + public static long getInMillis() { + synchronized(mCalendar) { + mCalendar.clear(); + return mCalendar.getTimeInMillis(); + } + } + + public static int getYear() { + synchronized(mCalendar) { + mCalendar.clear(); + return mCalendar.get(Calendar.YEAR); + } + } + + public static int getMonth() { + synchronized(mCalendar) { + mCalendar.clear(); + return mCalendar.get(Calendar.MONTH); + } + } + + public static int getDayOfMonth() { + synchronized(mCalendar) { + mCalendar.clear(); + return mCalendar.get(Calendar.DAY_OF_MONTH); + } + } + +} From 190a7af8e3895eda49b0568755061e01b0fd76f3 Mon Sep 17 00:00:00 2001 From: xor Date: Fri, 27 Feb 2009 15:33:51 +0000 Subject: [PATCH 2/6] Doh. class Calendar really sucks: clear() does not update the time but sets it to unix time 0. You need to use System.currentTimeMillis() for updating it. Fix this. git-svn-id: file:///home/toad/git-migration/temprepository/trunk/freenet@25859 67a373e5-eb02-0410-a15c-ee090a768436 --- src/freenet/support/CurrentTimeUTC.java | 26 ++++++++++--------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/freenet/support/CurrentTimeUTC.java b/src/freenet/support/CurrentTimeUTC.java index ae783cc7..23245975 100644 --- a/src/freenet/support/CurrentTimeUTC.java +++ b/src/freenet/support/CurrentTimeUTC.java @@ -20,38 +20,32 @@ public class CurrentTimeUTC { private static final GregorianCalendar mCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); - + public static Date get() { - synchronized(mCalendar) { - mCalendar.clear(); - return mCalendar.getTime(); - } + return new Date(); } - + public static long getInMillis() { - synchronized(mCalendar) { - mCalendar.clear(); - return mCalendar.getTimeInMillis(); - } + return System.currentTimeMillis(); } - + public static int getYear() { synchronized(mCalendar) { - mCalendar.clear(); + mCalendar.setTimeInMillis(System.currentTimeMillis()); return mCalendar.get(Calendar.YEAR); } } - + public static int getMonth() { synchronized(mCalendar) { - mCalendar.clear(); + mCalendar.setTimeInMillis(System.currentTimeMillis()); return mCalendar.get(Calendar.MONTH); } } - + public static int getDayOfMonth() { synchronized(mCalendar) { - mCalendar.clear(); + mCalendar.setTimeInMillis(System.currentTimeMillis()); return mCalendar.get(Calendar.DAY_OF_MONTH); } } From e826f07fe754e0987344fd99df4171b0f7d72f9e Mon Sep 17 00:00:00 2001 From: xor Date: Mon, 17 Aug 2009 15:47:27 +0200 Subject: [PATCH 3/6] Javadoc. --- src/freenet/support/CurrentTimeUTC.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/freenet/support/CurrentTimeUTC.java b/src/freenet/support/CurrentTimeUTC.java index 23245975..5ed6d160 100644 --- a/src/freenet/support/CurrentTimeUTC.java +++ b/src/freenet/support/CurrentTimeUTC.java @@ -25,6 +25,13 @@ public static Date get() { return new Date(); } + /** + * 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(); } From bfb318273d80c8cf460f535bc30503c6862b04be Mon Sep 17 00:00:00 2001 From: xor Date: Wed, 2 Feb 2011 22:12:27 +0100 Subject: [PATCH 4/6] - CurrentTimeUTC.get(): new Date() returns a Date which is set to local time. It stores the time zone as well so this sort of worked. We should return an UTC date anyway, therefore we now use Calendar --- src/freenet/support/CurrentTimeUTC.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/freenet/support/CurrentTimeUTC.java b/src/freenet/support/CurrentTimeUTC.java index 5ed6d160..5fe8a1a8 100644 --- a/src/freenet/support/CurrentTimeUTC.java +++ b/src/freenet/support/CurrentTimeUTC.java @@ -22,7 +22,10 @@ public class CurrentTimeUTC { private static final GregorianCalendar mCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); public static Date get() { - return new Date(); + synchronized(mCalendar) { + mCalendar.setTimeInMillis(System.currentTimeMillis()); + return mCalendar.getTime(); + } } /** From a81194ee5e243befc20c9d2579260b180e39d270 Mon Sep 17 00:00:00 2001 From: xor-freenet Date: Sun, 22 Oct 2023 16:10:14 +0200 Subject: [PATCH 5/6] CurrentTimeUTC: Move the class 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. Its commit history was preserved by committing all of the history to the FT repository, which yielded the 4 commits before this one here. This was done as such: - On fred, use the following command to create *.patch files. Enter the command as a single line: $ git format-patch --output-directory ~/Freetalk/patches "e0d3a2ed19e282f960453f413d5f91a38d6cb161~1.. 286b5c98ff6d53b9c6dabd9753bfef13aaa8c781" src/freenet/support/CurrentTimeUTC.java - On FT, apply the patches with: git am --whitespace=nowarn patches/* This added the class as of commit 286b5c98ff6d53b9c6dabd9753bfef13aaa8c781 which was the last commit before the fred developers applied modifications to it. I am not including these modifications yet because I have not reviewed them yet and have no time to do so right now. I will file an issue to do this in the bugtracker. --- .../support => plugins/Freetalk/util}/CurrentTimeUTC.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{freenet/support => plugins/Freetalk/util}/CurrentTimeUTC.java (100%) diff --git a/src/freenet/support/CurrentTimeUTC.java b/src/plugins/Freetalk/util/CurrentTimeUTC.java similarity index 100% rename from src/freenet/support/CurrentTimeUTC.java rename to src/plugins/Freetalk/util/CurrentTimeUTC.java From b3747e5e5449c9ad39a169fd0d2ebd001e5edf42 Mon Sep 17 00:00:00 2001 From: xor-freenet Date: Mon, 23 Oct 2023 14:52:07 +0200 Subject: [PATCH 6/6] CurrentTimeUTC: Amend the previous commit --- src/plugins/Freetalk/FetchFailedMarker.java | 2 +- src/plugins/Freetalk/MessageManager.java | 2 +- src/plugins/Freetalk/Persistent.java | 2 +- src/plugins/Freetalk/SubscribedBoard.java | 2 +- src/plugins/Freetalk/WoT/WoTIdentityManager.java | 2 +- src/plugins/Freetalk/WoT/WoTMessageManager.java | 2 +- src/plugins/Freetalk/tasks/NewBoardTask.java | 2 +- src/plugins/Freetalk/tasks/PersistentTask.java | 2 +- src/plugins/Freetalk/tasks/PersistentTaskManager.java | 2 +- src/plugins/Freetalk/tasks/SubscribeToAllBoardsTask.java | 2 +- src/plugins/Freetalk/tasks/WoT/IntroduceIdentityTask.java | 2 +- src/plugins/Freetalk/ui/NNTP/FreetalkNNTPHandler.java | 2 +- src/plugins/Freetalk/util/CurrentTimeUTC.java | 2 +- test/plugins/Freetalk/WoT/WoTMessageManagerTest.java | 2 +- test/plugins/Freetalk/WoT/WoTMessageXMLTest.java | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/plugins/Freetalk/FetchFailedMarker.java b/src/plugins/Freetalk/FetchFailedMarker.java index b92230d6..3abb7444 100644 --- a/src/plugins/Freetalk/FetchFailedMarker.java +++ b/src/plugins/Freetalk/FetchFailedMarker.java @@ -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 diff --git a/src/plugins/Freetalk/MessageManager.java b/src/plugins/Freetalk/MessageManager.java index 04efd7ce..8bddcaf7 100644 --- a/src/plugins/Freetalk/MessageManager.java +++ b/src/plugins/Freetalk/MessageManager.java @@ -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; diff --git a/src/plugins/Freetalk/Persistent.java b/src/plugins/Freetalk/Persistent.java index 8ea5a16c..632eaa33 100644 --- a/src/plugins/Freetalk/Persistent.java +++ b/src/plugins/Freetalk/Persistent.java @@ -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! diff --git a/src/plugins/Freetalk/SubscribedBoard.java b/src/plugins/Freetalk/SubscribedBoard.java index a90b854d..e897ee83 100644 --- a/src/plugins/Freetalk/SubscribedBoard.java +++ b/src/plugins/Freetalk/SubscribedBoard.java @@ -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; diff --git a/src/plugins/Freetalk/WoT/WoTIdentityManager.java b/src/plugins/Freetalk/WoT/WoTIdentityManager.java index 7c28fe22..f7f18dde 100644 --- a/src/plugins/Freetalk/WoT/WoTIdentityManager.java +++ b/src/plugins/Freetalk/WoT/WoTIdentityManager.java @@ -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; diff --git a/src/plugins/Freetalk/WoT/WoTMessageManager.java b/src/plugins/Freetalk/WoT/WoTMessageManager.java index ae300b11..18a90bcb 100644 --- a/src/plugins/Freetalk/WoT/WoTMessageManager.java +++ b/src/plugins/Freetalk/WoT/WoTMessageManager.java @@ -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 { diff --git a/src/plugins/Freetalk/tasks/NewBoardTask.java b/src/plugins/Freetalk/tasks/NewBoardTask.java index f79282c6..77570854 100644 --- a/src/plugins/Freetalk/tasks/NewBoardTask.java +++ b/src/plugins/Freetalk/tasks/NewBoardTask.java @@ -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; diff --git a/src/plugins/Freetalk/tasks/PersistentTask.java b/src/plugins/Freetalk/tasks/PersistentTask.java index 85b56f5a..eb1fb7a0 100644 --- a/src/plugins/Freetalk/tasks/PersistentTask.java +++ b/src/plugins/Freetalk/tasks/PersistentTask.java @@ -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; /** diff --git a/src/plugins/Freetalk/tasks/PersistentTaskManager.java b/src/plugins/Freetalk/tasks/PersistentTaskManager.java index a570ed4f..47ec050d 100644 --- a/src/plugins/Freetalk/tasks/PersistentTaskManager.java +++ b/src/plugins/Freetalk/tasks/PersistentTaskManager.java @@ -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; diff --git a/src/plugins/Freetalk/tasks/SubscribeToAllBoardsTask.java b/src/plugins/Freetalk/tasks/SubscribeToAllBoardsTask.java index 0afceb5e..a918dc9c 100644 --- a/src/plugins/Freetalk/tasks/SubscribeToAllBoardsTask.java +++ b/src/plugins/Freetalk/tasks/SubscribeToAllBoardsTask.java @@ -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; /** diff --git a/src/plugins/Freetalk/tasks/WoT/IntroduceIdentityTask.java b/src/plugins/Freetalk/tasks/WoT/IntroduceIdentityTask.java index 0c538d67..7179f5f2 100644 --- a/src/plugins/Freetalk/tasks/WoT/IntroduceIdentityTask.java +++ b/src/plugins/Freetalk/tasks/WoT/IntroduceIdentityTask.java @@ -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; diff --git a/src/plugins/Freetalk/ui/NNTP/FreetalkNNTPHandler.java b/src/plugins/Freetalk/ui/NNTP/FreetalkNNTPHandler.java index 67a4de4a..a368edb0 100644 --- a/src/plugins/Freetalk/ui/NNTP/FreetalkNNTPHandler.java +++ b/src/plugins/Freetalk/ui/NNTP/FreetalkNNTPHandler.java @@ -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; /** diff --git a/src/plugins/Freetalk/util/CurrentTimeUTC.java b/src/plugins/Freetalk/util/CurrentTimeUTC.java index 5fe8a1a8..bd279452 100644 --- a/src/plugins/Freetalk/util/CurrentTimeUTC.java +++ b/src/plugins/Freetalk/util/CurrentTimeUTC.java @@ -1,7 +1,7 @@ /* 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 freenet.support; +package plugins.Freetalk.util; import java.util.Calendar; import java.util.Date; diff --git a/test/plugins/Freetalk/WoT/WoTMessageManagerTest.java b/test/plugins/Freetalk/WoT/WoTMessageManagerTest.java index 412977ca..a38265a2 100644 --- a/test/plugins/Freetalk/WoT/WoTMessageManagerTest.java +++ b/test/plugins/Freetalk/WoT/WoTMessageManagerTest.java @@ -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 { diff --git a/test/plugins/Freetalk/WoT/WoTMessageXMLTest.java b/test/plugins/Freetalk/WoT/WoTMessageXMLTest.java index 3976920c..e400888f 100644 --- a/test/plugins/Freetalk/WoT/WoTMessageXMLTest.java +++ b/test/plugins/Freetalk/WoT/WoTMessageXMLTest.java @@ -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 {