Skip to content

Commit

Permalink
Item13581: TimeCalcPlugin enhance TIMESHOWSTORE with relative time an…
Browse files Browse the repository at this point in the history
…d add CALENDARDAYS
  • Loading branch information
KennethLavrsen committed Jul 29, 2015
1 parent e0fcad4 commit 1ef2570
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 5 deletions.
15 changes: 13 additions & 2 deletions data/System/TimeCalcPlugin.txt
Expand Up @@ -25,6 +25,8 @@ Some of the features are

%INCLUDE{"VarTIMESHOWSTORE"}%

%INCLUDE{"VarCALENDARDAYS"}%

---++ Time Format Tokens

For macros where a time format can be specified the following tokens are used
Expand Down Expand Up @@ -89,6 +91,12 @@ You can define a preference setting TIMECALCPLUGIN_HOLIDAYS in the current topic
* %<nop>TIMESHOWSTORE{"$day $month $year" time="$shipdate"}%
* Returns (value if installed is 2011-01-05) %TIMESHOWSTORE{"$year-$mo-$day" time="$shipdate"}%

* %<nop>TIMESHOWSTORE{time="12 Jun 2015" delta="last Sunday"}%
* Returns (value if installed is 08 Jun 2015) %TIMESHOWSTORE{time="12 Jun 2015" delta="last Monday"}%

* %<nop>CALENDARDAYS{startdate="23 Nov 2010" enddate="10 Jan 2011"}%
* Returns (value if installed is 48) %CALENDARDAYS{startdate="23 Nov 2010" enddate="10 Jan 2011"}%

Example of project plan. The test cannot be started until both part A and part B of the product have been built. The plugin calculates the date for Test Complete based on the critical path of the project.

| *Milestone* | *Raw macro* | *Actual result <br />(if installed)* | *Simulated result* |
Expand All @@ -114,7 +122,9 @@ It is set by listing the names of the weekdays in English separated by commas.

E.g. to define a work week with Friday and Saturday off define {TimeCalcPlugin}{WorkingDays} as Sunday, Monday, Tuesday, Wednesday, Thursday

For a 7 day work week simply list all 7 days.
For a 7 day work week simply list all 7 days. Or use the CALENDARDAYS macro which does not use working days or holidays.

The plugin requires the CPAN library Time::ParseDate. Redhat/Centos users can run 'yum install perl-Time-ParseDate-2013' with the 'epel' repo enabled. Debian based distributions can install libtime-modules-perl

---++ Installation

Expand All @@ -123,11 +133,12 @@ For a 7 day work week simply list all 7 days.
---++ Plugin Info

| Plugin Author(s): | Foswiki:Main.KennethLavrsen |
| Copyright: | &copy; 2010, Kenneth Lavrsen and Foswiki Contributors |
| Copyright: | &copy; 2010-2015, Kenneth Lavrsen and Foswiki Contributors |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; |
| 15 Jul 2015 (1.5) | Added CALENDARDAYS to the plugin. Extended the functionality of TIMESHOWSTORE to also accept relative dates as a delta parameter. Plugin now requires the CPAN module Time::ParseDate |
| 24 Jan 2011 (1.4) | Document the store parameter for ADDWORKINGDAYS |
| 05 Dec 2010 (1.3) | Add preference setting TIMECALCPLUGIN_HOLIDAYS so you do not have the specify the holidays in every single ADDWORKINGDAYS macro |
| 26 Nov 2010 (1.2) | ADDWORKINGDAYS now accepts multiple input dates or variables and will calculates based on the date that defines the critical path of the project |
Expand Down
12 changes: 12 additions & 0 deletions data/System/VarCALENDARDAYS.txt
@@ -0,0 +1,12 @@
#VarCALENDARDAYS
---+++ CALENDARDAYS{...} -- calculate calendar days
* Calculates the number of calendar days between two dates
* Syntax: =%<nop>CALENDARDAYS{startdate="date|$variable" enddate="date|$variable" includestart="on|off" includeend="on|off"}%=
* Supported parameters:
| *Parameter:* | *Description:* | *Default:* |
| startdate="date" | date can be in any format defined in %SYSTEMWEB%/TimeSpecifications (time is ignorred), or it can be the name of a !TimeCalcPlugin storage name prefixed by a '$'. E.g. $myvar | today |
| enddate="date" | date can be in any format defined in %SYSTEMWEB%/TimeSpecifications (time is ignorred), or it can be the name of a !TimeCalcPlugin storage name prefixed by a '$'. E.g. $myvar | today |
| includestart="on or off" | Include the start date in the calculation | "off" |
| includeend="on or off" | Include the end date in the calculation | "on" |
* Example: %<nop>CALENDARDAYS{startdate="23 Nov 2010" enddate="10 Jan 2011"}%
* This macro accepts !TimeCalcPlugin storage variables as input dates but it cannot store any values because the result is not a date.
90 changes: 87 additions & 3 deletions lib/Foswiki/Plugins/TimeCalcPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2010 Kenneth Lavrsen, kenneth@lavrsen.dk
# Copyright (C) 2010-2015 Kenneth Lavrsen, kenneth@lavrsen.dk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -36,12 +36,13 @@ use warnings;
use Foswiki::Func (); # The plugins API
use Foswiki::Plugins (); # For the API version
use Time::Local;
use Time::ParseDate (); # For relative dates

# $VERSION is referred to by Foswiki, and is the only global variable that
# *must* exist in this package. This should always be in the format
# $Rev: 10080 (2010-11-26) $ so that Foswiki can determine the checked-in status of the
# extension.
our $VERSION = '$Rev: 10080 (2010-11-26) $';
our $VERSION = '1,5';

# $RELEASE is used in the "Find More Extensions" automation in configure.
# It is a manually maintained string used to identify functionality steps.
Expand All @@ -53,7 +54,7 @@ our $VERSION = '$Rev: 10080 (2010-11-26) $';
# date - a date in 1 Jun 2009 format. Three letter English month names only.
# Note: it's important that this string is exactly the same in the extension
# topic - if you use %$RELEASE% with BuildContrib this is done automatically.
our $RELEASE = '1.4';
our $RELEASE = '1.5';

# Short description of this plugin
# One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
Expand Down Expand Up @@ -117,6 +118,7 @@ sub initPlugin {
Foswiki::Func::registerTagHandler( 'WORKINGDAYS', \&_WORKINGDAYS );
Foswiki::Func::registerTagHandler( 'ADDWORKINGDAYS', \&_ADDWORKINGDAYS );
Foswiki::Func::registerTagHandler( 'TIMESHOWSTORE', \&_TIMESHOWSTORE );
Foswiki::Func::registerTagHandler( 'CALENDARDAYS', \&_CALENDARDAYS );

# Plugin correctly initialized
return 1;
Expand Down Expand Up @@ -374,6 +376,20 @@ sub _TIMESHOWSTORE {
}
}
$datetime = time() unless defined $datetime;

my $relativeTime = defined $params->{delta} ?
$params->{delta} :
0;

my $calculatedTime = Time::ParseDate::parsedate( $relativeTime,
NO_RELATIVE => 0,
DATE_REQUIRED => 1,
UK => 1,
NOW => $datetime,
PREFER_FUTURE => 1,
GMT => 1 );

$datetime = $calculatedTime if defined $calculatedTime;

my $storageBin = $params->{store};

Expand All @@ -382,4 +398,72 @@ sub _TIMESHOWSTORE {
return Foswiki::Time::formatTime($datetime, $formatString, gmtime);
}


sub _CALENDARDAYS {
my($session, $params, $theTopic, $theWeb) = @_;
# $session - a reference to the Foswiki session object
# (you probably won't need it, but documented in Foswiki.pm)
# $params= - a reference to a Foswiki::Attrs object containing
# parameters.
# This can be used as a simple hash that maps parameter names
# to values, with _DEFAULT being the name for the default
# (unnamed) parameter.
# $topic - name of the topic in the query
# $web - name of the web in the query
# $topicObject - a reference to a Foswiki::Meta object containing the
# topic the macro is being rendered in (new for foswiki 1.1.x)
# Return: the result of processing the macro. This will replace the
# macro call in the final text.

# For example, %EXAMPLETAG{'hamburger' sideorder="onions"}%
# $params->{_DEFAULT} will be 'hamburger'
# $params->{sideorder} will be 'onions'


# To do - we need to be able to also accept serialized date
my $startdate = $params->{startdate};
if ( defined $startdate ) {
if ( $startdate =~ /^\s*\$(\w+)/ ) {
# if storage does exist the startdate is undefined
$startdate = $storage{ $1 };
$startdate = _returnNoonOfDate( $startdate ) if defined $startdate;
}
else {
$startdate = _returnNoonOfDate( Foswiki::Time::parseTime( $startdate ) );
}
}
$startdate = _returnNoonOfDate( time() ) unless defined $startdate;

my $enddate = $params->{enddate};
if ( defined $enddate ) {
if ( $enddate =~ /^\s*\$(\w+)/ ) {
# if storage does exist the startdate is undefined
$enddate = $storage{ $1 };
$enddate = _returnNoonOfDate( $enddate ) if defined $enddate;
}
else {
$enddate = _returnNoonOfDate( Foswiki::Time::parseTime( $enddate ) );
}
}
$enddate = _returnNoonOfDate( time() ) unless defined $enddate;

my $includestart = defined $params->{includestart} ?
Foswiki::Func::isTrue( $params->{includestart} ) : 0;
my $includeend = defined $params->{includeend} ?
Foswiki::Func::isTrue( $params->{includeend} ) : 1;

# Calculate working days between two times.
# Times are standard system times (secs since 1970).

# We allow the two dates to be swapped around
( $startdate, $enddate ) = ( $enddate, $startdate ) if ( $startdate > $enddate );
use integer;
$startdate -= 86400 if $includestart;
$enddate -= 86400 unless $includeend;
my $elapsed_days = int( ( $enddate - $startdate ) / 86400 );

return $elapsed_days;

}

1;
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/TimeCalcPlugin/MANIFEST
Expand Up @@ -3,6 +3,7 @@ data/System/TimeCalcPlugin.txt 0644 Documentation
data/System/VarWORKINGDAYS.txt 0644 Documentation
data/System/VarADDWORKINGDAYS.txt 0644 Documentation
data/System/VarTIMESHOWSTORE.txt 0644 Documentation
data/System/VarCALENDARDAYS.txt 0644 Documentation
lib/Foswiki/Plugins/TimeCalcPlugin.pm 0644 Perl module
lib/Foswiki/Plugins/TimeCalcPlugin/Config.spec 0644 Perl module

0 comments on commit 1ef2570

Please sign in to comment.