private static TimeExtent ExtractTimeExtentFromDate(string strDateTime)
{
if (string.IsNullOrEmpty(strDateTime))
return null;
string[] strsDateTime = strDateTime.Split('T'); // Separator between date and time
string strDate = strsDateTime[0];
string strTime = strsDateTime.Length > 1 ? strsDateTime[1] : null;
// Decode the date
string[] strsDate = strDate.Split('-'); // Y,M,D separator
int year, month = 0, day = 0;
if (!int.TryParse(strsDate[0], out year))
return null;
if (strsDate.Length > 1 && !int.TryParse(strsDate[1], out month))
return null;
if (strsDate.Length > 2 && !int.TryParse(strsDate[2], out day))
return null;
DateTime startTime = new DateTime(year, month > 0 ? month : 1, day > 0 ? day : 1);
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
if (string.IsNullOrEmpty(strTime))
{
// There is no time part --> calculate the end time to get an extent corresponding to the year, the month or the day
DateTime endTime;
if (month == 0)
endTime= startTime.AddYears(1); // TimeExtent is the whole year
else if (day == 0)
endTime = startTime.AddMonths(1); // TimeExtent is the whole month
else
endTime = startTime.AddDays(1); // TimeExtent is the whole day
endTime = endTime.AddSeconds(-1); // so the first second of the next year, month or day is not in the extent
return new TimeExtent(startTime, endTime);
}
else
{
// There is a time part
// Separate time part and offset part ('Z' at the end means UTC i.e. no offset)
int index = strTime.IndexOfAny(new char[] {'Z', '+', '-'});
TimeSpan timeOffsetSpan = new TimeSpan(0);
if (index > 0)
{
if (strTime[index] == '+' || strTime[index] == '-')
{
// Use time offset
string strOffset = strTime.Substring(index + 1);
TimeSpan.TryParse(strOffset, out timeOffsetSpan);
if (strTime[index] == '-')
timeOffsetSpan = -timeOffsetSpan;
}
strTime = strTime.Substring(0, index);
}
TimeSpan timeSpan;
if (!TimeSpan.TryParse(strTime, out timeSpan))
return null;
startTime += timeSpan;
startTime += timeOffsetSpan;
return new TimeExtent(startTime);
}
}