in reply to Re: Detecting whether DateTime object has a time component or not? (updated)
in thread Detecting whether DateTime object has a time component or not?

Yup, I ended up looking at the original data, which fortunately are also available. It's a CLI where user can specify the argument YYYYMMDD or YYYYMMDDTHH:MM:SS which will be coerced by the CLI framework into a DateTime before being passed to the subroutine. The coercion might be able to accept 'today' (no time-of-day elements) or 'now' (has time-of-day elements) or other formats in the future, but I think I'm okay for now, thanks.

  • Comment on Re^2: Detecting whether DateTime object has a time component or not? (updated)

Replies are listed 'Best First'.
Re^3: Detecting whether DateTime object has a time component or not?
by haukex (Archbishop) on Jun 16, 2017 at 16:34 UTC
    Yup, I ended up looking at the original data, which fortunately are also available.

    That's good! Here's how I might have done it:

    use warnings; use strict; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', pattern=>'%Y%m%dT%H:%M:%S', time_zone=>'UTC'); while (<DATA>) { chomp; my $dt; if ($_ eq 'today') { $dt = DateTime->now->truncate( to => 'day' ) ->set( hour=>23, minute=>59, second=>59 ) } elsif ($_ eq 'now') { $dt = DateTime->now } else { $_ .= "T23:59:59" if /^\d{8}$/; $dt = $strp->parse_datetime($_) } print "$_ => ",$dt->strftime('%Y-%m-%d %H:%M:%S %Z'),"\n"; } __DATA__ 20170616T15:30:53 20170616 now today