|
||
Inside Technique : Localizing time-based information We store and present all time-based information using Pacific Standard Time (PST) regardless of where you are located. In this article, we introduce two techniques presenting time and dates based on your user's local system time. We also explain and provide the script for a solution we hope to have running on the site. Solving this problem is much simpler than it may seem. To convert the time to the appropriate time zone, the content must be dynamically served, and you must be able to determine the appropriate time zone for your visitor. On SiteExperts.com most pages with time-based information are dynamically generated from our database (ignoring the complexities of our page cache). To generate a locale-specific time we need to convert and render the stored time in the user's local time zone while generating the page. The more complex operation is determining the user's local timezone. We found two approaches:
Asking the user their local time-zone is the easiest approach for retrieving the time-zone. However, it requires the user to log-on the system, does not support roaming where a user travels may travel between time-zones, and also requires an edit preferences screen to change their settings. The latter approach is a little more sophisticated. Instead of asking the user what timezone they are in you use a small client-script to calculate it for you. Javascript has a function on the Date object, getTimezoneOffset, which returns how many minutes the local system is from GMT. If you pass this information to the server, you now have a standard way to convert all your times. The challenge with this approach is communicating this time zone information back to the server. Again, there are atleast two approaches you can take:
In both cases, the script that runs on the server is very similar. You need to convert the time information to GMT time and then readjust it based on the user's settings. Below is a simple server-side ASP script for determining this:
<%
' Your offset from GMT time
' 480 is for Pacific Standard Time
Const localOffset = 480
Dim iUserOffset, sPostDate
' Get the time zone cookie
iUserOffset=request.cookies("tz")
sPostDate = ""
' If the timezone cookie does not exist
' use the default time. This occurs when
' the user has cookies disabled or this is
' the first page they visited
if iUserOffset="" then
iUserOffset=localOffset
sPostDate = " PST"
else
' Get the user's offset value
iUserOffset=cInt(t)
end if
function LocalizeDate(d)
' Call this function each time you want to display a date
LocalizeDate= dateAdd("n", localOffset - iUserOffset,d) & sPostDate
end function
%>
This describes the server-side function. You still need to write a small client-script for storing the timezone offset into the cookie. Below is a short javascript function you should include on top of your pages:
<SCRIPT>
var d = new Date()
if (d.getTimezoneOffset) {
var iMinutes = d.getTimezoneOffset()
document.cookie = "tz=" + iMinutes
}
</SCRIPT>
Below is the current time as generated by our server. The first time you view this page the time and date are in PST. If you refresh the page, the cookie will be set and the time and date should be in the time zone set on your computer. The PST designation will be removed once the cookie is available and we can calculate your local time. 6/19/2013 4:53:19 PM PST Why haven't we implemented this yet?We do not yet use this functionality on the site. While most SiteExperts pages are dynamically generated we have a sophisticated caching mechanism that greatly increases the scalability of our server. By introducing the localized time-zones we either need to enhance our cache to support multiple versions of the same page or remove the cache completely. We are still evaluating and testing the best way to integrate local time into the site. © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |