Wednesday, July 23, 2008

JavaScript dates and timezone offsets

Working with dates can be problematic when handling dates returned by a remote server. They were returned in GMT format, but when displayed on a page, the time was either ahead or behind (depending on which timezone you are in). For example, if you have the date returned via JSON:

var dates = {"StartDate": new Date(1216944000000),"EndDate": new Date(1217030399000)}

So the date now has the timezone offset added:

dates.StartDate = Fri Jul 25 2008 01:00:00 GMT+0100 (GMT Standard Time)
dates.EndDate = Sat Jul 26 2008 00:59:59 GMT+0100 (GMT Standard Time)

So now, to take into account the timezone offset, getTimezoneOffset() can be used, along with setMinutes() and getMinutes()

dates.StartDate.setMinutes(dates.StartDate.getTimezoneOffset() + dates.StartDate.getMinutes());
dates.EndDate.setMinutes(dates.EndDate.getTimezoneOffset() + dates.EndDate.getMinutes());

For a useful JavaScript library for working with dates (it extends the native Date object), check out Datejs. 30kb download, under an MIT open source license.

There is also another library, a smaller download (so slightly less functionality) available with the jQuery datePicker plugin - http://code.google.com/p/jqueryjs/source/browse/trunk/plugins/datePicker/v2/demo/date.js, under a dual MIT / GPL license. For more localisations (as well as string and array manipulation), visit http://code.google.com/p/jqueryjs/source/browse/trunk/plugins/methods. Note: jQuery is not required for these libraries to work.

GPL Compatible Licenses

There is a useful diagram on the GNU site that lists what licences are compatible with the GNU GPL: A Quick Guide to GPLv3: New Compatible Licenses, plus a comprehensive list with more detailed compatibility information.

Tuesday, July 22, 2008

Link: HTML Agility Pack (.NET)

The HTML Agility Pack is an HTML parser for .NET, supporting XPATH and XSLT parsing. Basically the HTML equivalent of XmlDocument.

Example use (from site):

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]")
{
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
}
doc.Save("file.htm");

Tuesday, July 15, 2008

Portable GIMP and fonts

GIMP (GNU Image Manipulation Program), the open source alternative to Photoshop can be downloaded as a portable version. However, due to being portable, the choice of fonts you can use is limited by what is on the operating system and part of the basic GIMP package, so if you use the font tool and choose a font that is not on another system, you may have problems. Thankfully, fonts are easy to copy (you can copy them from the Fonts applet in the control panel by just dragging them to another folder).

To make your fonts available wherever you use GIMP Portable, you just copy them to E:\PortableApps\GIMPPortable\App\gimp\share\gimp\2.0\fonts (where E: is the drive letter of your portable drive). Then you can go on any Windows PC (even without Office or OpenOffice installed) and still use your own fonts (those which you are licensed for).

Monday, July 07, 2008

JSON and JsonML

JSON (JavaScript Object Notation) is a data interchange format for transferring data between disparate systems. Unlike XML (which is also designed for this), it is far simpler and can be processed more easily by client applications (no validation, or other advanced features of XML) and easier to implement (no special parser needed like XML) - JSON: The Fat-Free Alternative to XML. There are examples of JSON messages (and the XML equivalent) on the JSON Example page. You can validate JSON online using JSONLint.

While its name suggests that it is only for programmers using JavaScript, it is useful for other programming languages as well. The JSON site lists implementations and even hosts code for several languages, e.g. JavaScript, C and Java.

JsonML is a way of transporting XML data by representing it in the JSON syntax. As a result, it can be be used for transmitting XHTML markup (represented as JSON rather than XHTML) which can then be added directly to the document (via the DOM, instead of innerHTML). For jQuery developers, there is a plugin for working with JsonML: JSONML Array to DOM - jQuery.ml

Thursday, July 03, 2008

An overview of jQuery.browser

Update: jQuery.version should have been jQuery.browser.version. Added Firefox 3.0.1

If you are a user of jQuery, you may be aware of the basic browser sniffer that comes with it. However, it may seem to return the wrong version number for certain browsers (Firefox and Safari in particular). This is because it returns the version number of the rendering engine (i.e. the part of the web browser that determines how a page should look and function). To help simplify things, I have created a table of common user agents and what jQuery returns:

BrowserjQuery.browserjQuery.browser.version
Internet Explorer 6jQuery.browser.msie = true6.0
Internet Explorer 7jQuery.browser.msie = true7.0
Firefox 1.5 (first release)jQuery.browser.mozilla = true1.8 or 1.8.0
Firefox 1.5.0.9jQuery.browser.mozilla = true1.8.0.9
Firefox 2.0 (first release)jQuery.browser.mozilla = true1.8.1
Firefox 2.0.0.9jQuery.browser.mozilla = true1.8.1.9
Firefox 3.0jQuery.browser.mozilla = true1.9
Firefox 3.0.1jQuery.browser.mozilla = true1.9.0.1
Safari 2.0.2jQuery.browser.safari = true416.11 / 416.12
Safari 2.0.4jQuery.browser.safari = true418.x - 419
Safari 3.0.xjQuery.browser.safari = true522+, 522.x, 523.x
Safari 3.1.xjQuery.browser.safari = true525.x, 526+
Opera xjQuery.browser.opera = truex

I have listed Opera as 'Opera x' as the version number 'x' is always returned by jQuery.browser.version (so no extra work is required, unlike Firefox and Safari).

This table is based on information from UserAgentString.com. There is a Firefox 1.5.0.12, but I am unsure of the version number, plus there are Safari versions with a slightly different rendering engine version number (i.e. 2.0.2 is not always 416.11). Basically, with Safari, if the version number returned is less than 418, it is Safari 2 or lower and if it is around 525/526 it is Safari 3.1.

Tuesday, July 01, 2008

Link: FreeTechBooks.com

FreeTechBooks.com is a site that offers many computing and engineering related text books (topics include Computer Science, Mathematics, Programming etc) available to download for free./p>