Add support for parsing unix Timestamps

This commit is contained in:
kaso17 2016-11-02 18:40:18 +01:00
parent 0f0cada1c6
commit dba033b5a5
1 changed files with 17 additions and 21 deletions

View File

@ -75,24 +75,6 @@ namespace Jackett.Utils
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
// http://www.codeproject.com/Articles/33298/C-Date-Time-Parser
public static DateTime FromFuzzyTime(string str, DateTimeRoutines.DateTimeFormat format = DateTimeRoutines.DateTimeFormat.USA_DATE)
@ -130,7 +112,7 @@ namespace Jackett.Utils
{
var time = str.Replace(match.Groups[0].Value, "");
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified);
dt += ParseTimeSpan(time);
dt += TimeSpan.Parse(time);
return dt;
}
@ -140,7 +122,7 @@ namespace Jackett.Utils
{
var time = str.Replace(match.Groups[0].Value, "");
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified);
dt += ParseTimeSpan(time);
dt += TimeSpan.Parse(time);
dt -= TimeSpan.FromDays(1);
return dt;
}
@ -151,11 +133,25 @@ namespace Jackett.Utils
{
var time = str.Replace(match.Groups[0].Value, "");
DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Unspecified);
dt += ParseTimeSpan(time);
dt += TimeSpan.Parse(time);
dt += TimeSpan.FromDays(1);
return dt;
}
try
{
// try parsing the str as an unix timestamp
var unixTimeStamp = long.Parse(str);
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dt = dt.AddSeconds(unixTimeStamp).ToLocalTime();
return dt;
}
catch (FormatException)
{
// it wasn't a timestamp, continue....
}
// add missing year
match = missingYearRegexp.Match(str);
if (match.Success)