Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by Laurent_R (Canon)
on Feb 05, 2019 at 12:22 UTC ( [id://1229403]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^2: strptime("%Y-%m") in perl6
by leszekdubiel (Scribe) on Feb 05, 2019 at 14:01 UTC
    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
        I need to check command line args. I expected perl6 to be simple as:
        "parse ARGS[0] as %Y-%m od die"
        Just one sentence without additional ARGS[0] manipulation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229403]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found