1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| use Modern::Perl;
use DateTime;
# data d'entrée
my @data = (
'50,2020'
, '3,2021'
, '18,2022'
, '8,2021'
);
foreach my $i ( @data ) {
my ($week, $year) = split ',', $i;
my $aref_dt = get_dt( $week, $year );
say 'week: ',$week, ' year: ', $year;
say $_ for $aref_dt->@*;
print "---\n";
}
sub get_dt {
my $week = shift;
my $year = shift;
# init la date
my $dt = DateTime->now( locale => 'fr' )
->set_time_zone( 'Europe/Paris' );
# recale sur la bonne semaine
unless ( $dt->week_number == $week
&& $dt->week_year == $year ) {
$dt->add( weeks => $week - $dt->week_number,
years => $year - $dt->week_year );
}
# recale sur le lundi
if ( $dt->local_day_of_week > 1 ) {
$dt->subtract( days => $dt->local_day_of_week - 1 )
}
# construction de la liste de dates
my $r;
foreach my $i ( 0..6 ) {
my $dt_clone = $dt->clone;
$dt_clone->add( days => $i );
push $r->@*, $dt_clone->ymd('-');
}
$r
} |
Partager