﻿    // This function detects whether it's DST(daylight saving time) or not.
    // see checkTimeZone.html for logic
    function isDST(cDate) {
        // take out time
        var midNight = new Date(cDate.getFullYear(), cDate.getMonth(), cDate.getDate(), 0, 0, 0, 0);
        // convert to UTC time, e.g, it shows date + 7:00:00 UTC below
        var UTC = midNight.toGMTString();
        // get PDT by getting rid of 'UTC' at the end
        var PDT = new Date(UTC.substring(0, UTC.lastIndexOf(" ")-1));
        // convert difference from milliseconds to hours
        var hoursDiff = (midNight - PDT) / (1000 * 60 * 60);
        // DST -7, PST -8
        if(hoursDiff == "-7"){
            return true;
        }
        
        return false;
    }
    
    function getTimeFormat(dateString) {
        // convert string to date object
        var cDate = new Date(dateString); 
            
        if(isDST(cDate)){
            // Buoy time is PST, add 1 hour to show PDT time
            var hour = cDate.getHours() + 1;
            cDate = new Date(cDate.getFullYear(), cDate.getMonth(), cDate.getDate(), hour, cDate.getMinutes(), 0, 0);
            
            return formatTime(cDate) + " PDT";
        }
        
        return formatTime(cDate) + " PST";
    }
    
    function formatTime(cDate){
       var hour   = cDate.getHours();
       var minute = cDate.getMinutes();
       var ampm = "AM";
       if (hour   > 11) { ampm = "PM";             }
       if (hour   > 12) { hour = hour - 12;      }
       if (hour   == 0) { hour = 12;             }
       if (hour   < 10) { hour   = "0" + hour;   }
       if (minute < 10) { minute = "0" + minute; }
       // month is zero-based
       var timeString = cDate.getMonth() + 1 + 
                        "/" + cDate.getDate() + 
                        "/" + cDate.getFullYear() + 
                        " " + 
                        hour +
                        ':' +
                        minute +
                        " " +
                        ampm;
                        
       return timeString;
    }

