mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
This commit is contained in:
@@ -9,9 +9,9 @@ namespace Jackett.Common.Utils
|
|||||||
public const string Rfc1123ZPattern = "ddd, dd MMM yyyy HH':'mm':'ss z";
|
public const string Rfc1123ZPattern = "ddd, dd MMM yyyy HH':'mm':'ss z";
|
||||||
|
|
||||||
private static readonly Regex _TimeAgoRegexp = new Regex(@"(?i)\bago", RegexOptions.Compiled);
|
private static readonly Regex _TimeAgoRegexp = new Regex(@"(?i)\bago", RegexOptions.Compiled);
|
||||||
private static readonly Regex _TodayRegexp = new Regex(@"(?i)\btoday([\s,]*|$)", RegexOptions.Compiled);
|
private static readonly Regex _TodayRegexp = new Regex(@"(?i)\btoday(?:[\s,]+(?:at){0,1}\s*|[\s,]*|$)", RegexOptions.Compiled);
|
||||||
private static readonly Regex _TomorrowRegexp = new Regex(@"(?i)\btomorrow([\s,]*|$)", RegexOptions.Compiled);
|
private static readonly Regex _TomorrowRegexp = new Regex(@"(?i)\btomorrow(?:[\s,]+(?:at){0,1}\s*|[\s,]*|$)", RegexOptions.Compiled);
|
||||||
private static readonly Regex _YesterdayRegexp = new Regex(@"(?i)\byesterday([\s,]*|$)", RegexOptions.Compiled);
|
private static readonly Regex _YesterdayRegexp = new Regex(@"(?i)\byesterday(?:[\s,]+(?:at){0,1}\s*|[\s,]*|$)", RegexOptions.Compiled);
|
||||||
private static readonly Regex _DaysOfWeekRegexp = new Regex(@"(?i)\b(monday|tuesday|wednesday|thursday|friday|saturday|sunday)\s+at\s+", RegexOptions.Compiled);
|
private static readonly Regex _DaysOfWeekRegexp = new Regex(@"(?i)\b(monday|tuesday|wednesday|thursday|friday|saturday|sunday)\s+at\s+", RegexOptions.Compiled);
|
||||||
private static readonly Regex _MissingYearRegexp = new Regex(@"^(\d{1,2}-\d{1,2})(\s|$)", RegexOptions.Compiled);
|
private static readonly Regex _MissingYearRegexp = new Regex(@"^(\d{1,2}-\d{1,2})(\s|$)", RegexOptions.Compiled);
|
||||||
private static readonly Regex _MissingYearRegexp2 = new Regex(@"^(\d{1,2}\s+\w{3})\s+(\d{1,2}\:\d{1,2}.*)$", RegexOptions.Compiled); // 1 Jan 10:30
|
private static readonly Regex _MissingYearRegexp2 = new Regex(@"^(\d{1,2}\s+\w{3})\s+(\d{1,2}\:\d{1,2}.*)$", RegexOptions.Compiled); // 1 Jan 10:30
|
||||||
@@ -287,28 +287,9 @@ namespace Jackett.Common.Utils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TimeSpan ParseTimeSpan(string time)
|
private static TimeSpan ParseTimeSpan(string time) =>
|
||||||
{
|
string.IsNullOrWhiteSpace(time)
|
||||||
if (string.IsNullOrWhiteSpace(time))
|
? TimeSpan.Zero
|
||||||
return TimeSpan.Zero;
|
: DateTime.Parse(time).TimeOfDay;
|
||||||
|
|
||||||
var offset = TimeSpan.Zero;
|
|
||||||
if (time.EndsWith("AM"))
|
|
||||||
{
|
|
||||||
time = time.Substring(0, time.Length - 2);
|
|
||||||
if (time.StartsWith("12")) // 12:15 AM becomes 00:15
|
|
||||||
time = "00" + time.Substring(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
31
src/Jackett.Test/Utils/DateTimeUtilTests.cs
Normal file
31
src/Jackett.Test/Utils/DateTimeUtilTests.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Jackett.Common.Utils.Tests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class DateTimeUtilTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void FromUnknownTest()
|
||||||
|
{
|
||||||
|
var today = DateTime.UtcNow.Date;
|
||||||
|
var yesterday = today.AddDays(-1);
|
||||||
|
var tomorrow = today.AddDays(1);
|
||||||
|
var testCases = new Dictionary<string, DateTime>
|
||||||
|
{
|
||||||
|
{"today, at 1:00 PM", today.AddHours(13)},
|
||||||
|
{"today at 12:00PM", today.AddHours(12)},
|
||||||
|
{"Today", today},
|
||||||
|
{"Today at 20:19:54", today.AddHours(20).AddMinutes(19).AddSeconds(54)},
|
||||||
|
{"Today 22:29", today.AddHours(22).AddMinutes(29)},
|
||||||
|
{"Yesterday\n 19:54:54", yesterday.AddHours(19).AddMinutes(54).AddSeconds(54)},
|
||||||
|
{"Yesterday\n 11:55 PM", yesterday.AddHours(23).AddMinutes(55)}
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var testCase in testCases)
|
||||||
|
Assert.AreEqual( testCase.Value, DateTimeUtil.FromUnknown(testCase.Key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user