Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

strptime("%Y-%m") in perl6

by leszekdubiel (Scribe)
on Feb 05, 2019 at 08:44 UTC ( [id://1229384]=perlquestion: print w/replies, xml ) Need Help??

leszekdubiel has asked for the wisdom of the Perl Monks concerning the following question:

I decided to give a try to perl6 and I got stuck -- how to parse date in format %Y-%m? Year and month only...?
perl6 -e 'say Date.new("2019-01-20"); say Date.new("2019-01"); ' 2019-01-20 Invalid Date string '2019-01'; use yyyy-mm-dd instead in block <unit> at -e line 1
Edit Let's say I want to check if month is valid. In perl 5 it is so simple:
use Time::Piece; localtime()->strptime(@ARGV[0], "%Y-%m");
I don't care about date parsing, I don't look at string at all -- perl5 does that for me.
bash$ perl -e 'use Time::Piece; localtime()->strptime(@ARGV[0], "%Y +-%m"); print "ok\n"; ' "2019-01" ok bash$ perl -e 'use Time::Piece; localtime()->strptime(@ARGV[0], "%Y +-%m"); print "ok\n"; ' "2019-77" Error parsing time at /usr/lib/x86_64-linux-gnu/perl/5.26/Time/Piece.p +m line 481.
What is elegant way to do thatn in perl6?

Replies are listed 'Best First'.
Re: strptime("%Y-%m") in perl6
by choroba (Cardinal) on Feb 05, 2019 at 08:53 UTC
    Use the named parameters:
    say Date.new(year => 2019, month => 1);

    I don't see any other signature where the missing day would pass:

    multi method new($year, $month, $day, :&formatter --> Date:D) multi method new(:$year!, :$month = 1, :$day = 1 --> Date:D) multi method new(Str $date --> Date:D) multi method new(Instant:D $dt --> Date:D) multi method new(DateTime:D $dt --> Date:D)
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: strptime("%Y-%m") in perl6
by Laurent_R (Canon) on Feb 05, 2019 at 12:22 UTC
    choroba has provided a good solution. Depending on what you need exactly, this is another possible solution using a formatter (demonstrated here on the REPL):
    > my $fmt = { sprintf "%04d-%02d", .year, .month }; -> ;; $_? is raw { #`(Block|173600432) ... } > say Date.new(2019, 1, 1, formatter => $fmt); 2019-01
    Update: Please note that you don't need to predefine the formatter:
    > say Date.new(2019, 1, "1", formatter => { sprintf "%04d-%02d", .year +, .month }) 2019-01
      Let's say I want to check if month is valid. In perl 5 it is so simple:
      use Time::Piece; localtime()->strptime(@ARGV[0], "%Y-%m");
      I don't care about date parsing, I don't look at string at all -- perl5 does that for me.
      bash$ perl -e 'use Time::Piece; localtime()->strptime(@ARGV[0], "%Y +-%m"); print "ok\n"; ' "2019-01" ok bash$ perl -e 'use Time::Piece; localtime()->strptime(@ARGV[0], "%Y +-%m"); print "ok\n"; ' "2019-77" Error parsing time at /usr/lib/x86_64-linux-gnu/perl/5.26/Time/Piece.p +m line 481.
      What is elegant way to do thatn in perl6?
        But also
        $ perl -MTime::Piece -le 'localtime()->strptime(shift, "%Y-%m-%d"); pr +int "ok"' 2019-02-30 ok
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Nor sure to really understand your requirement, but what about this:
        > for <1 5 3 13> -> $month { say Date.new(2019, $month, 1 )} 2019-01-01 2019-05-01 2019-03-01 Month out of range. Is: 13, should be in 1..12 in block <unit> at <unknown file> line 1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1229384]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-25 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found