Skip to content

Commit

Permalink
Item15092: fix use of timezones in compatParseTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Apr 28, 2022
1 parent 544535d commit 167631e
Show file tree
Hide file tree
Showing 27 changed files with 490 additions and 469 deletions.
15 changes: 14 additions & 1 deletion data/System/DateManipPlugin.txt
Expand Up @@ -80,6 +80,10 @@ See the [[https://metacpan.org/pod/distribution/Date-Manip/lib/Date/Manip/Lang/e
%DATETIME{"1511259079"}%

%DATETIME{"1er decembre 1990" language="fr"}%

%NOW%

%TODAY%
</verbatim>

---+++ Durations
Expand Down Expand Up @@ -360,6 +364,14 @@ recurrence is open. Make sure that you restrict the output of returned dates usi
| =footer="..."= | footer string appended to the output in case the recurrence is not empty | |
| =separator="..."= | separator string between returned lines returned | |

---+++ %NOW

returns the current time in epoch seconds

---+++ %TODAY

returns today's time in epoch seconds. Basically this is a shortcut for =%<nop>DATETIME{"today" lang="en" format="$epoch"}%=

---++ Valid formats

Part of the power of Date::Manip is its cheer amount of formats of dates, times and durations it is able to parse. So instead of repeating the documentation available online for this perl package here are simply the links to
Expand All @@ -382,6 +394,7 @@ the real source of the documentation.
---++ Change History

%TABLE{columnwidths="7em" tablewidth="100%"}%
| 28 Apr 2022 | fixed handling of timezones in compatParseTime(); added macros =%NOW= and =%TODAY= |
| 19 Oct 2020 | fixed compatibility check for <nop>DateTimePlugin; added =$epoch= to =%DURATION= macro |
| 12 Nov 2019 | improve compatibility with default time formatting |
| 03 Oct 2018 | performance improvement |
Expand All @@ -395,7 +408,7 @@ the real source of the documentation.
%META:FIELD{name="Release" title="Release" value="%25$RELEASE%25"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/%25TOPIC%25"}%
%META:FIELD{name="Copyright" title="Copyright" value="2017-2020 Michael Daum"}%
%META:FIELD{name="Copyright" title="Copyright" value="2017-2022 Michael Daum"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Home" title="Home" value="https://foswiki.org/Extensions/%25TOPIC%25"}%
%META:FIELD{name="Support" title="Support" value="https://foswiki.org/Support/%25TOPIC%25"}%
13 changes: 10 additions & 3 deletions lib/Foswiki/Plugins/DateManipPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, https://foswiki.org/
#
# DateManipPlugin is Copyright (C) 2017-2020 Michael Daum http://michaeldaumconsulting.com
# DateManipPlugin is Copyright (C) 2017-2022 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -22,8 +22,8 @@ use Foswiki::Func ();
use Foswiki::Time ();
use Error qw(:try);

our $VERSION = '3.00';
our $RELEASE = '19 Oct 2020';
our $VERSION = '4.00';
our $RELEASE = '28 Apr 2022';
our $SHORTDESCRIPTION = 'Date times, durations and recurrences';
our $NO_PREFS_IN_TOPIC = 1;
our $core;
Expand Down Expand Up @@ -60,6 +60,13 @@ sub initPlugin {
Foswiki::Func::registerTagHandler('DATETIME', sub { return getCore(shift)->DATETIME(@_); });
}

Foswiki::Func::registerTagHandler('NOW', sub { return time(); });
Foswiki::Func::registerTagHandler('TODAY', sub { return getCore(shift)->DATETIME({
"_DEFAULT" => "today",
lang => "en",
format => '$epoch',
}); });

Foswiki::Func::registerTagHandler('DURATION', sub { return getCore(shift)->DURATION(@_); });
Foswiki::Func::registerTagHandler('RECURRENCE', sub { return getCore(shift)->RECURRENCE(@_); });

Expand Down
115 changes: 58 additions & 57 deletions lib/Foswiki/Plugins/DateManipPlugin/Core.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, https://foswiki.org/
#
# DateManipPlugin is Copyright (C) 2017-2020 Michael Daum http://michaeldaumconsulting.com
# DateManipPlugin is Copyright (C) 2017-2022 Michael Daum http://michaeldaumconsulting.com
#
# 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 @@ -43,55 +43,11 @@ sub new {
}, $class);

$this->{_numCallsToParseTime} = 0;

$this->{_session} = $session;
$this->init;

return $this;
}

sub init {
my $this = shift;

$this->{_secondsOfUnit}{standard} = {
years => 60*60*24*365.2425,
months => 60*60*2*365.2425,
weeks => 60*60*24*7,
days => 60*60*24,
hours => 60*60,
minutes => 60,
seconds => 1,
};

my $start;
my $end;
my $delta;
my @fields;

$start = $this->getDate();
$start->parse($this->{workDayBeg});
$end = $this->getDate();
$end->parse($this->{workDayEnd});
$delta = $start->calc($end);
@fields = $delta->value();
my $daySecs = ($fields[6]||0) + ($fields[5]||0) * 60 + ($fields[4]||0) * 60 * 60;
#print STDERR "daySecs=$daySecs\n";

my $weekDays = $this->{workWeekEnd} - $this->{workWeekBeg} + 1;
#print STDERR "weekDays=$weekDays\n";

$this->{_secondsOfUnit}{business} = {
years => $daySecs * $weekDays * 52.143,
months => $daySecs * $weekDays * 52.143 / 12,
weeks => $daySecs * $weekDays,
days => $daySecs,
hours => 60*60,
minutes => 60,
seconds => 1,
};

}

sub finish {
my $this = shift;

Expand Down Expand Up @@ -323,7 +279,6 @@ sub compatFormatTime {
# }

# implements Foswiki::Time::parseTime
# defaultLocale ignored for now
sub compatParseTime {
my ($this, $string, $defaultLocal, $params) = @_;

Expand All @@ -334,10 +289,9 @@ sub compatParseTime {
return Foswiki::Time::origParseTime($string, $defaultLocal);
}

# SMELL: currently ignores defaultLocale param

$params ||= {};
$params->{lang} = $this->getLang($params);
$params->{tz} = 'GMT' unless $defaultLocal;
my $date = $this->getDate($params);

$string = _fixDateTimeString($string);
Expand Down Expand Up @@ -372,6 +326,53 @@ sub formatDate {
return $result;
}

sub getSecondsOfUnit {
my ($this, $isBusiness, $unit) = @_;

unless (exists $this->{_secondsOfUnit}) {
#print STDERR "computing seondsOfUnit\n";

$this->{_secondsOfUnit}{standard} = {
years => 60*60*24*365.2425,
months => 60*60*2*365.2425,
weeks => 60*60*24*7,
days => 60*60*24,
hours => 60*60,
minutes => 60,
seconds => 1,
};

my $start;
my $end;
my $delta;
my @fields;

$start = $this->getDate();
$start->parse($this->{workDayBeg});
$end = $this->getDate();
$end->parse($this->{workDayEnd});
$delta = $start->calc($end);
@fields = $delta->value();
my $daySecs = ($fields[6]||0) + ($fields[5]||0) * 60 + ($fields[4]||0) * 60 * 60;
#print STDERR "daySecs=$daySecs\n";

my $weekDays = $this->{workWeekEnd} - $this->{workWeekBeg} + 1;
#print STDERR "weekDays=$weekDays\n";

$this->{_secondsOfUnit}{business} = {
years => $daySecs * $weekDays * 52.143,
months => $daySecs * $weekDays * 52.143 / 12,
weeks => $daySecs * $weekDays,
days => $daySecs,
hours => 60*60,
minutes => 60,
seconds => 1,
};
}

return $this->{_secondsOfUnit}{$isBusiness?"business":"standard"}{$unit};
}

sub delta2sec {
my ($this, $delta, $isBusiness) = @_;

Expand All @@ -384,7 +385,7 @@ sub delta2sec {

foreach my $unit (qw(years months weeks days hours minutes seconds)) {
my $val = $fields[$index++];
$sec += $val * $this->{_secondsOfUnit}{$isBusiness?"business":"standard"}{$unit};
$sec += $val * $this->getSecondsOfUnit($isBusiness, $unit);
}
$sec = abs($sec);

Expand All @@ -409,7 +410,7 @@ sub formatDelta {
foreach my $unit (qw(years months weeks days hours minutes seconds)) {
next unless Foswiki::Func::isTrue($params->{$unit}, $all);

my $factor = $this->{_secondsOfUnit}{$isBusiness?"business":"standard"}{$unit};
my $factor = $this->getSecondsOfUnit($isBusiness, $unit);
my $count = floor($duration / $factor);
_writeDebug("duration=$duration, unit=$unit, seconds in $unit=$factor, count=$count");

Expand Down Expand Up @@ -443,6 +444,7 @@ sub formatDelta {
}
}

return $params->{null} if defined $params->{null} && $result eq '';
return $result;
}

Expand Down Expand Up @@ -509,7 +511,7 @@ sub _getObject {
"WorkDayEnd", $this->{workDayEnd},
);

$obj->config("setdate", "now,$tz") if $tz;
$obj->config("setdate", "zone,$tz") if $tz;
$this->{_cache}{$key} = $obj;
}

Expand Down Expand Up @@ -539,11 +541,11 @@ sub _translateFormat {
my $longdate = '$day $mon $year - $hours:$minutes';

$_[0] =~ s/\$(http|email)/\$wday, \$day \$month \$year \$hour:\$min:\$sec \$tz/gi;
$_[0] =~ s/\$?iso/$iso/g;
$_[0] =~ s/\$?rcs/$rcs/g;
$_[0] =~ s/\$?http/$http/g;
$_[0] =~ s/\$?email/$http/g;
$_[0] =~ s/\$?longdate/$longdate/g;
$_[0] =~ s/[\b\$]?iso\b/$iso/g;
$_[0] =~ s/[\b\$]?rcs\b/$rcs/g;
$_[0] =~ s/[\b\$]?http\b/$http/g;
$_[0] =~ s/[\b\$]?email\b/$http/g;
$_[0] =~ s/[\b\$]?longdate\b/$longdate/g;

$_[0] =~ s/\$year?s?/%Y/g;
$_[0] =~ s/\$ye/%y/g;
Expand Down Expand Up @@ -573,7 +575,6 @@ sub _translateFormat {
$_[0] =~ s/\$offset/%N/g;
$_[0] =~ s/\$epoch/%s/g;


return $_[0];
}

Expand Down
34 changes: 17 additions & 17 deletions locale/DateManipPlugin/Foswiki.pot
Expand Up @@ -16,67 +16,67 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:439
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:440
msgid "and"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:147
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:103
msgid "day"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:148
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:104
msgid "days"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:151
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:107
msgid "hour"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:152
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:108
msgid "hours"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:153
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:109
msgid "minute"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:154
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:110
msgid "minutes"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:144
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:100
msgid "month"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:145
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:146
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:101
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:102
msgid "months"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:402
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:403
msgid "null"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:155
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:111
msgid "second"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:156
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:112
msgid "seconds"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:149
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:105
msgid "week"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:150
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:106
msgid "weeks"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:142
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:98
msgid "year"
msgstr ""

#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:143
#: lib/Foswiki/Plugins/DateManipPlugin/Core.pm:99
msgid "years"
msgstr ""

0 comments on commit 167631e

Please sign in to comment.