@@ -14,6 +14,17 @@ namespace Microsoft.SemanticKernel.Plugins.Core;
1414/// </remark>
1515public sealed class TimePlugin
1616{
17+ private readonly TimeProvider _timeProvider ;
18+
19+ /// <summary>
20+ /// Initializes a new instance of the <see cref="TimePlugin"/> class.
21+ /// </summary>
22+ /// <param name="timeProvider">The time provider to use. Defaults to <see cref="TimeProvider.System"/>.</param>
23+ public TimePlugin ( TimeProvider ? timeProvider = null )
24+ {
25+ this . _timeProvider = timeProvider ?? TimeProvider . System ;
26+ }
27+
1728 /// <summary>
1829 /// Get the current date
1930 /// </summary>
@@ -24,7 +35,7 @@ public sealed class TimePlugin
2435 [ KernelFunction , Description ( "Get the current date" ) ]
2536 public string Date ( IFormatProvider ? formatProvider = null ) =>
2637 // Example: Sunday, 12 January, 2025
27- DateTimeOffset . Now . ToString ( "D" , formatProvider ) ;
38+ this . _timeProvider . GetLocalNow ( ) . ToString ( "D" , formatProvider ) ;
2839
2940 /// <summary>
3041 /// Get the current date
@@ -48,7 +59,7 @@ public string Today(IFormatProvider? formatProvider = null) =>
4859 [ KernelFunction , Description ( "Get the current date and time in the local time zone" ) ]
4960 public string Now ( IFormatProvider ? formatProvider = null ) =>
5061 // Sunday, January 12, 2025 9:15 PM
51- DateTimeOffset . Now . ToString ( "f" , formatProvider ) ;
62+ this . _timeProvider . GetLocalNow ( ) . ToString ( "f" , formatProvider ) ;
5263
5364 /// <summary>
5465 /// Get the current UTC date and time
@@ -60,7 +71,7 @@ public string Now(IFormatProvider? formatProvider = null) =>
6071 [ KernelFunction , Description ( "Get the current UTC date and time" ) ]
6172 public string UtcNow ( IFormatProvider ? formatProvider = null ) =>
6273 // Sunday, January 13, 2025 5:15 AM
63- DateTimeOffset . UtcNow . ToString ( "f" , formatProvider ) ;
74+ this . _timeProvider . GetUtcNow ( ) . ToString ( "f" , formatProvider ) ;
6475
6576 /// <summary>
6677 /// Get the current time
@@ -72,7 +83,7 @@ public string UtcNow(IFormatProvider? formatProvider = null) =>
7283 [ KernelFunction , Description ( "Get the current time" ) ]
7384 public string Time ( IFormatProvider ? formatProvider = null ) =>
7485 // Example: 09:15:07 PM
75- DateTimeOffset . Now . ToString ( "hh:mm:ss tt" , formatProvider ) ;
86+ this . _timeProvider . GetLocalNow ( ) . ToString ( "hh:mm:ss tt" , formatProvider ) ;
7687
7788 /// <summary>
7889 /// Get the current year
@@ -84,7 +95,7 @@ public string Time(IFormatProvider? formatProvider = null) =>
8495 [ KernelFunction , Description ( "Get the current year" ) ]
8596 public string Year ( IFormatProvider ? formatProvider = null ) =>
8697 // Example: 2025
87- DateTimeOffset . Now . ToString ( "yyyy" , formatProvider ) ;
98+ this . _timeProvider . GetLocalNow ( ) . ToString ( "yyyy" , formatProvider ) ;
8899
89100 /// <summary>
90101 /// Get the current month name
@@ -96,7 +107,7 @@ public string Year(IFormatProvider? formatProvider = null) =>
96107 [ KernelFunction , Description ( "Get the current month name" ) ]
97108 public string Month ( IFormatProvider ? formatProvider = null ) =>
98109 // Example: January
99- DateTimeOffset . Now . ToString ( "MMMM" , formatProvider ) ;
110+ this . _timeProvider . GetLocalNow ( ) . ToString ( "MMMM" , formatProvider ) ;
100111
101112 /// <summary>
102113 /// Get the current month number
@@ -108,7 +119,7 @@ public string Month(IFormatProvider? formatProvider = null) =>
108119 [ KernelFunction , Description ( "Get the current month number" ) ]
109120 public string MonthNumber ( IFormatProvider ? formatProvider = null ) =>
110121 // Example: 01
111- DateTimeOffset . Now . ToString ( "MM" , formatProvider ) ;
122+ this . _timeProvider . GetLocalNow ( ) . ToString ( "MM" , formatProvider ) ;
112123
113124 /// <summary>
114125 /// Get the current day of the month
@@ -120,7 +131,7 @@ public string MonthNumber(IFormatProvider? formatProvider = null) =>
120131 [ KernelFunction , Description ( "Get the current day of the month" ) ]
121132 public string Day ( IFormatProvider ? formatProvider = null ) =>
122133 // Example: 12
123- DateTimeOffset . Now . ToString ( "dd" , formatProvider ) ;
134+ this . _timeProvider . GetLocalNow ( ) . ToString ( "dd" , formatProvider ) ;
124135
125136 /// <summary>
126137 /// Get the date a provided number of days in the past
@@ -129,7 +140,7 @@ public string Day(IFormatProvider? formatProvider = null) =>
129140 [ KernelFunction ]
130141 [ Description ( "Get the date offset by a provided number of days from today" ) ]
131142 public string DaysAgo ( [ Description ( "The number of days to offset from today" ) ] double input , IFormatProvider ? formatProvider = null ) =>
132- DateTimeOffset . Now . AddDays ( - input ) . ToString ( "D" , formatProvider ) ;
143+ this . _timeProvider . GetLocalNow ( ) . AddDays ( - input ) . ToString ( "D" , formatProvider ) ;
133144
134145 /// <summary>
135146 /// Get the current day of the week
@@ -141,7 +152,7 @@ public string DaysAgo([Description("The number of days to offset from today")] d
141152 [ KernelFunction , Description ( "Get the current day of the week" ) ]
142153 public string DayOfWeek ( IFormatProvider ? formatProvider = null ) =>
143154 // Example: Sunday
144- DateTimeOffset . Now . ToString ( "dddd" , formatProvider ) ;
155+ this . _timeProvider . GetLocalNow ( ) . ToString ( "dddd" , formatProvider ) ;
145156
146157 /// <summary>
147158 /// Get the current clock hour
@@ -153,7 +164,7 @@ public string DayOfWeek(IFormatProvider? formatProvider = null) =>
153164 [ KernelFunction , Description ( "Get the current clock hour" ) ]
154165 public string Hour ( IFormatProvider ? formatProvider = null ) =>
155166 // Example: 9 PM
156- DateTimeOffset . Now . ToString ( "h tt" , formatProvider ) ;
167+ this . _timeProvider . GetLocalNow ( ) . ToString ( "h tt" , formatProvider ) ;
157168
158169 /// <summary>
159170 /// Get the current clock 24-hour number
@@ -165,7 +176,7 @@ public string Hour(IFormatProvider? formatProvider = null) =>
165176 [ KernelFunction , Description ( "Get the current clock 24-hour number" ) ]
166177 public string HourNumber ( IFormatProvider ? formatProvider = null ) =>
167178 // Example: 21
168- DateTimeOffset . Now . ToString ( "HH" , formatProvider ) ;
179+ this . _timeProvider . GetLocalNow ( ) . ToString ( "HH" , formatProvider ) ;
169180
170181 /// <summary>
171182 /// Get the date of the previous day matching the supplied day name
@@ -181,7 +192,7 @@ public string DateMatchingLastDayName(
181192 [ Description ( "The day name to match" ) ] DayOfWeek input ,
182193 IFormatProvider ? formatProvider = null )
183194 {
184- DateTimeOffset dateTime = DateTimeOffset . Now ;
195+ DateTimeOffset dateTime = this . _timeProvider . GetLocalNow ( ) ;
185196
186197 // Walk backwards from the previous day for up to a week to find the matching day
187198 for ( int i = 1 ; i <= 7 ; ++ i )
@@ -206,7 +217,7 @@ public string DateMatchingLastDayName(
206217 [ KernelFunction , Description ( "Get the minutes on the current hour" ) ]
207218 public string Minute ( IFormatProvider ? formatProvider = null ) =>
208219 // Example: 15
209- DateTimeOffset . Now . ToString ( "mm" , formatProvider ) ;
220+ this . _timeProvider . GetLocalNow ( ) . ToString ( "mm" , formatProvider ) ;
210221
211222 /// <summary>
212223 /// Get the seconds on the current minute
@@ -218,7 +229,7 @@ public string Minute(IFormatProvider? formatProvider = null) =>
218229 [ KernelFunction , Description ( "Get the seconds on the current minute" ) ]
219230 public string Second ( IFormatProvider ? formatProvider = null ) =>
220231 // Example: 07
221- DateTimeOffset . Now . ToString ( "ss" , formatProvider ) ;
232+ this . _timeProvider . GetLocalNow ( ) . ToString ( "ss" , formatProvider ) ;
222233
223234 /// <summary>
224235 /// Get the local time zone offset from UTC
@@ -230,7 +241,7 @@ public string Second(IFormatProvider? formatProvider = null) =>
230241 [ KernelFunction , Description ( "Get the local time zone offset from UTC" ) ]
231242 public string TimeZoneOffset ( IFormatProvider ? formatProvider = null ) =>
232243 // Example: -08:00
233- DateTimeOffset . Now . ToString ( "%K" , formatProvider ) ;
244+ this . _timeProvider . GetLocalNow ( ) . ToString ( "%K" , formatProvider ) ;
234245
235246 /// <summary>
236247 /// Get the local time zone name
@@ -246,5 +257,5 @@ public string TimeZoneOffset(IFormatProvider? formatProvider = null) =>
246257 public string TimeZoneName ( ) =>
247258 // Example: PST
248259 // Note: this is the "current" timezone and it can change over the year, e.g. from PST to PDT
249- TimeZoneInfo . Local . DisplayName ;
260+ this . _timeProvider . LocalTimeZone . DisplayName ;
250261}
0 commit comments