Skip to content

Commit

Permalink
[Date] Use the new format_default method on StringFormatter to do def…
Browse files Browse the repository at this point in the history
…ault stringification on Date and TimeSpan
  • Loading branch information
Whiteknight committed Mar 3, 2012
1 parent c6aab2b commit ec37e35
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/core/StringFormatter.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ namespace Rosella.StringFormatter
f[type_c] = fmt;
}

// Search for a formatter for the given type. Return null if none are found.
function get_type_formatter(var type)
{
var type_c = Rosella.get_type_class(type);
var f = Rosella.StringFormatter.type_formatters();
if (exists f[type_c])
return f[type_c];
return null;
}

/* Role-Specific Formatters
*/

Expand Down
21 changes: 11 additions & 10 deletions src/date/Date.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,25 @@ namespace Rosella.Date
}

// Get the default Date formatter object
const string DEFAULT_DATE_FORMATTER = "Rosella.Date.default_date_formatter";
function default_date_formatter()
{
var temp = Rosella.Globals.get_global(DEFAULT_DATE_FORMATTER);
var date_c = class Rosella.Date;
var temp = Rosella.StringFormatter.get_type_formatter(date_c);
if (temp == null) {
temp = new Rosella.Date.DateFormatter();
Rosella.Globals.register_global(DEFAULT_DATE_FORMATTER, temp);
Rosella.StringFormatter.register_type_formatter(date_c, temp);
}
return temp;
}

const string DEFAULT_TIMESPAN_FORMATTER = "Rosella.Date.default_timespan_formatter";
// Get the default TimeSpan formatter object
function default_timespan_formatter()
{
var temp = Rosella.Globals.get_global(DEFAULT_TIMESPAN_FORMATTER);
var ts_c = class Rosella.Date.TimeSpan;
var temp = Rosella.StringFormatter.get_type_formatter(ts_c);
if (temp == null) {
temp = new Rosella.Date.TimeSpanFormatter();
Rosella.Globals.register_global(DEFAULT_TIMESPAN_FORMATTER, temp);
Rosella.StringFormatter.register_type_formatter(ts_c, temp);
}
return temp;
}
Expand Down Expand Up @@ -533,18 +534,18 @@ class Rosella.Date
{
if (f == null || f == "")
return string(self);
return self.get_string_converter().format(self, f);
return self.get_string_formatter().format(self, f);
}

// Get the string converter to use
function get_string_converter()
// Get the string formatter to use
function get_string_formatter()
{
return Rosella.Date.default_date_formatter();
}

function to_string()
{
return sprintf("%04d/%02d/%02d %02d:%02d:%02d", [self.year(), self.month(), self.day(), self.hours(), self.minutes(), self.seconds()]);
return self.get_string_formatter().format_default(self);
}

/* Operations and TimeSpans
Expand Down
7 changes: 7 additions & 0 deletions src/date/DateFormatter.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ class Rosella.Date.DateFormatter : Rosella.StringFormatter
if (contains(f, "ss")) f = f.replace("ss", format("%02d", obj.seconds()));
return f;
}

function format_default(var obj)
{
return sprintf("%04d/%02d/%02d %02d:%02d:%02d",
[obj.year(), obj.month(), obj.day(), obj.hours(), obj.minutes(), obj.seconds()]
);
}
}
9 changes: 5 additions & 4 deletions src/date/TimeSpan.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,20 @@ class Rosella.Date.TimeSpan
{
if (f == null || f == "")
return string(self);
return self.get_string_converter().format(self, f);
return self.get_string_formatter().format(self, f);
}

// Get the string converter to use
function get_string_converter()
// Get the string formatter to use. Use the default formatter. Subclasses
// can use something else
function get_string_formatter()
{
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()]);
return self.get_string_formatter().format_default(self);
}

/* Vtables
Expand Down
7 changes: 7 additions & 0 deletions src/date/TimeSpanFormatter.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ class Rosella.Date.TimeSpanFormatter : Rosella.StringFormatter
if (contains(f, "ss")) f = f.replace("ss", format("%02d", obj.seconds()));
return f;
}

function format_default(var obj)
{
return sprintf("%d days, %02d:%02d:%02d",
[obj.days(), obj.hours(), obj.minutes(), obj.seconds()]
);
}
}

0 comments on commit ec37e35

Please sign in to comment.