public static DateTime ValidateTimestamp(object formattedValue)
{
string value = formattedValue as string;
if (String.IsNullOrEmpty(value))
{
throw new FormatException("Timestamp must not be null.");
}
DateTime timestamp = new DateTime(DateTime.Now.Year, 1, 1);
string[] fields = value.Split(':');
if (fields == null || fields.Length < 3)
{
throw new FormatException("Timestamp must have format 'HH:MM:SS'");
}
ushort hours = Convert.ToUInt16(fields[0]);
timestamp = timestamp.AddHours(hours);
if (hours > 23)
{
throw new FormatException("The hour must be less than 24.'");
}
ushort minutes = Convert.ToUInt16(fields[1]);
timestamp = timestamp.AddMinutes(minutes);
if (minutes > 59)
{
throw new FormatException("The minute must be less than 60.'");
}
var secondsWithMs = fields[2].Split('.');
double seconds = Convert.ToDouble(secondsWithMs[0]);
timestamp = timestamp.AddSeconds(seconds);
if (seconds > 60)
{
throw new FormatException("The second must be less than 60.'");
}
if (secondsWithMs.Length == 2)
{
double milliseconds = Convert.ToDouble(secondsWithMs[1]);
timestamp.AddMilliseconds(milliseconds);
}
return timestamp;
}