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


In reply to Another date question by sshingor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.