From c5bda0a596d9f8c6e1b3767c1091b3e403ff99c8 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Fri, 10 Jul 2015 16:36:41 +0000
Subject: replaced fetching of current week by static calculation algorightm


diff --git a/rhimport-rs b/rhimport-rs
index a593f1d..e528b5b 100755
--- a/rhimport-rs
+++ b/rhimport-rs
@@ -77,17 +77,7 @@ if(!$to_cart) {
   exit 1;
 }
 
-my $curweek = get("http://airplay/getweek.php");
-if(!defined $curweek || $curweek !~ /\d/ ) {
-  print "can't fetch current week info!\n";
-  $dbh->disconnect();
-  exit 1;
-}
-if($curweek < 1 || $curweek > 4) {
-  print "current week($curweek) out of bounds!\n";
-  $dbh->disconnect();
-  exit 1;
-}
+my $curweek = get_rd_week();
 my $nextweek = $curweek == 4 ? 1 : $curweek + 1;
 
 my @today = Date::Calc::Today();
diff --git a/rhimport-sm b/rhimport-sm
index aaa71f2..2466154 100755
--- a/rhimport-sm
+++ b/rhimport-sm
@@ -70,17 +70,7 @@ if(scalar(@allowed_dbs) != 1) {
 my $dropbox = $allowed_dbs[0]->{'PATH'};
 my $to_cart = $allowed_dbs[0]->{'TO_CART'};
 
-my $curweek = get("http://airplay/getweek.php");
-if(!defined $curweek || $curweek !~ /\d/ ) {
-  print "can't fetch current week info!\n";
-  $dbh->disconnect();
-  exit 1;
-}
-if($curweek < 1 || $curweek > 4) {
-  print "current week($curweek) out of bounds!\n";
-  $dbh->disconnect();
-  exit 1;
-}
+my $curweek = get_rd_week();
 
 my @today = Date::Calc::Today();
 my $dow = Date::Calc::Day_of_Week(@today);
diff --git a/rhimport-sv b/rhimport-sv
index 72290c8..1c05f04 100755
--- a/rhimport-sv
+++ b/rhimport-sv
@@ -70,17 +70,7 @@ if(scalar(@allowed_dbs) != 1) {
 my $dropbox = $allowed_dbs[0]->{'PATH'};
 my $to_cart = $allowed_dbs[0]->{'TO_CART'};
 
-my $curweek = get("http://airplay/getweek.php");
-if(!defined $curweek || $curweek !~ /\d/ ) {
-  print "can't fetch current week info!\n";
-  $dbh->disconnect();
-  exit 1;
-}
-if($curweek < 1 || $curweek > 4) {
-  print "current week($curweek) out of bounds!\n";
-  $dbh->disconnect();
-  exit 1;
-}
+my $curweek = get_rd_week();
 
 my @today = Date::Calc::Today();
 my $dow = Date::Calc::Day_of_Week(@today);
diff --git a/rhimport.pm b/rhimport.pm
index 79c921e..6181f56 100644
--- a/rhimport.pm
+++ b/rhimport.pm
@@ -30,6 +30,9 @@ use IPC::Open3;
 use File::Spec;
 use File::Basename;
 use URI::Escape;
+use POSIX;
+use DateTime;
+use DateTime::TimeZone;
 
 my $ssh_host = "airplay";
 my $ssh_user = "rhimport";
@@ -37,6 +40,33 @@ my $ssh_key_file = "$ENV{'HOME'}/.rhimport/import.key";
 my $ssh_dir = "/programm/.rhimport";
 my $rdimport_wrapper = "/usr/local/bin/dropbox_newfile.pl";
 
+sub get_rd_week
+{
+  #
+  # This computes the current Rivendell Week based on the number
+  # of weeks since epoch.
+  #
+  # Explanation:
+  #  epoch was at 01.01.1970 which was a Thursday.
+  #  Monday in that week is (s-from-epoch + 3*24*60*60) seconds ago.
+  #  This needs to be adjusted by the timezone offset for Europe/Vienna
+  #  which is of course not constant (damn you daylight savings time)
+  #  Divide this by (7*24*60*60) and you get the number of
+  #  weeks since the Monday in the week of epoch adjusted for timezone offsets.
+  #  This week had week number 3 so add an offset of 2 and
+  #  get the modulo of 4. This rounded down gives you the current week
+  #  with 0 meaning Week 1. So add 1 to that number and you will get
+  #  the current RD week.
+  #
+  my $now = DateTime->now();
+  my $tz = DateTime::TimeZone->new(name => 'Europe/Vienna');
+  my $tz_offset = $tz->offset_for_datetime($now);
+  my $sEpoch = $now->epoch() + $tz_offset;  
+  my $week = floor(((($sEpoch + 259200)/604800) + 2) % 4) + 1;
+
+  return $week;
+}
+
 sub check_key_file
 {
   if(-e "$ssh_key_file") {
-- 
cgit v0.10.2