Add support for parsing AM/PM TimeSpans

This commit is contained in:
kaso17 2016-11-01 22:36:23 +01:00 committed by kaso17
parent 70dfe3a021
commit e3c7f3de28
1 changed files with 21 additions and 3 deletions

View File

@ -75,6 +75,24 @@ namespace Jackett.Utils
return DateTime.SpecifyKind(DateTime.Now - timeAgo, DateTimeKind.Local); return DateTime.SpecifyKind(DateTime.Now - timeAgo, DateTimeKind.Local);
} }
public static TimeSpan ParseTimeSpan(string time)
{
TimeSpan offset = TimeSpan.Zero;
if (time.EndsWith("AM"))
{
time = time.Substring(0, time.Length - 2);
}
else if (time.EndsWith("PM"))
{
time = time.Substring(0, time.Length - 2);
offset = TimeSpan.FromHours(12);
}
var ts = TimeSpan.Parse(time);
ts += offset;
return ts;
}
// Uses the DateTimeRoutines library to parse the date // Uses the DateTimeRoutines library to parse the date
// http://www.codeproject.com/Articles/33298/C-Date-Time-Parser // http://www.codeproject.com/Articles/33298/C-Date-Time-Parser
public static DateTime FromFuzzyTime(string str, DateTimeRoutines.DateTimeFormat format = DateTimeRoutines.DateTimeFormat.USA_DATE) public static DateTime FromFuzzyTime(string str, DateTimeRoutines.DateTimeFormat format = DateTimeRoutines.DateTimeFormat.USA_DATE)
@ -112,7 +130,7 @@ namespace Jackett.Utils
{ {
var time = str.Replace(match.Groups[0].Value, ""); var time = str.Replace(match.Groups[0].Value, "");
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified); DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified);
dt += TimeSpan.Parse(time); dt += ParseTimeSpan(time);
return dt; return dt;
} }
@ -122,7 +140,7 @@ namespace Jackett.Utils
{ {
var time = str.Replace(match.Groups[0].Value, ""); var time = str.Replace(match.Groups[0].Value, "");
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified); DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified);
dt += TimeSpan.Parse(time); dt += ParseTimeSpan(time);
dt -= TimeSpan.FromDays(1); dt -= TimeSpan.FromDays(1);
return dt; return dt;
} }
@ -133,7 +151,7 @@ namespace Jackett.Utils
{ {
var time = str.Replace(match.Groups[0].Value, ""); var time = str.Replace(match.Groups[0].Value, "");
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified); DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified);
dt += TimeSpan.Parse(time); dt += ParseTimeSpan(time);
dt += TimeSpan.FromDays(1); dt += TimeSpan.FromDays(1);
return dt; return dt;
} }