diff --git a/src/date/Date.winxed b/src/date/Date.winxed index 8082498f..b0d429c7 100644 --- a/src/date/Date.winxed +++ b/src/date/Date.winxed @@ -581,10 +581,13 @@ class Rosella.Date Rosella.Error.invalid(__FUNCTION__, "Unknown type in Date.add: %s", typeof(ts)); } + // Determine if two Date objects are equal function equals(var d) { if (!(d instanceof Rosella.Date)) return false; + if (d instanceof Rosella.Date.SpecialDate) + return d.equals(self); var a = self.time_array; var b = d.time_array; for (int i = 0; i < 6; i++) { @@ -596,6 +599,7 @@ class Rosella.Date function compare_to(var d) { + // TODO: Can we attempt to coerce to a Date and compare in a sane way? if (!(d instanceof Rosella.Date)) Rosella.Error.invalid("Cannot compare Date and non-Date PMC"); if (d instanceof Rosella.Date.SpecialDate) diff --git a/src/date/Doomsday.winxed b/src/date/Doomsday.winxed index fa2bb72c..24c91b8d 100644 --- a/src/date/Doomsday.winxed +++ b/src/date/Doomsday.winxed @@ -1,3 +1,19 @@ +namespace Rosella.Date.Doomsday +{ + // doomsdays[N] is the day of month N that falls on a doomsday. + const string GET_DOOMSDAY_ARRAY = "Rosella.Date.Doomsday.get_doomsday_array"; + function get_doomsday_array() + { + var temp = Rosella.Globals.get_global(GET_DOOMSDAY_ARRAY); + if (temp == null) { + int doomsdays[] = [ 3, 28, 7, 4, 9, 6, 11, 8, 5, 10, 7, 12 ]; + temp = doomsdays; + Rosella.Globals.register_global(GET_DOOMSDAY_ARRAY, temp); + } + return temp; + } +} + /* Doomsday Algorithm This class implements the Doomsday Algorithm, a fast, computationally-simple method @@ -5,14 +21,8 @@ */ class Rosella.Date.Doomsday { - var doomsdays; - // Constructor - function Doomsday() - { - // doomsdays[N] is the day of month N that falls on a doomsday. - self.doomsdays = [ 3, 28, 7, 4, 9, 6, 11, 8, 5, 10, 7, 12 ]; - } + function Doomsday() { } // Get the day of the week for the give Date. Return an integer function get_day(int year, int month, int day) @@ -24,11 +34,13 @@ class Rosella.Date.Doomsday if (day < 1 || day > 31) Rosella.Error.invalid(__FUNCTION__, "Bad day"); + var doomsdays = Rosella.Date.Doomsday.get_doomsday_array(); + // Determine the doomsday for this year. int dday = self.__get_dday(year); int leap = Rosella.Date.is_leap_year(year); - int anchor = self.doomsdays[month - 1]; + int anchor = doomsdays[month - 1]; if (leap && month <= 2) anchor += 1; // the anchor date for jan/feb is +1 for leap years. diff --git a/src/date/TimeSpan.winxed b/src/date/TimeSpan.winxed index f7b672f2..dd1c5d51 100644 --- a/src/date/TimeSpan.winxed +++ b/src/date/TimeSpan.winxed @@ -1,3 +1,7 @@ +/* Time Span Class + These objects represent a length of time without defined start and end + times +*/ class Rosella.Date.TimeSpan { const int IDX_SECONDS = 0; @@ -6,6 +10,8 @@ class Rosella.Date.TimeSpan const int IDX_DAYS = 3; var parts; + // 2-date Constructor. Take two Date objects and calculate the length of + // time between them function TimeSpan(var start, var end) { if (!(start instanceof Rosella.Date && end instanceof Rosella.Date)) @@ -19,6 +25,7 @@ class Rosella.Date.TimeSpan self.TimeSpan(diff); } + // Constructor taking a number of seconds function TimeSpan(int diff) { int secs = diff % 60; @@ -32,25 +39,32 @@ class Rosella.Date.TimeSpan self.TimeSpan(days, hours, mins, secs); } + // Constructor taking day, hour, minute and second function TimeSpan(int days, int hours, int min, int sec) { int parts[] = [sec, min, hours, days]; self.parts = parts; } + // Create a new TimeSpan the same as this one function copy() { return new Rosella.Date.TimeSpan(int(self.days()), int(self.hours()), int(self.minutes()), int(self.seconds())); } + // Get the number of seconds function seconds() { return self.parts[IDX_SECONDS]; } + // Get the number of minutes function minutes() { return self.parts[IDX_MINUTES]; } + // Get the number of hours function hours() { return self.parts[IDX_HOURS]; } + // Get the number of days function days() { return self.parts[IDX_DAYS]; } + // Get the length as a total number of seconds function to_total_seconds() { return self.seconds() + @@ -61,15 +75,18 @@ class Rosella.Date.TimeSpan ); } + // Add a second TimeSpan and get a new one function add(var ts) { if (ts instanceof Rosella.Date.TimeSpan) { int diff = self.to_total_seconds() + ts.to_total_seconds(); - return new TimeSpan(diff); + return new Rosella.Date.TimeSpan(diff); } Rosella.Error.invalid(__FUNCTION__, "Cannot add %s to TimeSpan", typeof(ts)); } + // Subtract a timespan from this one, return the absolute difference as a + // new TimeSpan function subtract(var ts) { if (ts instanceof Rosella.Date.TimeSpan) { @@ -94,11 +111,15 @@ class Rosella.Date.TimeSpan return Rosella.Date.default_timespan_formatter(); } + // Get a string represetation function to_string() { return sprintf("%d days, %02d:%02d:%02d", [self.days(), self.hours(), self.minutes(), self.seconds()]); } + /* Vtables + */ + function get_string[vtable]() { return self.to_string(); } function clone[vtable]() { return self.copy(); } diff --git a/src/date/TimeSpanFormatter.winxed b/src/date/TimeSpanFormatter.winxed index b339577b..2cd82009 100644 --- a/src/date/TimeSpanFormatter.winxed +++ b/src/date/TimeSpanFormatter.winxed @@ -8,8 +8,6 @@ class Rosella.Date.TimeSpanFormatter : Rosella.StringFormatter function TimeSpanFormatter() { } // Format the TimeSpan object - // TODO: Support additional codes - // TODO: Find an algorithm which isn't so slow and wasteful function format(var obj, string f) { if (contains(f, "dd")) f = f.replace("dd", format("%02d", obj.days()));