App-Sqitch

 view release on metacpan or  search on metacpan

lib/App/Sqitch/DateTime.pm  view on Meta::CPAN

package App::Sqitch::DateTime;

use 5.010;
use strict;
use warnings;
use utf8;
use parent 'DateTime';
use DateTime 1.04;
use Locale::TextDomain qw(App-Sqitch);
use App::Sqitch::X qw(hurl);
use List::Util qw(first);
use constant ISWIN => $^O eq 'MSWin32';

our $VERSION = 'v1.6.1'; # VERSION

sub as_string_formats {
    return qw(
        raw
        iso
        iso8601
        rfc
        rfc2822
        full
        long
        medium
        short
    );
}

sub validate_as_string_format {
    my ( $self, $format ) = @_;
    hurl datetime => __x(
        'Unknown date format "{format}"',
        format => $format
    ) unless (first { $format eq $_ } $self->as_string_formats)
          || $format =~ /^(?:cldr|strftime):/;
    return $self;
}

sub as_string {
    my ( $self, %opts ) = @_;
    my $format = $opts{format} || 'raw';
    my $dt     = $self->clone;

    if ($format eq 'raw') {
        $dt->set_time_zone('UTC');
        return $dt->iso8601 . 'Z';
    }

    $dt->set_time_zone('local');

    if ( first { $format eq $_ } qw(iso iso8601) ) {
        return join ' ', $dt->ymd('-'), $dt->hms(':'), $dt->strftime('%z');
    } elsif ( first { $format eq $_ } qw(rfc rfc2822) ) {
        $dt->set_locale('en_US');
        ( my $rv = $dt->strftime('%a, %d %b %Y %H:%M:%S %z') ) =~
            s/\+0000$/-0000/;
        return $rv;
    } else {
        if (ISWIN) {
            require Win32::Locale;
            $dt->set_locale( Win32::Locale::get_locale() );
        } else {
            require POSIX;
            $dt->set_locale( POSIX::setlocale( POSIX::LC_TIME() ) );
        }
        return $dt->format_cldr($format) if $format =~ s/^cldr://;
        return $dt->strftime($format) if $format =~ s/^strftime://;
        my $meth = $dt->locale->can("datetime_format_$format") or hurl(
            datetime => __x(
                'Unknown date format "{format}"',
                format => $format
            )
        );
        return $dt->format_cldr( $dt->locale->$meth );
    }
}

1;

__END__

=head1 Name

App::Sqitch::DateTime - Sqitch DateTime object

=head1 Synopsis

  my $dt = App::Sqitch::DateTime->new(%params);
  say $dt->as_string( format => 'iso' );

=head1 Description

This subclass of L<DateTime> provides additional interfaces to support named
formats. These can be used for L<status|sqitch-status> or L<log|sqitch-log>
C<--date-format> options. App::Sqitch::DateTime provides a list of supported
formats, validates that a format string, and uses the formats to convert
itself into the appropriate string.

=head1 Interface

=head2 Class Methods

=head3 C<as_string_formats>

  my @formats = App::Sqitch::DateTime->as_string_formats;

Returns a list of formats supported by the C<format> parameter to
C<as_string>. The list currently includes:

=over

=item C<iso>

=item C<iso8601>

ISO-8601 format.

=item C<rfc>

=item C<rfc2822>

RFC-2822 format.

=item C<full>

=item C<long>

=item C<medium>

=item C<short>

Localized format of the specified length.

=item C<raw>



( run in 1.074 second using v1.01-cache-2.11-cpan-ceb78f64989 )