On 12/21/06 6:45 PM, Dan Wierenga wrote:
> my $user = My::User->new(user_name => '[EMAIL PROTECTED]');
> ##My::User has 'use base qw(Rose::DB::Object);'
> $user->load;
> print $user->expires;
> 
> I get:
> 2006-12-21T13:59:59

What you're seeing is a stringified version of a DateTime object.  The
expires() method returns a DateTime object, not a string.

    $dt = $user->expires; # $dt is a DateTime object

You can call any method you want on that object to format it.  Examples:

    $string = $user->expires->strftime('%Y-%m-%d %H:%M:%S');
    $string = $user->expires->ymd;

Check the DateTime documentation for details:

    http://search.cpan.org/dist/DateTime/lib/DateTime.pm

DateTime column methods also accept a format => ... parameter.  From:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/MakeMethods/Da
te.pm#datetime

"If passed two arguments and the first argument is "format", then the second
argument is taken as a format string and passed to Rose::DateTime::Util's
format_date function along with the current value of the datetime
attribute."

Example:

    $string = $user->expires(format => '%Y-%m-%d %H:%M:%S');

You can find more information on format specifiers for this parameter here:

http://search.cpan.org/dist/Rose-DateTime/lib/Rose/DateTime/Util.pm#format_d
ate

> Obviously, I could just s/T/ /g on any field that's a datetime in my
> database, but I'd like a cleaner way that will make all datetime fields
> return as they are in the mysql client.

If you want the values to appear exactly as they would when sent to the
database, you can do that by convincing the object that it's saving into the
database using a utility method:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Util.pm#set_st
ate_saving

Example:

    use Rose::DB::Object::Util qw(set_state_saving unset_state_saving);
    ...
    set_state_saving($user);   # convince $user that it's saving to the db
    $string = $user->expires;  # exactly as it would be sent to MySQL
    unset_state_saving($user); # don't forget to restore the previous state!

Unfortunately, DateTime does not seem to provide a good way to set the
default stringification format for DateTime objects.  A hack to do that
would be to put this code somewhere in your app:

    use DateTime;
    no warnings 'redefine';
    sub DateTime::_stringify { shift->strftime('%Y-%m-%d %H:%M:%S') }

Then all DateTime objects will stringify "without the T" everywhere in your
code, yielding:

    $dt = $user->expires; # a DateTime object, but...

    print $dt; # prints a string like "2003-12-31 12:34:56" -- no "T"!

-John



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to