Monday, November 08, 2010 at 12:02 AM.
system.verbs.builtins.date.iso8601StringToDate
on iso8601StringToDate (s) {
<<Changes
<<2/12/03; 6:22:38 PM by JES
<<Created. Given the date as an ISO 8601 formatted string, return it as a Frontier date-type.
<<Example: 1994-11-05T08:15:30-05:00
<<Spec: http://www.w3.org/TR/NOTE-datetime
<<Cribbed from code written by Andre Radke at soap.decode.simpleType.
local (multiplier = 1);
if s beginsWith "-" {
s = string.delete (s, 1, 1);
multiplier = -1};
local (datestring = string.nthField (s, "T", 1));
s = string.delete (s, 1, string.length (datestring) + 1);
local (ixplus, ixminus, tzstring, timestring);
ixplus = string.patternMatch ("+", s);
ixminus = string.patternMatch ("-", s);
if (ixminus > 0) and (ixplus > 0) {
scripterror ("Can't decode \"" + soap.xmlutils.getElementName (adrelement) + "\" element because the format of the timeInstant was invalid.")};
if (ixminus > 0) or (ixplus > 0) {
local (ix = ixminus + ixplus);
timestring = string.mid (s, 1, ix - 1);
tzstring = string.delete (s, 1, ix)}
else {
timestring = s};
local (year = multiplier * number (string.nthField (datestring, "-", 1)));
local (month = number (string.nthField (datestring, "-", 2)));
local (day = number (string.nthField (datestring, "-", 3)));
local (hour = number (string.nthField (timestring, ":", 1)));
local (minute = number (string.nthField (timestring, ":", 2)));
local (second = number (string.mid (string.nthField (timestring, ":", 3), 1, 2)));
local (tzoffset = 60 * (60 * number (string.nthField (tzstring, ":", 1)) + number (string.nthField (tzstring, ":", 2))));
if ixminus {
tzoffset = -1 * tzoffset};
return (date.set (day, month, year, hour, minute, second) - tzoffset + date.getCurrentTimeZone ())}
<<bundle //test code
<<date.iso8601StringToDate ("2003-02-12T18:25:38-08:00")
<<"2/12/03; 6:25:38 PM"
This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.