[Jifty-commit] r940 - in Jifty-DBI/trunk: lib/Jifty/DBI/Filter
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Wed Apr 26 23:56:18 EDT 2006
Author: jesse
Date: Wed Apr 26 23:56:18 2006
New Revision: 940
Added:
Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Date.pm
Modified:
Jifty-DBI/trunk/ (props changed)
Jifty-DBI/trunk/Changes
Log:
r11959 at hualien: jesse | 2006-04-26 23:54:53 -0400
* Jifty::DBI::Filter::Date
Modified: Jifty-DBI/trunk/Changes
==============================================================================
--- Jifty-DBI/trunk/Changes (original)
+++ Jifty-DBI/trunk/Changes Wed Apr 26 23:56:18 2006
@@ -1,5 +1,5 @@
Revision history for Perl extension Jifty::DBI.
-
+* Added a filter for Dates, lib/Jifty/DBI/Filter/Date.pm
* Switched Jifty::DBI::Record to autocreate methods on object load rather than
use AUTOLOAD.
Added: Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Date.pm
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Date.pm Wed Apr 26 23:56:18 2006
@@ -0,0 +1,93 @@
+package Jifty::DBI::Filter::Date;
+
+use warnings;
+use strict;
+
+use base qw|Jifty::DBI::Filter|;
+use DateTime ();
+use DateTime::Format::ISO8601 ();
+use DateTime::Format::Strptime ();
+
+
+=head1 NAME
+
+Jifty::DBI::Filter::Date - DateTime object wrapper around date columns
+
+=head1 DESCRIPTION
+
+This filter allow you to work with DateTime objects that represent "Dates",
+store everything in the database in GMT and not hurt yourself badly
+when you pull them out and put them in repeatedly
+text dates.
+
+=head2 encode
+
+If value is a DateTime object then move it into a "floating" timezone
+and expand it into ISO 8601 format C<YYYY-MM-DD>. By storing it in
+the database as a floating timezone, it doesn't matter if the user's
+desired timezone changes between lookups
+
+Does nothing if value is not defined or is a string.
+
+
+=cut
+
+sub encode {
+ my $self = shift;
+
+ my $value_ref = $self->value_ref;
+ return unless $$value_ref;
+ warn "We ended up with ".$$value_ref ." on encode";
+
+ return unless UNIVERSAL::isa( $$value_ref, 'DateTime' );
+ $$value_ref->time_zone('floating');
+
+ my $format = ($self->column->type eq "date" ? "%Y-%m-%d" : "%Y-%m-%d %H:%M:%S");
+ $$value_ref = $$value_ref->strftime($format);
+ warn "We ended up with ".$$value_ref ." on encode";
+ return 1;
+}
+
+=head2 decode
+
+If we're loading something from a column that doesn't specify times, then
+it's loaded into a floating timezone.
+
+If value is defined then converts it into DateTime object otherwise do
+nothing.
+
+=cut
+
+sub decode {
+ my $self = shift;
+
+ my $value_ref = $self->value_ref;
+ return unless defined $$value_ref;
+
+ warn "Our value is ".$$value_ref;
+# XXX: Looks like we should use special modules for parsing DT because
+# different MySQL versions can return DT in different formats(none strict ISO)
+# Pg has also special format that depends on "european" and
+# server time_zone, by default ISO
+# other DBs may have own formats(Interbase for example can be forced to use special format)
+# but we need Jifty::DBI::Handle here to get DB type
+
+ my $str = join('T', split ' ', $$value_ref, 2);
+ my $dt = DateTime::Format::ISO8601->parse_datetime($str);
+ $dt->time_zone('floating');
+ $dt->set_formatter(DateTime::Format::Strptime->new(pattern => '%Y-%m-%d'));
+ if ($dt) {
+ warn "and we end up with ".$dt;
+ $$value_ref = $dt;
+ } else {
+ return;
+ }
+}
+
+=head1 SEE ALSO
+
+L<Jifty::DBI::Filter>, L<DateTime>
+
+=cut
+
+1;
More information about the Jifty-commit
mailing list