My technical and personal observations about softwares and other entities

Tuesday, July 19

Date::Manip difficulties

Date::Manip-Some of the things we have to write down some where to reuse. Its difficult to re-do calculations whenever required. So I am writting some of my required items in this posting.

For the following two questions I can use ParseRecur subroutine defined in Date::Manip subroutine.

The syntax of subroutine is

@dates=&ParseRecur($string [,$base,$date0,$date1,$flags]);

Where $string is of the form Y:M:W:D:H:MN:S and $date0-starting date, $date1 is enddate. $base -the first event that occurs, $flags.-change the behavior of event, but dont bother much,

Also note that $base, $date0 and $date1 should not contain any asterixes..

Following part is cut+paste from "perldoc Date::Manip" command.
Any value "N" to the left of the asterisk refers to the "Nth" one. Any value to the right of the asterisk refers to a value as it appears on a calendar/clock. Values to the right can be listed a single values, ranges (2 numbers separated by a dash "-"), or a comma separated list of values or ranges. In a few cases, negative values are appropriate.


Q. 1. Get the Array of Every Monday between two dates ?

A. 1. Following script can gives the answer to our question.

===============
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;

my $date0 = '07/20/2005'; # 20 July 2005
my $date1 = '11/21/2005'; # 21 Nov 2005
my $base = '07/18/2005'; # 18 th july was Monday..

my $string = "0:0:1:0:0:0:0";

my @dates = map UnixDate($_, "%m\/%d\/%Y"), ParseRecur($string, $base, $date0, $date1);

print "The Mondays between $date0 and $date1 are : @dates\n";

=====================

When executed this gives the result
The Mondays between 07/20/2005 and 11/21/2005 are : 07/25/2005 08/01/2005 08/08/2005 08/15/2005 08/22/2005 08/29/2005 09/05/2005 09/12/2005 09/19/2005 09/26/2005 10/03/2005 10/10/2005 10/17/2005 10/24/2005 10/31/2005 11/07/2005 11/14/2005

Q.2. Get the Array of days betwen two days ?
A. 2. Following example gives the answer.

===============
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;

my $date0 = '07/20/2005'; # 20 July 2005
my $date1 = '8/15/2005'; # 21 Nov 2005
my $base = '07/18/2005'; # 18 th july was some day, we are looking for
# every day..

my $string = "0:0:0:1:0:0:0";

my @dates = map UnixDate($_, "%m\/%d\/%Y"), ParseRecur($string, $base, $date0, $date1);

print "The Days between $date0 and $date1 are : @dates\n";

=============
This gives the result ..
The Days between 07/20/2005 and 8/15/2005 are : 07/20/2005 07/21/2005 07/22/2005 07/23/2005 07/24/2005 07/25/2005 07/26/2005 07/27/2005 07/28/2005 07/29/2005 07/30/2005 07/31/2005 08/01/2005 08/02/2005 08/03/2005 08/04/2005 08/05/2005 08/06/2005 08/07/2005 08/08/2005 08/09/2005 08/10/2005 08/11/2005 08/12/2005 08/13/2005 08/14/2005

Q.3. List only Monday to Friday days between two dates..i.e. Working Days between two dates. ?
A.3. The string $string = "0:0:1*1-5:0:0:0" gives the result.
See the following script.
==========================
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;

my $date0 = '07/20/2005'; # 20 July 2005
my $date1 = '8/15/2005'; # 15 Aug 2005
my $base = '07/18/2005'; # 18 th july was some day, we are looking for
# every day..

my $string = "0:0:1*1-5:0:0:0";

my @dates = map UnixDate($_, "%d\/%b\/%Y"), ParseRecur($string, $base, $date0, $date1);
print "The Working Days between $date0 and $date1 are : @dates\n";

=====================
The script gives the following result...
The Working Days between 07/20/2005 and 8/15/2005 are : 20/Jul/2005 21/Jul/2005 22/Jul/2005 25/Jul/2005 26/Jul/2005 27/Jul/2005 28/Jul/2005 29/Jul/2005 01/Aug/2005 02/Aug/2005 03/Aug/2005 04/Aug/2005 05/Aug/2005 08/Aug/2005 09/Aug/2005 10/Aug/2005 11/Aug/2005 12/Aug/2005
=================

No comments: