diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index 70436fb15..b4c1c57e0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -62,12 +62,13 @@ import java.io.OutputStream; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; +import java.util.Calendar; import java.util.Hashtable; import java.util.Stack; - import java.util.Vector; import java.util.zip.CRC32; import java.util.zip.ZipInputStream; + import org.apache.tools.ant.BuildException; import org.apache.tools.ant.FileScanner; import org.apache.tools.ant.Project; @@ -76,6 +77,7 @@ import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.ZipFileSet; import org.apache.tools.ant.types.ZipScanner; +import org.apache.tools.ant.util.DateUtils; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.SourceFileScanner; import org.apache.tools.ant.util.MergingMapper; @@ -135,9 +137,21 @@ public class Zip extends MatchingTask { * @deprecated Use setDestFile(File) instead */ public void setFile(File file) { - log("DEPRECATED - The file attribute has been renamed destfile." - +" This attribute will be unsupported before Ant1.5 is released", + log("DEPRECATED - The file attribute has been renamed destfile.", Project.MSG_ERR); + log("This attribute will be unsupported before Ant1.5 is released.", + Project.MSG_ERR); + + log("Be aware that the effect of using the file attribute depends on", + Project.MSG_ERR); + log("the phase of the moon.", Project.MSG_WARN); + int phase = DateUtils.getPhaseOfMoon(Calendar.getInstance()); + if (phase == 4) { + log("You are lucky! Full moon tonight."); + } else if (phase == 0) { + log("Be careful! New moon tonight.", Project.MSG_WARN); + } + setDestFile(file); } diff --git a/src/main/org/apache/tools/ant/util/DateUtils.java b/src/main/org/apache/tools/ant/util/DateUtils.java index 06eeef26b..45a7fcf10 100644 --- a/src/main/org/apache/tools/ant/util/DateUtils.java +++ b/src/main/org/apache/tools/ant/util/DateUtils.java @@ -57,6 +57,7 @@ import java.text.ChoiceFormat; import java.text.DateFormat; import java.text.MessageFormat; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -66,6 +67,11 @@ import java.util.TimeZone; * or a plurialization correct elapsed time in minutes and seconds. * * @author Stephane Bailliez + * @author Stefan Bodewig + * + * @since Ant 1.5 + * + * @version $Revision$ */ public final class DateUtils { @@ -172,4 +178,45 @@ public final class DateUtils { return sdf; } + /** + * Calculate the phase of the moon for a given date. + * + *
Code heavily influenced by hacklib.c in Nethack
+ * + *The Algorithm: + * + *
+ * moon period = 29.53058 days ~= 30, year = 365.2422 days + * + * days moon phase advances on first day of year compared to preceding year + * = 365.2422 - 12*29.53058 ~= 11 + * + * years in Metonic cycle (time until same phases fall on the same days of + * the month) = 18.6 ~= 19 + * + * moon phase on first day of year (epact) ~= (11*(year%19) + 18) % 30 + * (18 as initial condition for 1900) + * + * current phase in days = first day phase + days elapsed in year + * + * 6 moons ~= 177 days + * 177 ~= 8 reported phases * 22 + * + 11/22 for rounding + *+ * + * @return The phase of the moon as a number between 0 and 7 with + * 0 meaning new moon and 4 meaning full moon. + * + * @since 1.2, Ant 1.5 + */ + public static int getPhaseOfMoon(Calendar cal) { + int dayOfTheYear = cal.get(Calendar.DAY_OF_YEAR); + int yearInMetonicCycle = ((cal.get(Calendar.YEAR)-1900) % 19) + 1; + int epact = (11 * yearInMetonicCycle + 18) % 30; + if ((epact == 25 && yearInMetonicCycle > 11) || epact == 24) { + epact++; + } + return (((((dayOfTheYear + epact) * 6) + 11) % 177) / 22) & 7; + } } diff --git a/src/testcases/org/apache/tools/ant/util/DateUtilsTest.java b/src/testcases/org/apache/tools/ant/util/DateUtilsTest.java index f516fe6c3..da17e5dc4 100644 --- a/src/testcases/org/apache/tools/ant/util/DateUtilsTest.java +++ b/src/testcases/org/apache/tools/ant/util/DateUtilsTest.java @@ -63,6 +63,7 @@ import junit.framework.TestCase; * TestCase for DateUtils. * * @author Stephane Bailliez + * @author Stefan Bodewig */ public class DateUtilsTest extends TestCase { public DateUtilsTest(String s) { @@ -115,4 +116,15 @@ public class DateUtilsTest extends TestCase { DateUtils.ISO8601_TIME_PATTERN); assertEquals("20:11:12", text); } + + public void testPhaseOfMoon() { + TimeZone timeZone = TimeZone.getTimeZone("GMT"); + Calendar cal = Calendar.getInstance(timeZone); + // should be full moon + cal.set(2002, 2, 27); + assertEquals(4, DateUtils.getPhaseOfMoon(cal)); + // should be new moon + cal.set(2002, 2, 12); + assertEquals(0, DateUtils.getPhaseOfMoon(cal)); + } }