I have an API which will accept timezone offset as string. I need to convert the timezone to TimeSpan and add the timespan with the data i have which is in UTC. Here is what i'm trying.
private bool TryGetHrAndMinFromTimeZone(string timeZone, out TimeSpan result)
{
try
{
var isPositive = !timeZone.StartsWith("-");
var hrAndMin = string.Concat(timeZone.Where(x => x != '-' && x != '+')).Split(':');
var hr = int.Parse(hrAndMin[0]);
var min = int.Parse(hrAndMin[1]);
result = isPositive ? new TimeSpan(hr, min, 0) : new TimeSpan(-hr, -min, 0);
return true;
}
catch (Exception)
{
throw new Exception(string.Format("Provided TimeZone '{0}' is Invalid ", timeZone));
}
}
Is there any better option to do it?