diff --git a/src/Apollo.Application/ToDos/TimeParsingService.cs b/src/Apollo.Application/ToDos/TimeParsingService.cs index d1a8266c..7c2d0a2e 100644 --- a/src/Apollo.Application/ToDos/TimeParsingService.cs +++ b/src/Apollo.Application/ToDos/TimeParsingService.cs @@ -70,6 +70,15 @@ public async Task> ParseTimeAsync( if (DateTime.TryParse(input.Trim(), CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsed)) { + // If the input is a time-only string (e.g., "3:00 PM"), DateTime.TryParse uses the + // system's current local date as the default, which may not match the user's today. + // Anchor such times to today in the user's timezone instead. + if (TimeOnly.TryParse(input.Trim(), CultureInfo.InvariantCulture, out _)) + { + var today = GetTodayInUserTimezone(now, userTimeZoneId); + parsed = today.Add(parsed.TimeOfDay); + } + var utcParsed = ConvertToUtc(parsed, userTimeZoneId); return Result.Ok(utcParsed); } @@ -112,6 +121,28 @@ private async Task> ParseWithLlmAsync( return Result.Ok(utcResult); } + private static DateTime GetTodayInUserTimezone(DateTime utcNow, string? userTimeZoneId) + { + if (string.IsNullOrWhiteSpace(userTimeZoneId)) + { + return utcNow.Date; + } + + try + { + var timeZone = TimeZoneInfo.FindSystemTimeZoneById(userTimeZoneId); + return TimeZoneInfo.ConvertTimeFromUtc(utcNow, timeZone).Date; + } + catch (TimeZoneNotFoundException) + { + return utcNow.Date; + } + catch (InvalidTimeZoneException) + { + return utcNow.Date; + } + } + private static DateTime ConvertToUtc(DateTime parsedDate, string? userTimeZoneId) { return parsedDate.Kind switch diff --git a/tests/Apollo.Application.Tests/ToDos/TimeParsingServiceTests.cs b/tests/Apollo.Application.Tests/ToDos/TimeParsingServiceTests.cs index cdf49ead..058b062e 100644 --- a/tests/Apollo.Application.Tests/ToDos/TimeParsingServiceTests.cs +++ b/tests/Apollo.Application.Tests/ToDos/TimeParsingServiceTests.cs @@ -75,10 +75,38 @@ public async Task ParseTimeAsyncWithCommonTimeFormatReturnsUtcDateTimeAsync( // Act var result = await _service.ParseTimeAsync(input); - // Assert + // Assert - time-only input should be anchored to today's date (not 0001-01-01) + Assert.True(result.IsSuccess); + Assert.Equal(ReferenceTime.Year, result.Value.Year); + Assert.Equal(ReferenceTime.Month, result.Value.Month); + Assert.Equal(ReferenceTime.Day, result.Value.Day); + Assert.Equal(expectedHour, result.Value.Hour); + Assert.Equal(expectedMinute, result.Value.Minute); + } + + [Theory] + [InlineData("3:00 PM", "America/Chicago", 2025, 12, 30, 21, 0)] + [InlineData("9:30 AM", "America/New_York", 2025, 12, 30, 14, 30)] + public async Task ParseTimeAsyncWithTimeOnlyInputAndTimezoneAnchorsToTodayInUserTimezoneAsync( + string input, string timezone, int expectedYear, int expectedMonth, int expectedDay, + int expectedHour, int expectedMinute) + { + // Arrange - fuzzy parser fails + _ = _fuzzyTimeParser + .Setup(p => p.TryParseFuzzyTime(input, ReferenceTime)) + .Returns(Result.Fail("Not fuzzy")); + + // Act + var result = await _service.ParseTimeAsync(input, timezone); + + // Assert - time should be anchored to today (2025-12-30) and converted to UTC Assert.True(result.IsSuccess); + Assert.Equal(expectedYear, result.Value.Year); + Assert.Equal(expectedMonth, result.Value.Month); + Assert.Equal(expectedDay, result.Value.Day); Assert.Equal(expectedHour, result.Value.Hour); Assert.Equal(expectedMinute, result.Value.Minute); + Assert.Equal(DateTimeKind.Utc, result.Value.Kind); } [Theory]