# Copyright (c) 2009 - 2015 Tropo, now part of Cisco
# Released under the MIT license. See the file LICENSE
# for the complete license

answer
require 'time'

#end_time is SpeechTek's start date and time

end_time = Time.local(2010, 8, 2, 9, 0, 0)

#finding the user's area code and subtracting the appropriate hours from serverside GMT

TIME_ZONE_HASH={'242'=>3,'246'=>3,'264'=>3,'268'=>3,'284'=>3,'340'=>3,'441'=>3,'473'=>3,'506'=>3,'664'=>3,'758'=>3,'767'=>3,'784'=>3,'809'=>3,'829'=>3,'868'=>3,'869'=>3,'902'=>3,'201'=>4,'202'=>4,'203'=>4,'207'=>4,'212'=>4,'215'=>4,'216'=>4,'226'=>4,'229'=>4,'231'=>4,'234'=>4,'239'=>4,'240'=>4,'248'=>4,'252'=>4,'260'=>4,'267'=>4,'269'=>4,'276'=>4,'289'=>4,'301'=>4,'302'=>4,'304'=>4,'305'=>4,'313'=>4,'315'=>4,'317'=>4,'321'=>4,'330'=>4,'336'=>4,'339'=>4,'345'=>4,'347'=>4,'351'=>4,'352'=>4,'386'=>4,'401'=>4,'404'=>4,'407'=>4,'410'=>4,'412'=>4,'413'=>4,'416'=>4,'418'=>4,'419'=>4,'423'=>4,'434'=>4,'438'=>4,'440'=>4,'443'=>4,'450'=>4,'470'=>4,'475'=>4,'478'=>4,'484'=>4,'502'=>4,'508'=>4,'513'=>4,'514'=>4,'516'=>4,'517'=>4,'518'=>4,'519'=>4,'540'=>4,'551'=>4,'561'=>4,'567'=>4,'570'=>4,'571'=>4,'574'=>4,'585'=>4,'586'=>4,'603'=>4,'606'=>4,'607'=>4,'609'=>4,'610'=>4,'613'=>4,'614'=>4,'616'=>4,'617'=>4,'631'=>4,'646'=>4,'647'=>4,'649'=>4,'678'=>4,'703'=>4,'704'=>4,'705'=>4,'706'=>4,'716'=>4,'717'=>4,'718'=>4,'724'=>4,'727'=>4,'732'=>4,'734'=>4,'740'=>4,'754'=>4,'757'=>4,'762'=>4,'765'=>4,'770'=>4,'772'=>4,'774'=>4,'781'=>4,'786'=>4,'787'=>4,'802'=>4,'803'=>4,'804'=>4,'807'=>4,'810'=>4,'812'=>4,'813'=>4,'814'=>4,'819'=>4,'828'=>4,'835'=>4,'843'=>4,'845'=>4,'848'=>4,'850'=>4,'856'=>4,'857'=>4,'859'=>4,'860'=>4,'862'=>4,'863'=>4,'864'=>4,'865'=>4,'876'=>4,'878'=>4,'904'=>4,'905'=>4,'906'=>4,'908'=>4,'910'=>4,'912'=>4,'914'=>4,'917'=>4,'919'=>4,'931'=>4,'937'=>4,'939'=>4,'941'=>4,'947'=>4,'954'=>4,'959'=>4,'973'=>4,'978'=>4,'980'=>4,'989'=>4,'204'=>5,'205'=>5,'210'=>5,'214'=>5,'217'=>5,'218'=>5,'219'=>5,'224'=>5,'225'=>5,'228'=>5,'251'=>5,'254'=>5,'256'=>5,'262'=>5,'270'=>5,'281'=>5,'306'=>5,'308'=>5,'309'=>5,'312'=>5,'314'=>5,'316'=>5,'318'=>5,'319'=>5,'320'=>5,'325'=>5,'331'=>5,'334'=>5,'337'=>5,'361'=>5,'402'=>5,'405'=>5,'409'=>5,'414'=>5,'417'=>5,'430'=>5,'432'=>5,'469'=>5,'479'=>5,'501'=>5,'504'=>5,'507'=>5,'512'=>5,'515'=>5,'563'=>5,'573'=>5,'580'=>5,'601'=>5,'605'=>5,'608'=>5,'612'=>5,'615'=>5,'612'=>5,'620'=>5,'630'=>5,'636'=>5,'641'=>5,'651'=>5,'660'=>5,'662'=>5,'682'=>5,'708'=>5,'712'=>5,'713'=>5,'715'=>5,'731'=>5,'763'=>5,'769'=>5,'773'=>5,'779'=>5,'806'=>5,'815'=>5,'816'=>5,'817'=>5,'830'=>5,'832'=>5,'847'=>5,'870'=>5,'901'=>5,'903'=>5,'913'=>5,'918'=>5,'920'=>5,'936'=>5,'940'=>5,'952'=>5,'956'=>5,'972'=>5,'979'=>5,'985'=>5,'208'=>6,'250'=>6,'303'=>6,'307'=>6,'385'=>6,'403'=>6,'406'=>6,'435'=>6,'480'=>6,'505'=>6,'520'=>6,'541'=>6,'575'=>6,'602'=>6,'623'=>6,'701'=>6,'719'=>6,'720'=>6,'780'=>6,'785'=>6,'801'=>6,'915'=>6,'928'=>6,'970'=>6,'206'=>7,'209'=>7,'213'=>7,'253'=>7,'310'=>7,'323'=>7,'360'=>7,'408'=>7,'415'=>7,'424'=>7,'425'=>7,'503'=>7,'509'=>7,'510'=>7,'530'=>7,'559'=>7,'562'=>7,'604'=>7,'619'=>7,'626'=>7,'650'=>7,'661'=>7,'702'=>7,'707'=>7,'714'=>7,'760'=>7,'775'=>7,'778'=>7,'805'=>7,'818'=>7,'831'=>7,'858'=>7,'909'=>7,'916'=>7,'925'=>7,'949'=>7,'951'=>7,'971'=>7,'907'=>8,'808'=>9}
TIME_ZONE_NAME={nil=>'. since you\'re not calling from a regular phone, we can\'t detect your region, so the countdown is in eastern time.',3=>', atlantic time.', 4=>', eastern time.',5=>', central time.',6=>', mountain time.',7=>', pacific time.',8=>', alaskan time.',9=>', ha why an time.'}

def find_time_zone(i)
    t =  Time.now
    if TIME_ZONE_HASH.has_key?("#{i}")
       return t-(60*60*TIME_ZONE_HASH[i])
    else
      return t-(60*60*4)
    end
end

# This takes the callerID and uses the time_zone_offset_hash to get the gmt offset

gmt_offset = TIME_ZONE_HASH[$currentCall.callerID[0,3]]
  
#this returns the timestamp accounting for gmt offset
  
start_time = find_time_zone($currentCall.callerID[0,3])

#determining the amount of time in seconds separating the two

difference = end_time - start_time

#calculating the days, hours, minutes and seconds out of the initial large seconds value generated by 'difference'

days = difference / (60*60*24)
days_remainder = difference % (60*60*24)
hours = days_remainder / (60*60)
hours_remainder = days_remainder % (60 * 60)
minutes = hours_remainder / 60
seconds = hours_remainder % 60

#dropping the values after the decimal

days2 = days.to_i
hours2 = hours.to_i
minutes2 = minutes.to_i
seconds2 = seconds.to_i

options = {:choices => 'yes(yes, 1), no(no,2)',
    :attempts => 2,
    :timeout => 15,
    :voice => 'simon',
    :onBadChoice =>  lambda { |event|
      case event.attempt
        when 1
          say 'Sorry, didn\'t catch that. Let\'s try it again, this time say yes or no.', {:voice => 'simon'}
        when 2
          say 'Sorry, I still don\'t understand. Please give us a call back.', {:voice => 'simon'}
        end
      },  
    :onTimeout =>   lambda { |event|
        case event.attempt
          when 1
            say 'Sorry, couldn\'t hear you. Let\'s try that again.', {:voice => 'simon'}
          when 2
            say 'Sorry, still couldn\'t hear you. Please give us a call back.', {:voice => 'simon'}
          end
        }
        }

result = ask '' + 'Countdown to Speech Tek activated!  Do you want to know how long you have before the party starts?', options

if result.name=='choice'
  case result.value
  when 'yes'
    say days2.to_s + 'days,' + hours2.to_s + 'hours,' + minutes2.to_s + 'minutes and,' + seconds2.to_s + 'seconds' + TIME_ZONE_NAME[gmt_offset] + '. Better get ready!', {:voice => 'simon'}
  when 'no'
    say 'Well, okay then.  I guess I\'ll just go by myself.', {:voice => 'simon'}
  end
end

hangup