Skip to content

Commit

Permalink
Add "object" date format.
Browse files Browse the repository at this point in the history
This is so that one can easily get a DateTime object for a date value,
rather than a string.
  • Loading branch information
theory committed May 7, 2010
1 parent 182dbfd commit fc9374f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
12 changes: 12 additions & 0 deletions lib/Bric/Changes.pod
Expand Up @@ -1475,6 +1475,18 @@ Sources Manager). [David]

=back

=head2 Improvements

=over

=item *

Added the "object" format for datetime values. Pass "object" as the formatting
string and a L<DateTime> object will be returned, rather than a string.
[David]

=back

=head1 VERSION 1.10.10 (2010-04-11)

=head2 Improvements
Expand Down
16 changes: 10 additions & 6 deletions lib/Bric/Util/Time.pm
Expand Up @@ -200,10 +200,11 @@ Takes a date/time string formatted for the database, converts it to the time
zone set in the "Time Zone" preference, and returns it in the C<strftime>
format provided by C<$format>. If C<$format> is not provided, the date/time
will be returned in the format set in the "Date/Time Format" preference. If
C<$format> is 'epoch', it will return the time in epoch seconds. If
C<$db_date> is not provided and C<$bool> is false, then C<local_date()>
returns C<undef>. If C<$db_date> is not provided and C<$bool> is true, then
C<local_date()> returns the current date/time.
C<$format> is 'epoch', it will return the time in epoch seconds. If C<$format>
is 'object', it will return a L<DateTime> object. If C<$db_date> is not
provided and C<$bool> is false, then C<local_date()> returns C<undef>. If
C<$db_date> is not provided and C<$bool> is true, then C<local_date()> returns
the current date/time.
Use this function in Bricolage accessor methods to return a localized
date/time string.
Expand Down Expand Up @@ -234,8 +235,11 @@ sub local_date {
$format ||= Bric::Util::Pref->lookup_val('Date/Time Format');
my $dt = $db_date ? db_datetime($db_date) : DateTime->now;
$dt->set_time_zone(Bric::Util::Pref->lookup_val('Time Zone'));
return $format eq 'epoch' ? $dt->epoch : $dt->strftime($format);
} # strfdate()
return $dt if $format eq 'object';
return $format eq 'epoch' ? $dt->epoch
: $format eq 'object' ? $dt
: $dt->strftime($format);
}

##############################################################################

Expand Down
6 changes: 5 additions & 1 deletion t/Bric/Util/Time/DevTest.pm
Expand Up @@ -56,7 +56,7 @@ sub test_strfdate : Test(5) {

##############################################################################
# Test local_date().
sub test_local_date : Test(6) {
sub test_local_date : Test(7) {
is( local_date($utc_date), $local_date,
"Check local date is '$local_date'" );
is( local_date($utc_iso_short_date), $local_date,
Expand All @@ -66,6 +66,10 @@ sub test_local_date : Test(6) {
is( local_date($utc_iso_date, 'epoch'), $epoch,
"Check that local date is '$epoch'" );

# Try the "object" format.
isa_ok local_date($utc_iso_date, 'object'), 'DateTime',
'Should get DateTime object for "object" format';

# Check the local date with a known format argument.
my $current = DateTime->now(time_zone => $tz)->strftime(ISO_8601_FORMAT);
is( local_date(undef, ISO_8601_FORMAT, 1), $current,
Expand Down

0 comments on commit fc9374f

Please sign in to comment.