sshingor has asked for the wisdom of the Perl Monks concerning the following question:
Hello Perl gurus,
I did read few threads on PerMonks before posting this question here.
Friday 01-01-2016, starts and ends the week. Inspite of this being the first day in the first week of year, the week indicated by DateTime is 53.I also tried using localtime function and evantually calculating week# based of yearday. Although, it returns week 1, the calculation based on yearday, returns week 1 for the week beginning 04/01/2016 - 08/01/2016, which is actually week 2.
cal -w 1 2016 command starts with week 1 and goes on .. finally shows last week of 2016 as week 53, which as per me is correct.
I think most of you would agree that the cal command output is correct.
Question is, how do I get it right in perl ?
==== #!/usr/bin/env perl #use 5.012; use strict; use warnings; use DateTime; use Time::Local; my $dt = DateTime->new(year => 2016, month => 1, day => 1); print "DateTIme confirms that 01-01-2016 was a => " . $dt->day_name . +"\n"; print "DateTime return the Week no for 01-01-2016 => " . $dt->week . " +\n"; $dt = DateTime->new(year => 2016, month => 1, day => 4); print "DateTIme confirms that 04-01-2016 was a => " . $dt->day_name . +"\n"; print "DateTime return the Week no for 04-01-2016 => " . $dt->week . " +\n"; my ($MONTHDAY, $WEEKDAY, $YEARDAY) = (localtime (timelocal(0,0,0,1,0,2 +016)))[3,6,7]; my $WEEKNUM = int($YEARDAY / 7) + 1; print "using localtime to calculate weekno (01/01/2016) => " . $WEEKNU +M . "\n"; ($MONTHDAY, $WEEKDAY, $YEARDAY) = (localtime (timelocal(0,0,0,4,0,2016 +)))[3,6,7]; $WEEKNUM = int($YEARDAY / 7) + 1; print "using localtime to calculate weekno (04/01/2016) => " . $WEEKNU +M . "\n"; print "UNIX system cal command output for 1st month \n"; system("cal -w 1 2016"); ===
Output:
Perl DateTIme confirms that 01-01-2016 was a => Friday
Perl DateTime return the Week no for 01-01-2016 => 53
Perl DateTIme confirms that 04-01-2016 was a => Monday
Perl DateTime return the Week no for 04-01-2016 => 1
using localtime to calculate weekno (01/01/2016) => 1
using localtime to calculate weekno (04/01/2016) => 1
UNIX system cal command output for 1st month
January 2016
Su Mo Tu We Th Fr Sa
1 1 2
2 3 4 5 6 7 8 9
3 10 11 12 13 14 15 16
4 17 18 19 20 21 22 23
5 24 25 26 27 28 29 30
6 31
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Another date question
by haukex (Archbishop) on Aug 27, 2018 at 10:28 UTC | |
|
Re: Another date question
by Corion (Patriarch) on Aug 27, 2018 at 10:33 UTC | |
|
Re: Another date question
by thanos1983 (Parson) on Aug 27, 2018 at 11:00 UTC | |
by SBECK (Chaplain) on Aug 27, 2018 at 12:36 UTC | |
by thanos1983 (Parson) on Aug 27, 2018 at 13:41 UTC | |
|
Re: Another date question
by sshingor (Initiate) on Aug 27, 2018 at 11:31 UTC |