/* * GnuDialer - Complete, free predictive dialer * * Complete, free predictive dialer for contact centers. * * Copyright (C) 2003-2008, GnuDialer Project * * Heath Schultz * Richard Lyman * * This program is free software, distributed under the terms of * the GNU General Public License. */ #include #include #include #include #include "isholiday.h" #ifndef TZFILTER #define TZFILTER int localDay() { time_t raw; tm * ptm; time(&raw); ptm = localtime(&raw); return ptm->tm_wday; } std::string getFilter(const std::string & ear, const std::string & lat, const bool & isFedHoliday, const std::string & allowedHours, const int & tzo) { bool debugMode = true; bool useNewFilter = true; std::string theFilter; int gyear,gmonth,gday,gdow,ghour; int lyear,lmonth,lday,ldow,lhour; int isdst = 0; if (debugMode) { std::cout << "allowedhour string: " << allowedHours << std::endl; } //bool isFedHoliday = isHoliday(); time_t raw; tm * ptm; time(&raw); ptm = gmtime(&raw); // We have to compute daylight savings time manually // because gmt doesn't have it, so it won't adjust // and it's not safe to check it for localtime either // because we can't be sure localtime even has dst. if (ptm->tm_mon > 3) isdst = 1; if (ptm->tm_mon == 3) if ((ptm->tm_mday - 1) - ptm->tm_wday > 0) isdst = 1; if (ptm->tm_mon > 9) isdst = 0; if (ptm->tm_mon == 9) if (ptm->tm_mday + (7 - ptm->tm_wday) > 31) isdst = 0; gyear = ptm->tm_year + 1900; gmonth = ptm->tm_mon + 1; gday = ptm->tm_mday; gdow = ptm->tm_wday; ghour = ptm->tm_hour + isdst; if (debugMode) { std::cout << " gyear: " << gyear << " gmonth: " << gmonth << " gday: " << gday << " gdow: " << gdow << " ghour: " << ghour << " isdst: " << isdst << std::endl; } std::ostringstream HourStream; HourStream << ptm->tm_hour + isdst; std::string h = HourStream.str(); time(&raw); ptm = localtime(&raw); lyear = ptm->tm_year + 1900; lmonth = ptm->tm_mon + 1; lday = ptm->tm_mday; ldow = ptm->tm_wday; //lhour = ptm->tm_hour + isdst; lhour = ptm->tm_hour; ldow = ptm->tm_wday; if (debugMode) { std::cout << " lyear: " << lyear << " lmonth: " << lmonth << " lday: " << lday << " ldow: " << ldow << " lhour: " << lhour << " isdst: " << isdst << std::endl; } int difference = 0; int hits = 0; int pos = 0; int end = 0; int target = 0; std::string allowhour; std::string theINString = "IN("; for (unsigned int i = 0; i < 23; i++) { if (allowedHours.find(",",pos) != std::string::npos) { end = allowedHours.find(",", pos); allowhour = allowedHours.substr(pos,end-pos); difference = stoi(allowhour) - lhour; if (difference < -12) { difference = difference + 25; } target = tzo + difference; if (target > 12) { target = target - 25; } if (target < -12) { target = target + 25; } if (debugMode) { std::cout << "found something - pos:" << pos << " end:" << end << " allowhour: " << allowhour << " difference: " << difference << " target: " << itos(tzo + difference) << ((stoi(allowhour) >= lhour) ? " ahead" : " behind") << std::endl; } pos = end+1; theINString += "'"; theINString += itos(target); theINString += "'"; hits++; if (hits >= 1) { theINString += ","; } } } end = allowedHours.find("\n", pos); allowhour = allowedHours.substr(pos,end-pos); difference = stoi(allowhour) - lhour; if (difference < -12) { difference = difference + 25; } target = tzo + difference; if (target > 12) { target = target - 25; } if (target < -12) { target = target + 25; } if (debugMode) { std::cout << "found something - pos:" << pos << " end:" << end << " allowhour: " << allowhour << " difference: " << difference << " target: " << itos(tzo + difference) << ((stoi(allowhour) >= lhour) ? " ahead" : " behind") << std::endl; } theINString += "'"; theINString += itos(target); hits++; if (hits == 0) { theINString += "'"; difference = -99; theINString += itos(difference); } theINString += "')"; if (debugMode) { std::cout << " theINString: " << theINString << std::endl; } if (debugMode) { std::cout << " lhour minus h: " << (lhour - stoi(h)) << std::endl; } std::string daytype = "wday"; if (isFedHoliday) daytype = "hol"; else { if (ldow == 0) daytype = "sun"; else if (ldow == 6) daytype = "sat"; } if (useNewFilter) { theFilter = "( tzl "; theFilter += theINString; theFilter += " OR tzh "; theFilter += theINString; theFilter += " )"; if (debugMode) std::cout << "NEW Timezone filter: " << theFilter << std::endl; } else { theFilter = "(((tzl+" + h + "<0 AND tzl+" + h + "+24>=" + ear + ") OR (tzl+" + h + ">=0 AND tzl+" + h + ">=" + ear + ")) "; theFilter += "AND "; theFilter += "((tzh+" + h + "<0 AND tzh+" + h + "+24<" + lat + ") OR (tzh+" + h + ">=0 AND tzh+" + h + "<" + lat + "))) "; theFilter += "AND "; theFilter += "(((tzl+" + h + "<0 AND tzl+" + h + "+24>=" + daytype + "l) OR (tzl+" + h + ">=0 AND tzl+" + h + ">=" + daytype + "l)) "; theFilter += "AND "; theFilter += "((tzh+" + h + "<0 AND tzh+" + h + "+24<" + daytype + "h) OR (tzh+" + h + ">=0 AND tzh+" + h + "<" + daytype + "h))) AND tzl!=0 "; if (debugMode) std::cout << "OLD Timezone filter: " << theFilter << std::endl; } //NOTES Section //compute allowed hours into the tz's used below //h = utc+dst hour //allowed hours = 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 //AM 0 1 2 3 4 5 6 7 8 9 10 11 //AM 0-1 1-2 2-3 3-4 4-5 5-6 6-7 7-8 8-9 9-10 10-11 11-12 //PM 12-13 13-14 14-15 15-16 16-17 17-18 18-19 19-20 20-21 21-22 22-23 23-24 //PM 12 13 14 15 16 17 18 19 20 21 22 23 // if localhour is 14 and localhour - h = -8 , allowed hours 9,10,13,14,15 = -13,-12,-9,-8,-7 //while allowed hours // 9,10,13 should be dropped, and 14,15 = -8,-7 //theFilter = "( tzl IN(-13,-12,-9,-8,-7) OR tzh IN(-13,-12,-9,-8,-7) )"; //AND ( tzl IN(,'-3','-2','-1','2','3','4','5','7') OR tzh IN(,'-3','-2','-1','2','3','4','5','7') ) return theFilter; } #endif