Skip to content

Commit

Permalink
updated custom.js to accommodate timezones when checking if current t…
Browse files Browse the repository at this point in the history
…ime is during office hours
  • Loading branch information
danpeltier committed Jun 17, 2018
1 parent 000048d commit e8527a3
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions app/js/custom.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// JavaScript Document

// retrieve phone number and guarantee modal content from REST API
var today, starttime, endtime;

// retrieve phone number, office hours, and guarantee modal content from REST API
$.ajax({
url: "https://www.algaecal.com/wp-json/acf/v3/options/options",
dataType: "text",
success:function(data){
var optiondata = JSON.parse(data);

var phonenum = optiondata.acf.default_phone_number;
var guarantee = optiondata.acf["7yr_full_copy"];
var officehours = optiondata.acf.office_hours;
Expand All @@ -14,22 +17,45 @@ $.ajax({

$(".modal-body").html(guarantee);

var today = new Date();
today = new Date();

var starthour = parseInt(parseInt(officehours[today.getDay()].starting_time)/100);
var startmin = starthour*100 - parseInt(officehours[today.getDay()].starting_time);

var endhour = parseInt(parseInt(officehours[today.getDay()].closing_time)/100);
var endmin = endhour*100 - parseInt(officehours[today.getDay()].closing_time);

var starttime = new Date(today.getFullYear(),today.getMonth(),today.getDate(),starthour,startmin,0,0);
var endtime = new Date(today.getFullYear(),today.getMonth(),today.getDate(),endhour,endmin,0,0);
starttime = new Date(today.getFullYear(),today.getMonth(),today.getDate(),starthour,startmin,0,0);

endtime = new Date(today.getFullYear(),today.getMonth(),today.getDate(),endhour,endmin,0,0);

if(today.getTime() >= starttime.getTime() && today.getTime() <= endtime.getTime()){
// show "speak to our bone health specialists message"
$(".speak-msg").show();
}
// calculate offset for timezones (PST or PDT) to ensure that AlgaeCal is actually open (using Vancouver, B.C.)

var tzurl = "https://maps.googleapis.com/maps/api/timezone/json?location=49.246670,-123.094729&timestamp=" + parseInt(today.getTime()/1000) + "&key=AIzaSyACPIqxYiiRjBvsB7uXPST6vFibTXPHKZs";

$.ajax({
url: tzurl,
dataType: "text",
success:function(tzdata){

var timezonedata = JSON.parse(tzdata);

// subtract offsets to get GMT of start and end times of office hours and convert to milliseconds:
var chkstarttime = starttime.getTime() - timezonedata.dstOffset*1000 - timezonedata.rawOffset*1000;
var chkendtime = endtime.getTime() - timezonedata.dstOffset*1000 - timezonedata.rawOffset*1000;

// get offset for current local time and convert to milliseconds
var tz = today.toString().split("GMT")[1].split(" (")[0];
var chktoday = today.getTime() - parseInt(tz)/100*60*60*1000;

// check if current time is truly during open office hours:
if(chktoday >= chkstarttime && chktoday <= chkendtime){
// show "speak to our bone health specialists message"
$(".speak-msg").show();
}
}
});

}
});

Expand Down

0 comments on commit e8527a3

Please sign in to comment.