This commit refactors the EventOccurrence parsing logic to utilize the EventOccurrenceParseGroup class, enhancing the organization of parsed occurrences by grouping them based on event definitions and optional section levels. The changes include updates to the EventOccurrenceParseResult, EventOccurrenceParser, and EventOccurrenceParserService to accommodate the new grouping structure. Additionally, tests are modified to reflect these changes, ensuring that the parsing functionality remains intact and accurate. This refactor improves data handling and aligns with the overall architecture of the application.
54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using Core.Parsers.EventOccurrence;
|
|
|
|
namespace GoogleSheetsScheduleImport;
|
|
|
|
/// <summary>
|
|
/// Parses time labels from the first column of schedule grids.
|
|
/// </summary>
|
|
public static class TimeCellParser
|
|
{
|
|
private static readonly Regex ClockRegex = new(
|
|
@"^(?<h>\d{1,2})(?::(?<m>\d{2}))?\s*(?<ap>a\.?m\.?|p\.?m\.?|AM|PM|am|pm)\s*$",
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
public static bool TryParse(string? cellText, out TimeOnly time)
|
|
{
|
|
time = default;
|
|
if (string.IsNullOrWhiteSpace(cellText))
|
|
return false;
|
|
var t = cellText.Trim();
|
|
if (t.Equals("NOON", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
time = new TimeOnly(12, 0);
|
|
return true;
|
|
}
|
|
|
|
var m = ClockRegex.Match(t);
|
|
if (m.Success)
|
|
{
|
|
var h = int.Parse(m.Groups["h"].Value, CultureInfo.InvariantCulture);
|
|
var minute = m.Groups["m"].Success ? int.Parse(m.Groups["m"].Value, CultureInfo.InvariantCulture) : 0;
|
|
var apStr = m.Groups["ap"].Value;
|
|
var isPm = apStr.Contains('P', StringComparison.OrdinalIgnoreCase);
|
|
var isAm = apStr.Contains('A', StringComparison.OrdinalIgnoreCase);
|
|
if (isPm && h < 12) h += 12;
|
|
if (isAm && h == 12) h = 0;
|
|
time = new TimeOnly(h, minute);
|
|
return true;
|
|
}
|
|
|
|
// Fallback: use core TimeParser if string already looks like parsed format
|
|
try
|
|
{
|
|
time = TimeParser.Parse(t);
|
|
return true;
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|