Permalink
Browse files

Enhanced the Flower::Utils::Date library.

 * The old 'date:' modifier is now 'dateof:'.
 * The new 'date:' modifier takes an ISO datetime string.
 * strftime: can now take an ISO datetime string directly.
  • Loading branch information...
1 parent aacac68 commit 51c96305192a8b8b13e8e5652647d077acfed8f1 @supernovus committed Jan 12, 2011
Showing with 43 additions and 9 deletions.
  1. +16 −3 lib/Flower/Utils/Date.pm
  2. +27 −6 t/06-utils-date.t
@@ -4,7 +4,8 @@ use DateTime::Utils;
our sub export() {
my %modifiers = {
- 'date' => &date_new,
+ 'date' => &date_string,
+ 'dateof' => &date_new,
'time' => &date_time,
'strftime' => &date_format,
'rfc' => &date_format_rfc,
@@ -13,8 +14,8 @@ our sub export() {
return %modifiers;
}
-## date: modifier, Creates a DateTime object with the given spec.
-## Usage: date: year [month] [day] [hour] [minute] [second] :tz(timezone)
+## dateof: modifier, Creates a DateTime object with the given spec.
+## Usage: dateof: year [month] [day] [hour] [minute] [second] :tz(timezone)
## The named paramter 'tz' must be specified in the common ISO offset format,
## '-0800' would represent a timezone that is 8 hours behind UTC.
## '+0430' would represent a timezone that is 4 hours and 30 minutes ahead.
@@ -39,6 +40,14 @@ our sub date_new ($parent, $query, *%opts) {
}
}
+## date: modifier, Creates a DateTime object based on an ISO datetime stamp.
+
+our sub date_string ($parent, $query, *%opts) {
+ my $dtstring = $parent.query($query);
+ my $dt = DateTime.new(~$dtstring);
+ return $parent.process-query($dt, |%opts);
+}
+
## time: modifier, Creates a DateTime object based on an epoch integer/string.
our sub date_time ($parent, $query, *%opts) {
@@ -80,6 +89,10 @@ our sub date_format ($parent, $query, *%opts) {
elsif $date ~~ Int {
$return = strftime($format, DateTime.new($date, :$timezone));
}
+ elsif $date ~~ Str {
+ ## We assume in the case of Str, the timezone is in the string.
+ $return = strftime($format, DateTime.new($date));
+ }
}
}
}
View
@@ -5,7 +5,7 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-plan 7;
+plan 10;
my $xml = '<?xml version="1.0"?>';
@@ -23,19 +23,19 @@ my %date = {
## test 1
-my $template = '<date tal:content="date: 2010 10 10"/>';
+my $template = '<date tal:content="dateof: 2010 10 10"/>';
my $flower = Flower.new(:template($template));
$flower.load-modifiers('Date');
-is $flower.parse(), $xml~'<date>2010-10-10T00:00:00Z</date>', 'date: modifier';
+is $flower.parse(), $xml~'<date>2010-10-10T00:00:00Z</date>', 'dateof: modifier';
## test 2
-$template = '<date tal:content="date: ${date/year} ${date/month} ${date/day}"/>';
+$template = '<date tal:content="dateof: ${date/year} ${date/month} ${date/day}"/>';
$flower.=another(:template($template));
-is $flower.parse(:date(%date)), $xml~'<date>2010-10-10T00:00:00Z</date>', 'date: modifier using string parsing';
+is $flower.parse(:date(%date)), $xml~'<date>2010-10-10T00:00:00Z</date>', 'dateof: modifier using string parsing';
## test 3
@@ -60,7 +60,7 @@ is $flower.parse(:date(%date)), $xml~'<date>Oct 09, 2010</date>', 'strftime: mod
## test 6
-$template = "<date tal:content=\"strftime: rfc: \{\{date: 2010 10 10 :tz('-0800')}}\"/>";
+$template = "<date tal:content=\"strftime: rfc: \{\{dateof: 2010 10 10 :tz('-0800')}}\"/>";
$flower.=another(:template($template));
is $flower.parse(), $xml~'<date>Sun, 10 Oct 2010 00:00:00 -0800</date>', 'strftime: with rfc: modifier';
@@ -73,3 +73,24 @@ $flower.=another(:template($template));
is $flower.parse(), $xml~'<date>'~$now~'</date>', 'strftime: with now: modifier';
+## test 8
+
+$template = '<date tal:content="date: \'2011-01-12T15:15:00-0800\'"/>';
+$flower.=another(:template($template));
+
+is $flower.parse(:date(%date)), $xml~'<date>2011-01-12T15:15:00-0800</date>', 'date: modifier';
+
+## test 9
+$template = '<date tal:content="strftime: \'%b %d, %Y\' {{date: \'2011-01-12T15:15:00-0800\'}}"/>';
+$flower.=another(:template($template));
+
+is $flower.parse(:date(%date)), $xml~'<date>Jan 12, 2011</date>', 'strftime: with date: modifier';
+
+## test 10
+
+$template = '<date tal:content="strftime: \'%b %d, %Y\' \'2011-01-12T15:15:00-0800\'"/>';
+$flower.=another(:template($template));
+
+is $flower.parse(:date(%date)), $xml~'<date>Jan 12, 2011</date>', 'strftime: with iso date string';
+
+

0 comments on commit 51c9630

Please sign in to comment.