Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Another date question

by thanos1983 (Parson)
on Aug 27, 2018 at 11:00 UTC ( #1221182=note: print w/replies, xml ) Need Help??


in reply to Another date question

Hello sshingor,

Welcome to the Monastery. I will agree with the fellow Monks that have already replied to your question. Just to add something minor here on how the week numbers are calculated.

Read the documentation from ISO_8601/Week_dates:

The week number can be described by counting the Thursdays: week 12 co +ntains the 12th Thursday of the year. The ISO week-numbering year starts at the first day (Monday) of week 0 +1 and ends at the Sunday before the new ISO year (hence without overl +ap or gap). It consists of 52 or 53 full weeks. The first ISO week of + a year may have up to three days that are actually in the Gregorian +calendar year that is ending; if three, they are Monday, Tuesday and +Wednesday. Similarly, the last ISO week of a year may have up to thre +e days that are actually in the Gregorian calendar year that is start +ing; if three, they are Friday, Saturday, and Sunday. The Thursday of + each ISO week is always in the Gregorian calendar year denoted by th +e ISO week-numbering year. Examples: Monday 29 December 2008 is written "2009-W01-1" Sunday 3 January 2010 is written "2009-W53-7"

Minor sample of code with Date::Manip:

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $datestr = ParseDate("01/01/2016"); my $secs = UnixDate($datestr,'%s'); say ParseDateString("epoch $secs"); say UnixDate($datestr,"%J"); __END__ $ perl test.pl 2016010100:00:00 2015-W53-5

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Another date question
by SBECK (Hermit) on Aug 27, 2018 at 12:36 UTC

    I agree with others that in most cases, you're better off sticking with the ISO 8601 definitions. But, just in case you're interested, Date::Manip defaults to ISO 8601, but you can specify other definitions of week using some of the config variables that are built in. For example:

    • FirstDay can be used to specify a different day of the week as the first day
    • Jan1Week1 can be used to say that Jan 1 is in the first week of the year
    Most of the time I would discourage using these... but they are there if you need them.

      Hello SBECK,

      Thanks a lot for this information I had no idea about that. Just to add more information in case that someone is interested those variables can be set at the config file. More information can be found in the official documentation Date::Manip::Config/BASIC CONFIGURATION VARIABLES.

      Update: Just for future reference in case that someone is interested to overwrite the ISO 8601 (not recommended). As fellow Monk SBECK indicated we can use the Jan1Week1=1 a small sample of code:

      #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $datestr = ParseDate("01/01/2016"); say UnixDate($datestr,"%J"); Date_Init("Jan1Week1=1"); say UnixDate($datestr,"%J"); __END__ $ perl test.pl 2015-W53-5 2016-W01-5

      In case that someone does not want to define the config file is can load the configurations like this. More information on the official documentation. Sample of config file can be found here Date::Manip::ConfigFile - sample config file.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-11-26 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?