From 4c46ddbf7d32a0ca958a40869fb8f420bef32de7 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Thu, 11 Jul 2013 13:13:55 -0700 Subject: [PATCH] Do not use `date -d` as it's not portable Change shlib.sh and jobd.pl to use a new parse_date function in Util.pm. It uses Time::Local gmtime and some string matching to correctly parse the date in a portable fashion. Currently it only handles the "date '+%a, %d %b %Y %T %z'" format but that's all it needs to at this point. --- Girocco/Util.pm | 27 ++++++++++++++++++++++++++- jobd/jobd.pl | 3 ++- shlib.sh | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Girocco/Util.pm b/Girocco/Util.pm index 1e2bb48..b3f7511 100644 --- a/Girocco/Util.pm +++ b/Girocco/Util.pm @@ -4,6 +4,7 @@ use strict; use warnings; use Girocco::Config; +use Time::Local; BEGIN { use base qw(Exporter); @@ -12,7 +13,7 @@ BEGIN { filedb_atomic_append filedb_atomic_edit filedb_atomic_grep valid_email valid_email_multi valid_repo_url valid_web_url url_path - projects_html_list); + projects_html_list parse_date); } @@ -224,4 +225,28 @@ EOT return ($count || (defined($options->{emptyok}) && $options->{emptyok})) ? $html : ''; } +my %_month_names; +BEGIN { + %_month_names = ( + jan => 0, feb => 1, mar => 2, apr => 3, may => 4, jun => 5, + jul => 6, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11 + ); +} + +# Should be in "date '+%a, %d %b %Y %T %z'" format as saved to lastgc, lastrefresh and lastchange +# The leading "%a, " is optional, returns undef if unrecognized date +sub parse_date { + my $dstr = shift || ''; + $dstr = $1 if $dstr =~/^[^\s]+,\s*(.*)$/; + return undef unless $dstr =~ + /^\s*(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+([+-]\d{4})/; + my ($d,$b,$Y,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7); + my $m = $_month_names{lc($b)}; + return undef unless defined($m); + my $seconds = timegm(0+$S, 0+$M, 0+$H, 0+$d, 0+$m, $Y-1900); + my $offset = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2))); + $offset = -$offset if substr($z,0,1) eq '-'; + return $seconds - $offset; +} + 1; diff --git a/jobd/jobd.pl b/jobd/jobd.pl index e213793..3acde5c 100755 --- a/jobd/jobd.pl +++ b/jobd/jobd.pl @@ -14,6 +14,7 @@ use POSIX ":sys_wait_h"; use Girocco::Config; use Girocco::Project; use Girocco::User; +use Girocco::Util; # Options my $quiet; @@ -102,7 +103,7 @@ sub is_operation_uptodate { my ($project, $which, $threshold) = @_; my $path = get_project_path($project); my $timestamp = `GIT_DIR="$path" $Girocco::Config::git_bin config "gitweb.$which"`; - my $unix_ts = `date +%s -d "$timestamp"`; + my $unix_ts = parse_date($timestamp); (time - $unix_ts) <= $threshold ? $timestamp : undef; } diff --git a/shlib.sh b/shlib.sh index 8c3ea42..873c8e5 100644 --- a/shlib.sh +++ b/shlib.sh @@ -93,7 +93,7 @@ config_set_raw() { check_interval() { od="$(config_get "$1")" [ -n "$od" ] || return 1 - os="$(date +%s -d "$od")" + os="$(perl -I@basedir@ -MGirocco::Util -e "print parse_date('$od')")" [ -n "$os" ] || return 1 ns="$(date +%s)" [ $ns -lt $(($os+$2)) ] -- 2.11.4.GIT