Skip to content

Commit

Permalink
Item13097: remove dependency on CPAN modules
Browse files Browse the repository at this point in the history
This fixes duration maths and removes some of DateTime's limitations.
  • Loading branch information
MichaelDaum committed Nov 17, 2014
1 parent e08f287 commit 460db1c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 368 deletions.
56 changes: 13 additions & 43 deletions data/System/TimeSincePlugin.txt
@@ -1,29 +1,7 @@
%META:TOPICINFO{author="ProjectContributor" date="1407719364" format="1.1" version="1"}%
<!--

PLEASE DO NOT EDIT THIS TOPIC

It is automatically generated from the subversion repository, and any changes
you make will simply be overwritten the next time a release is generated.

Instead, you could check your fix in, raise a bug in the Bugs web, or mail the author.
-->
%META:TOPICINFO{author="ProjectContributor" date="1416229558" format="1.1" version="1"}%
---+!! %TOPIC%

This plugin is inspired by the <nop>WordPress <nop>TimeSince plugin.
It which can be used to display a time difference in a human readable way, for instance to tell you that a topic has been published "2 days, 3 hours ago".

There are some problems with the original
[[http://binarybonsai.com/wordpress/time-since][TimeSince plugin for WordPress]].
For example, it was only able to display a time diff with at most
two expressions: "1day, 2hours ago" or "1 week ago" but not "1 day, 2 hours and
3 minutes". It even displays only one time expression if the difference does
not occure in two adjacent time units (time units are year, month, week, day,
hour, minute). If a time interval differs in week and hour the <nop>TimeSince
WP plugin will only display the week difference and not the hours as week and
hour are not adjacent. This is fixed in the TimeSincePlugin for Foswiki. Several
other parameters have been added to give a finer control of the format and the
information displayed.
This plugin displays a duration between two points in time in a human readable way.

%TOC%

Expand All @@ -32,34 +10,28 @@ information displayed.
=%<nop>TIMESINCE%=,
=%<nop>TIMESINCE{[[from]="..."] [to="..."]}%=

Without arguments the time since the current topic has been created will be displayed.

| *Argument* | *Description* |
| =from="&lt;time-experssion>"= | the starting time point, if ommited the current time |
| =to="&lt;time-expression>"= | the ending timepoint, if ommited the current time |
| =units="1...7"= | number of time units to display, default 2; \
(example units="3": 2 months, 1 day and 3 minutes) |
| =topic="&lt;topic-name>"= | topic name to read the revision time from in case no =from= time was specified |
| =web="&lt;web-name>"= | web name to read the revision time from in case no =from= time was specified |
| =from="&lt;time-experssion>"= | the starting time point; defaults to last change time of =topic= |
| =to="&lt;time-expression>"= | the ending timepoint; defaults to current time |
| =units="1...7"= or %BR% =units="years,months,...,seconds"= | number of time units to display (default 2) or list of units to be displayed; \
(example units="3", units="years, months, days") |
| =seconds="on,off"= | switch on/off seconds, default off |
| =minutes="on,off"= | switch on/off minutes |
| =hours="on,off"= | switch on/off hours |
| =days="on,off"= | switch on/off days |
| =weeks="on,off"= | switch on/off weeks |
| =months="on,off"= | switch on/off months |
| =years="on,off"= | switch on/off years |
| =abs="on,off"= | always return a positive difference between =from= and =to= |
| =null="&lt;format-string>"= | to be displayed if the tim difference is null; \
defaults to "about now" |
| =format="&lt;format-string>"= | format of the output, \
defaults to "$time", other usefull settings =format="$time ago"=, \
=format="replied $time later"=, ="format="$time after the incident"= |
| =negformat="&lt;format-string>"= | use this format string if the time difference is negative |

Note, that switching off years and months (or any other two adjacent time unites) won't display the correct duration
when the times differ more than a year, that is: years can't be converted to weeks, the next lower unit.
The following units can be converted to each other:
* year &lt;=&gt; months
* weeks &lt;=&gt; days
* hours &lt;=&gt; minutes

Time expressions can be specified in any of the following format:
* 31 Dec 2001 - 23:59 (Foswiki date)
* 31 Dec 2001
Expand Down Expand Up @@ -89,7 +61,7 @@ Format strings may contain the following:
| %<nop>TIMESINCE{"11 Sep 2001 - 8:45" units="7" seconds="on"}% \
| 4 years, 2 months, 2 days, 5 hours, 50 minutes, 16 seconds \
| %TIMESINCE{"11 Sep 2001 - 8:45" units="7" seconds="on"}% |
| %<nop>TIMESINCE{from="1955/04/18" to="1979-03-14" units="3"}% | 23 years, 11 months, 6 days \
| %<nop>TIMESINCE{from="1955/04/18" to="1979-03-14" units="3"}% | 23 years, 11 months, 1 day \
| %TIMESINCE{from="1955/04/18" to="1979-03-14" units="3"}% |


Expand All @@ -102,21 +74,19 @@ Format strings may contain the following:
-->

| Plugin Author: | Michael Daum |
| Copyright &copy;: | 2005-2009, Michael Daum http://michaeldaumconsulting.com |
| Copyright &copy;: | 2005-2014, Michael Daum http://michaeldaumconsulting.com |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 23 Apr 2009: | converted to foswiki plugin |
| 17 Nov 2014: | fixed duration math by removing dependency on CPAN::DateTime; improved units parameter |
| 23 Apr 2009: | converted to Foswiki plugin |
| 13 Aug 2008: | replaced CPAN:Time::Local with much more powerful and more precise CPAN:DateTime |
| 03 Jan 2008: | improved parsing time formats |
| 13 Oct 2006: | added =negformat= parameter |
| 31 Aug 2006: | added NO_PREFS_IN_TOPIC |
| 27 Apr 2006: | added lazy compilation |
| 11 Nov 2005: | Initial version |
| Dependency: | $Foswiki::Plugins::VERSION 1.1 |
| CPAN Dependencies: | CPAN:DateTime |
| Other Dependencies: | none |
| Perl Version: | 5.005 |
| Plugin Home: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |
27 changes: 7 additions & 20 deletions lib/Foswiki/Plugins/TimeSincePlugin.pm
@@ -1,14 +1,7 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2005-2009 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2005-2014 Michael Daum http://michaeldaumconsulting.com
#
# Adapted from WordPress plugin TimeSince by
# Michael Heilemann (http://binarybonsai.com),
# Dunstan Orchard (http://www.1976design.com/blog/archive/2004/07/23/redesign-time-presentation/),
# Nataile Downe (http://blog.natbat.co.uk/archive/2003/Jun/14/time_since)
#
# Thanks to all of you!!!
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
Expand All @@ -22,23 +15,17 @@
package Foswiki::Plugins::TimeSincePlugin;

use strict;
use vars qw(
$VERSION $RELEASE $NO_PREFS_IN_TOPIC $SHORTSUMMARY
$isInitialized
);
use warnings;

$VERSION = '$Rev$';
$RELEASE = '3.00';
$NO_PREFS_IN_TOPIC = 1;
$SHORTSUMMARY = 'Display time difference in a human readable way';
our $VERSION = '4.00';
our $RELEASE = '4.00';
our $NO_PREFS_IN_TOPIC = 1;
our $SHORTSUMMARY = 'Display time difference in a human readable way';
our $isInitialized;

###############################################################################
sub initPlugin {

if ($Foswiki::Plugins::VERSION < 1.1) {
return 0;
}

Foswiki::Func::registerTagHandler('TIMESINCE', \&handleTimeSince);
return 1;
}
Expand Down

0 comments on commit 460db1c

Please sign in to comment.