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

Hi
if I understand correctly Date::Calc module documentation, the following code should always print the same date.
It does not. The printed dates even change if I run the code more times...
Is there a mistake in my code, or it is a problem with Windows (Strawberry Perl)?
Thanks!
use strict; use Date::Calc qw(Date_to_Time Time_to_Date); my @current_date = (2023,5,17, 1,0,0); for my $i (1..10) { my $time = Date_to_Time(@current_date); my @date = Time_to_Date([$time]); print join(",",@date),"\n"; }
Result:
C:> perl date.pl 1970,6,10,23,54,16 1970,6,10,23,57,28 1970,6,11,0,0,40 1970,6,10,23,54,40 1970,6,11,0,0,16 1970,6,11,0,16,16 1970,6,10,23,55,4 1970,6,10,23,57,52 1970,6,10,23,59,52 1970,6,10,23,54,16 C:> perl date.pl 1971,4,5,20,27,4 1971,4,5,20,30,16 1971,4,5,20,33,28 1971,4,5,20,27,28 1971,4,5,20,33,4 1971,4,5,20,49,4 1971,4,5,20,27,52 1971,4,5,20,30,40 1971,4,5,20,32,40 1971,4,5,20,27,4

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: Date::Calc on Windows
by soonix (Chancellor) on May 17, 2023 at 17:57 UTC
    It looks like you wrote
    my @date = Time_to_Date([$time]);
    but that should be
    my @date = Time_to_Date($time);
    because Time_to_Date wants a scalar number, not an array reference…
      Thank you!!
Re: Date::Calc on Windows
by haukex (Archbishop) on May 17, 2023 at 17:59 UTC

    Please use <code> tags instead of <pre> tags to format your code. That makes it easier for us to see that you're calling Time_to_Date([$time]), which is the mistake here: the square brackets in the Date::Calc documentation in "($year,$month,$day, $hour,$min,$sec) = Time_to_Date([time])" refer to the fact that the time value is optional, not that you should put square brackets around the argument (which makes it an arrayref, which then gets interpreted by Time_to_Date as those strange datetimes you're getting).