-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimezoneTest.cs
More file actions
143 lines (125 loc) · 4.96 KB
/
Copy pathTimezoneTest.cs
File metadata and controls
143 lines (125 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using RunCommandsService;
using Cronos;
namespace RunCommandsService
{
/// <summary>
/// Test class for timezone and cron expression improvements.
/// Run this as: dotnet run --project RunCommandsService.csproj -- --run-tests
/// Or call RunAllTests() from your own test runner.
/// </summary>
public class TimezoneTest
{
// Note: Main method removed to avoid conflict with Program.cs
// Call RunAllTests() from your test runner or Program.cs with args check
public static void RunAllTests()
{
Console.WriteLine("Testing timezone and cron expression improvements...\n");
// Test timezone mapping
TestTimezoneMapping();
// Test cron expression parsing
TestCronParsing();
// Test next occurrence calculation
TestNextOccurrence();
Console.WriteLine("\nAll tests completed!");
}
private static void TestTimezoneMapping()
{
Console.WriteLine("=== Testing Timezone Mapping ===");
var testZones = new List<string>
{
"America/New_York",
"Europe/London",
"Asia/Tokyo",
"Australia/Sydney",
"Europe/Berlin",
"America/Los_Angeles",
"Invalid/Timezone",
"",
null
};
foreach (var zone in testZones)
{
try
{
var tzInfo = TimeZoneHelper.FindTimeZone(zone);
Console.WriteLine($"'{zone ?? "null"}' -> '{tzInfo.Id}' (Display: {tzInfo.DisplayName})");
}
catch (Exception ex)
{
Console.WriteLine($"'{zone ?? "null"}' -> Error: {ex.Message}");
}
}
Console.WriteLine();
}
private static void TestCronParsing()
{
Console.WriteLine("=== Testing Cron Expression Parsing ===");
var testCrons = new List<string>
{
"0 9 * * *", // Every day at 9 AM
"0 23 * * 1-5", // Weekdays at 11 PM
"*/5 * * * *", // Every 5 minutes
"0 0 1 1 *", // Every Jan 1 at midnight
"invalid-cron", // Invalid expression
"", // Empty string
null // Null
};
foreach (var cron in testCrons)
{
try
{
if (string.IsNullOrEmpty(cron))
{
Console.WriteLine($"'{cron ?? "null"}' -> Invalid (empty/null)");
}
else
{
var expression = CronExpression.Parse(cron);
Console.WriteLine($"'{cron}' -> Valid");
}
}
catch (Exception ex)
{
Console.WriteLine($"'{cron ?? "null"}' -> Error: {ex.Message}");
}
}
Console.WriteLine();
}
private static void TestNextOccurrence()
{
Console.WriteLine("=== Testing Next Occurrence Calculation ===");
var testCases = new[]
{
new { Cron = "0 9 * * *", TimeZone = "America/New_York" },
new { Cron = "0 9 * * *", TimeZone = "Europe/London" },
new { Cron = "0 9 * * *", TimeZone = "Asia/Tokyo" },
new { Cron = "*/15 * * * *", TimeZone = "UTC" }
};
var now = DateTime.UtcNow;
foreach (var testCase in testCases)
{
try
{
var cron = CronExpression.Parse(testCase.Cron);
var tz = TimeZoneHelper.FindTimeZone(testCase.TimeZone);
// Use Cronos directly with UTC base time
var nextOccurrence = cron.GetNextOccurrence(DateTime.SpecifyKind(now, DateTimeKind.Utc), tz);
Console.WriteLine($"Cron '{testCase.Cron}' in '{testCase.TimeZone}' -> Next: {nextOccurrence?.ToString("o") ?? "null"}");
// Show local time too for comparison
if (nextOccurrence.HasValue)
{
var localTime = TimeZoneInfo.ConvertTimeFromUtc(nextOccurrence.Value, tz);
Console.WriteLine($" -> Local time: {localTime:yyyy-MM-dd HH:mm:ss} ({tz.Id})");
}
}
catch (Exception ex)
{
Console.WriteLine($"Cron '{testCase.Cron}' in '{testCase.TimeZone}' -> Error: {ex.Message}");
}
}
Console.WriteLine();
}
}
}