Click here to view or download the code for this example
ASP Code Articles
Show a timed message
Another forum question, Another code article!
Demonstration page
For this we have a completely self-contained package. A class for the OOP buffs.
It's all a matter of timing
First off, there is the problem of timing and server locale, because ASP code runs on the server, everything to do with times and dates is taken from the server clock and the server date settings. Not really helpful if the server is located in one time zone and the website users are in another or as in this case you want to show a message to users in the UK between specific times.
Secondly, vBScript, which the majority of ASP code is written in, has no native functions for time zone offsets, BUT fear not, a bit of trickery with ASP in JScript to the rescue in the form of toUTCxxxx methods.
The output and usage of these are exactly the same as they are in client side javascript.
This bit of jscript is used to put the required time data into session variables.
01: <%@language="jscript"%> 02: <% 03: var today = new Date(); 04: Session("UTCDate") = today.toUTCString(); 05: Session("UTCHour") = today.getUTCHours(); 06: Session("UTCMin") = today.getUTCMinutes(); 07: %>
Take note: If editing the above code, JScript is case sensitive. So "Session" is NOT the same as "session" whereas in case insensitive vBScript it would be.
The Class definition file
Ok a chunk of VbScript OOP code.
01: <% 02: class TimedMessage 03: private m_sMessage 04: private m_bShowMessage 05: private m_bHasOverlap 06: private m_bIsDST 07: private m_iStartTime 08: private m_iEndTime 09: private m_sUTCValue 10: private m_sGMTValue 11: private m_iUTCHour 12: private m_iUTCMin 13: private m_iOffset 14: private sub class_initialize() 15: getTimes() 16: m_iOffset = 0 17: m_bIsDST = false 18: end sub 19: public property let Offset(ByVal value) 20: if value > 12 then value = 12 21: if value < -12 then value = -12 22: m_iOffset = value 23: end property 24: public property get LocalOffset() 25: Offset = m_iOffset 26: end property 27: public property let StartTime(ByVal value) 28: m_iStartTime = value 29: end property 30: public property get StartTime() 31: StartTime = m_iStartTime 32: end property 33: public property let EndTime(ByVal value) 34: m_iEndTime = value 35: end property 36: public property get EndTime() 37: EndTime = m_iEndTime 38: end property 39: public property let IsDST(ByVal value) 40: m_bIsDST = value 41: end property 42: public property get IsDST() 43: IsDST = m_bIsDST 44: end property 45: public property let Message(ByVal value) 46: m_sMessage = value 47: end property 48: public property get Message() 49: Message = m_sMessage 50: end property 51: public property get GMTDate() 52: response.write m_sGMTValue 53: end property 54: public property get UTCDate() 55: response.write m_sUTCValue 56: end property 57: public property get GMTHour() 58: if m_bIsDST then 59: if m_iUTCHour = 23 then 60: GMTHour = 0 61: else 62: GMTHour = m_iUTCHour + 1 63: end if 64: else 65: GMTHour = UTCHour 66: end if 67: end property 68: public property get OffsetHour() 69: OffsetHour = (m_iUTCHour + m_iOffset) & ":" & m_iUTCMin 70: end property 71: public property get OffsetTime() 72: OffsetTime = (m_iUTCHour + m_iOffset) & ":" & m_iUTCMin 73: end property 74: public property get UTCHour() 75: UTCHour = m_iUTCHour 76: end property 77: public property get GMTTime() 78: GMTTime = GMTHour & ":" & m_iUTCMin 79: end property 80: public property get UTCTime() 81: UTCTime = m_iUTCHour & ":" & m_iUTCMin 82: end property 83: public property get ServerTime() 84: ServerTime = FormatDateTime(Time(),VBShortTime) 85: end property 86: 87: public function ShowMessage() 88: if m_iEndTime < m_iStartTime then 89: ' show time crosses midnight 90: m_bHasOverlap = true 91: end if 92: if m_bHasOverlap then 93: if GMTHour <= m_iStartTime then 94: m_bShowMessage = true 95: end if 96: else 97: if GMTHour >= m_iStartTime then 98: m_bShowMessage = true 99: end if 100: end if 101: 102: if m_bHasOverlap then 103: if GMTHour >= m_iEndTime then 104: m_bShowMessage = false 105: end if 106: else 107: if m_iEndTime = GMTHour then 108: m_bShowMessage = false 109: end if 110: end if 111: if m_bShowMessage then 112: ShowMessage = m_sMessage 113: end if 114: end function 115: ' TODO: 116: ' logic needs adding for start times after midnight 117: 'with a finish time that is between midnight and starttime 118: private sub getTimes() 119: server.execute("utc_time.asp") 120: m_sUTCValue = _ replace(session("UTCDate"),"UTC","") 121: m_sGMTValue = _ replace(session("GMTDate"),"UTC","") 122: m_iUTCHour = cint(session("UTCHour")) 123: m_iUTCMin = cint(session("UTCMin")) 124: end sub 125: end class 126: %>
Save the above file as cl_message.asp
Code walkthrough
Variable Declarations
01: ' declare a class name 02: class TimedMessage 03: ' declare the local variables 04: ' using a descriptive name makes for ease of maintenance 05: private m_sMessage 06: private m_bShowMessage 07: private m_bHasOverlap 08: ' hasoverlap is a boolean for the time going over midnight 09: private m_bIsDST 10: ' isdst is a boolean for the required time zone using Daylight Saving 11: ' if set will adjust the time +1 hour 12: private m_iStartTime 13: private m_iEndTime 14: private m_sUTCValue 15: private m_sGMTValue 16: private m_iUTCHour 17: private m_iUTCMin 18: private m_iLocalOffset
Declaring and Naming Variables
NB: I use descriptive names for variables so it is pretty self explanatory when returning to the code months or years later it doesn't take long to work out what they are for.My naming convention is so I know the scope and type of each variable as well from the name. So;
m_ indicates a module wide variable, and l_ would be local to a function or method.
The character following the underscore indicates the type
eg: m_s would be a module wide string and l_b would indicate a local boolean
The rest of the class file is just the property and method declarations used for setting and/or reading the various values used and are pretty self-explanatory.
The ones that should be given a bit more detail with extra commenting are, getTimes() and ShowMessage().
01: private sub getTimes() 02: server.execute("utc_time.asp") 03: ' call the jscript page and 04: 'put the values into session variables 05: m_sUTCValue = _ replace(session("UTCDate"),"UTC","") 06: m_sGMTValue = _ replace(session("GMTDate"),"UTC","") 07: m_iUTCHour = cint(session("UTCHour")) 08: m_iUTCMin = cint(session("UTCMin")) 09: ' retrieve the session variables 10: 'and set them into the variables. 11: end sub 12: public function ShowMessage() 13: if m_iEndTime < m_iStartTime then 14: ' show time crosses midnight 15: m_bHasOverlap = true 16: end if 17: ' check for start and end times crossing midnight 18: 'and set HasOverlap 19: 20: if m_bHasOverlap then 21: if GMTHour <= m_iStartTime then 22: m_bShowMessage = true 23: end if 24: else 25: if GMTHour >= m_iStartTime then 26: m_bShowMessage = true 27: end if 28: end if 29: 30: if m_bHasOverlap then 31: if GMTHour >= m_iEndTime then 32: m_bShowMessage = false 33: end if 34: else 35: if m_iEndTime <= GMTHour then 36: m_bShowMessage = false 37: end if 38: end if 39: if m_bShowMessage then 40: ShowMessage = m_sMessage 41: end if 42: end function 43: ' TODO: 44: ' logic needs adding for start times after midnight 45: 'with a finish time that is between midnight and starttime
How to use the class
This is a basic page showing how to set the class, set the message, start and end times, then show the message if required.
01: <%option explicit%> 02: <!--#include file="cl_message.asp" --> 03: <% 04: ' the file containing the class declaration is included 05: dim m_otm 06: set m_otm = new TimedMessage 07: ' dim variable and instantiate the class object 08: %> 09: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 10: "http://www.w3.org/TR/html4/strict.dtd"> 11: <html> 12: <head> 13: <title>GMT / UTC Time</title> 14: <meta http-equiv="Content-Type" content="text/html; _ charset=iso-8859-1"> 15: <link rel="stylesheet" href="style.css" _ type="text/css"> 16: <style type="text/css"> 17: h1 { 18: color:blue; 19: text-align:center; } 20: </style> 21: <script type="text/javascript"> 22: function checkEnd(hour){ 23: } 24: function checkStart(hour){ 25: } 26: </script> 27: </head> 28: <% 29: m_otm.isdst = false 30: ' set DST OFF 31: m_otm.message = "<h1> It's that time _ already</h1>" 32: ' Define the message to be shown 33: if request.form("set") <> "" then 34: m_otm.starttime = cint(request.form("start")) 35: m_otm.endtime = cint(request.form("end")) 36: else 37: m_otm.starttime = 09 38: m_otm.endtime = 12 39: end if 40: ' set the start time and end time 41: ' uses 24 hour (military) clock 42: %> 43: <body> 44: <h1>UCT Time = 45: <%response.write m_otm.UTCTime() 46: ' display the UTC time 47: %></h1> 48: <% 49: response.write m_otm.showmessage 50: ' call the method to display the message 51: %> 52: <% 53: %> 54: <br> 55: <% 56: with response 57: .write "GMT Hour: " & m_otm.GMThour & ":00" 58: .write "<br>" 59: .write "Start Time: " & m_otm.starttime & ":00" 60: .write "<br>" 61: .write "End Time: " & m_otm.endtime & ":00" 62: end with 63: ' show some of the properties 64: %> 65: <form method="post" action="" _ name="settime"> 66: Start Time:- 67: <select name="start" _ onchange="checkEnd(this.value)"> 68: <% 69: const l_sSelect = "selected" 70: dim l_ix 71: for l_ix = 0 to 23 72: %> 73: <option value="<%=l_ix%>" 74: <% if m_otm.starttime = l_ix then 75: with response 76: .write l_sSelect 77: .write "=" 78: .write chr(34) 79: .write l_sSelect 80: .write chr(34) 81: end with 82: end if 83: %> 84: > 85: <%=l_ix%> 86: <% next %> 87: </select> 88: End time:- 89: <select name="end" _ onchange="checkStart(this.value)"> 90: <% 91: for l_ix = 0 to 23 92: %> 93: <option value="<%=l_ix%>" 94: <% if m_otm.endtime = l_ix then 95: with response 96: .write l_sSelect 97: .write "=" 98: .write chr(34) 99: .write l_sSelect 100: .write chr(34) 101: end with 102: end if 103: %> 104: > 105: <%=l_ix%> 106: <% next %> 107: </select> 108: <br> 109: <input type="submit" name="set" _ value="Set Time"> 110: </form> 111: <p> 112: Select start and end times around the current time to display or hide _ the message. 113: </body> 114: </html> 115: <% set m_otm = Nothing 116: ' release and destroy the class object 117: %>
Demonstration page
Download
Download a zip file with all the code required

