What steps will reproduce the problem?
1. Call DateUtil.parseW3CDTF("2008-01-22T00:00:00Z")
What is the expected output? What do you see instead?
Result date is correct, but time is 7:00PM
Time should be 12:00 AM (midnight)
What version of the product are you using? On what operating system?
Not sure what version. I grabbed from svn February 23, 2008. Windows
Vista Business
Please provide any additional information below.
I fixed the issue, actually two issues, by modifying DateUtil.parseW3CDTF
to now look like the following. I commented my changes.
First change is changing the order of looking for the offset and placing
Zulu last and the default. This is because some Java libraries leaves off
the "Z" when no offset.
Second change is changing the way utc is calculated. It gave the incorrect
result when time is midnight. I changed to simply create a Date object and
let the Date object calculate utc instead of the code.
Here is the full parseW3CDTF method:
public static function parseW3CDTF(str:String):Date
{
var finalDate:Date;
try
{
var dateStr:String = str.substring(0, str.indexOf("T"));
var timeStr:String = str.substring(str.indexOf("T")+1, str.length);
var dateArr:Array = dateStr.split("-");
var year:Number = Number(dateArr.shift());
var month:Number = Number(dateArr.shift());
var date:Number = Number(dateArr.shift());
var multiplier:Number;
var offsetHours:Number;
var offsetMinutes:Number;
var offsetStr:String;
// Modified by Dale Bronk for SpectrumK12. Changed order of finding
offset.
// First check for "+", then "-", then assume "Z". This covers the
instances
// when "Z" is left off of the date string as some Java libraries will
not add
// the "Z" when Zulu or no offset.
// if (timeStr.indexOf("Z") != -1)
// {
// multiplier = 1;
// offsetHours = 0;
// offsetMinutes = 0;
// timeStr = timeStr.replace("Z", "");
// }
// else if (timeStr.indexOf("+") != -1)
// {
// multiplier = 1;
// offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length);
// offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
// offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1,
offsetStr.length));
// timeStr = timeStr.substring(0, timeStr.indexOf("+"));
// }
// // Modified by Dale Bronk for Spectrumk12. Next line use to be
simply "else", now "else if..."
// else if (timeStr.indexOf("-") != -1) // offset is -
// {
// multiplier = -1;
// offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length);
// offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
// offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1,
offsetStr.length));
// timeStr = timeStr.substring(0, timeStr.indexOf("-"));
// }
if (timeStr.indexOf("+") != -1)
{
multiplier = 1;
offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length);
offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1,
offsetStr.length));
timeStr = timeStr.substring(0, timeStr.indexOf("+"));
}
else if (timeStr.indexOf("-") != -1)
{
multiplier = -1;
offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length);
offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1,
offsetStr.length));
timeStr = timeStr.substring(0, timeStr.indexOf("-"));
}
else // No offset, assume Zulu
{
multiplier = 1;
offsetHours = 0;
offsetMinutes = 0;
timeStr = timeStr.replace("Z", "");
}
var timeArr:Array = timeStr.split(":");
var hour:Number = Number(timeArr.shift());
var minutes:Number = Number(timeArr.shift());
var secondsArr:Array = (timeArr.length > 0) ?
String(timeArr.shift()).split(".") : null;
var seconds:Number = (secondsArr != null && secondsArr.length > 0) ?
Number(secondsArr.shift()) : 0;
var milliseconds:Number = (secondsArr != null && secondsArr.length > 0)
? Number(secondsArr.shift()) : 0;
// Modified by Dale Bronk for Spectrumk12. Instead of calculating utc,
I now create a Date and let the
// Date object calculate utc. This is because the the original
calculation of utc was giving an incorrect
// result when the time was "00:00:00" which is midnight (12:00:00 AM).
finalDate = new Date(year, month, date, hour, minutes, seconds,
milliseconds);
var utc : Number = finalDate.getTime();
//var utc:Number = Date.UTC(year, month-1, date, hour, minutes,
seconds, milliseconds);
var offset:Number = (((offsetHours * 3600000) + (offsetMinutes *
60000)) * multiplier);
finalDate = new Date(utc - offset);
if (finalDate.toString() == "Invalid Date")
{
throw new Error("This date does not conform to W3CDTF.");
}
}
catch (e:Error)
{
var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
eStr += "The internal error was: " + e.toString();
throw new Error(eStr);
}
return finalDate;
}