Skip to content

Commit

Permalink
[Date] Add comments and a handful of cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Mar 3, 2012
1 parent 5ebc86e commit 642e6d1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/date/Date.winxed
Expand Up @@ -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++) {
Expand All @@ -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)
Expand Down
28 changes: 20 additions & 8 deletions src/date/Doomsday.winxed
@@ -1,18 +1,28 @@
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
to determine the day of week of any day.
*/
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)
Expand All @@ -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.

Expand Down
23 changes: 22 additions & 1 deletion 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;
Expand All @@ -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))
Expand All @@ -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;
Expand All @@ -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() +
Expand All @@ -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) {
Expand All @@ -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(); }
Expand Down
2 changes: 0 additions & 2 deletions src/date/TimeSpanFormatter.winxed
Expand Up @@ -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()));
Expand Down

0 comments on commit 642e6d1

Please sign in to comment.