Skip to content

Commit 2d93bbc

Browse files
committed
Refactor date handling in GetStationBoardAsync to prioritize user-provided date and improve timezone management
1 parent 925a99c commit 2d93bbc

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

AbeckDev.DbTimetable.Mcp/Services/TimeTableService.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,32 @@ public async Task<string> GetStationBoardAsync(
4848
CancellationToken cancellationToken = default)
4949
{
5050
// Format: yyMMddHHmm (e.g., 2511051830 for 2025-11-05 18:30)
51-
var germanTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"));
52-
var dateParam = date?.ToString("yyMMdd") ?? germanTime.ToString("yyMMdd");
53-
var hourParam = date?.ToString("HH") ?? germanTime.ToString("HH");
51+
// If date is provided by user, use it directly (user is expected to provide German time)
52+
// If not provided, get current time in German timezone
53+
DateTime effectiveTime;
54+
if (date.HasValue)
55+
{
56+
effectiveTime = date.Value;
57+
}
58+
else
59+
{
60+
try
61+
{
62+
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
63+
effectiveTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
64+
}
65+
catch (TimeZoneNotFoundException)
66+
{
67+
effectiveTime = DateTime.Now; // fallback to local time if timezone not found
68+
}
69+
catch (InvalidTimeZoneException)
70+
{
71+
effectiveTime = DateTime.Now; // fallback to local time if timezone invalid
72+
}
73+
}
74+
75+
var dateParam = effectiveTime.ToString("yyMMdd");
76+
var hourParam = effectiveTime.ToString("HH");
5477

5578
using var request = new HttpRequestMessage(HttpMethod.Get, $"plan/{evaNo}/{dateParam}/{hourParam}");
5679
request.Headers.Add("DB-Client-Id", _config.ClientId);

0 commit comments

Comments
 (0)