Skip to content

Commit 1e103a3

Browse files
authored
Rename TimeZoneTimeProvider to RequestTimeProvider for better clarity and consistency (#32)
This PR renames the `TimeZoneTimeProvider` class to `RequestTimeProvider` to improve code readability and better reflect its purpose. The class is primarily used to provide timezone-aware time information based on HTTP request context, making "RequestTimeProvider" a more descriptive and intuitive name. ## Changes Made - **Renamed class**: `TimeZoneTimeProvider` → `RequestTimeProvider` - **Renamed file**: `TimeZoneTimeProvider.cs` → `RequestTimeProvider.cs` - **Updated dependency injection**: Modified `Program.cs` to register the renamed class - **Updated constructor parameters**: Updated all template engines (`HandlebarsTemplateEngine`, `ScribanTemplateEngine`, `RazorTemplateEngine`) to use the new class name - **Updated variable names**: Changed all instances of `timeZoneTimeProvider` to `requestTimeProvider` throughout the codebase for consistency - **Updated Razor template injection**: Modified the `@inject` directive in `RazorTemplateEngine` to reference the new class and variable names ## Benefits - **Improved clarity**: The new name better describes the class's role in providing request-scoped time information - **Better consistency**: Variable names now follow camelCase convention with the new class name - **Enhanced maintainability**: More descriptive naming makes the codebase easier to understand for new developers All existing functionality remains unchanged - this is purely a refactoring change to improve code quality and readability. <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Rename TimeZoneTimeProvider class to RequestTimeProvider and update related variables</issue_title> > <issue_description>The `TimeZoneTimeProvider` class should be renamed to `RequestTimeProvider` for better clarity and consistency. Additionally, any variables of this type (e.g., `timeZoneTimeProvider`, `tztm`, or similar) should be updated to reflect the new class name throughout the codebase. > > **Tasks:** > - Rename the `TimeZoneTimeProvider` class to `RequestTimeProvider`. > - Update all variable names that reference this type to follow the new naming (e.g., `requestTimeProvider`). > - Ensure all references in documentation and comments are also updated. > - Run all tests to confirm there are no regressions after the rename. > > This refactor will help improve code readability and maintainability.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> Fixes #31 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.
2 parents 3ba2473 + b64140a commit 1e103a3

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

src/PdfSmith.BusinessLayer/Services/TimeZoneTimeProvider.cs renamed to src/PdfSmith.BusinessLayer/Services/RequestTimeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PdfSmith.BusinessLayer.Services;
44

5-
public class TimeZoneTimeProvider(ITimeZoneService timeZoneService) : TimeProvider
5+
public class RequestTimeProvider(ITimeZoneService timeZoneService) : TimeProvider
66
{
77
public override TimeZoneInfo LocalTimeZone => timeZoneService.GetTimeZone() ?? TimeZoneInfo.Utc;
88
}

src/PdfSmith.BusinessLayer/Templating/HandlebarsTemplateEngine.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace PdfSmith.BusinessLayer.Templating;
88

9-
public class HandlebarsTemplateEngine(TimeZoneTimeProvider timeZoneTimeProvider) : ITemplateEngine
9+
public class HandlebarsTemplateEngine(RequestTimeProvider requestTimeProvider) : ITemplateEngine
1010
{
11-
private readonly Lazy<IHandlebars> handlebarsInstance = new(() => CreateHandlebarsInstance(timeZoneTimeProvider));
11+
private readonly Lazy<IHandlebars> handlebarsInstance = new(() => CreateHandlebarsInstance(requestTimeProvider));
1212

1313
public Task<string> RenderAsync(string template, object? model, CultureInfo culture, CancellationToken cancellationToken = default)
1414
{
@@ -32,7 +32,7 @@ public Task<string> RenderAsync(string template, object? model, CultureInfo cult
3232
}
3333
}
3434

35-
private static IHandlebars CreateHandlebarsInstance(TimeZoneTimeProvider timeZoneTimeProvider)
35+
private static IHandlebars CreateHandlebarsInstance(RequestTimeProvider requestTimeProvider)
3636
{
3737
var handlebars = Handlebars.Create();
3838

@@ -92,7 +92,7 @@ private static IHandlebars CreateHandlebarsInstance(TimeZoneTimeProvider timeZon
9292
var format = arguments.ElementAtOrDefault(0)?.ToString() ??
9393
$"{CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern} {CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern}";
9494

95-
var now = timeZoneTimeProvider.GetLocalNow().DateTime;
95+
var now = requestTimeProvider.GetLocalNow().DateTime;
9696
return now.ToString(format, CultureInfo.CurrentCulture);
9797
});
9898

@@ -101,7 +101,7 @@ private static IHandlebars CreateHandlebarsInstance(TimeZoneTimeProvider timeZon
101101
var format = arguments.ElementAtOrDefault(0)?.ToString() ??
102102
$"{CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern} {CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern}";
103103

104-
var now = timeZoneTimeProvider.GetUtcNow().DateTime;
104+
var now = requestTimeProvider.GetUtcNow().DateTime;
105105
return now.ToString(format, CultureInfo.CurrentCulture);
106106
});
107107

src/PdfSmith.BusinessLayer/Templating/RazorTemplateEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public async Task<string> RenderAsync(string template, object? model, CultureInf
1313
{
1414
try
1515
{
16-
var sanitizedTemplate = DateTimeNowRegex.Replace(template, "@timeZoneTimeProvider.GetLocalNow().DateTime");
17-
sanitizedTemplate = DateTimeOffsetNowRegex.Replace(sanitizedTemplate, "@timeZoneTimeProvider.GetLocalNow()");
16+
var sanitizedTemplate = DateTimeNowRegex.Replace(template, "@requestTimeProvider.GetLocalNow().DateTime");
17+
sanitizedTemplate = DateTimeOffsetNowRegex.Replace(sanitizedTemplate, "@requestTimeProvider.GetLocalNow()");
1818

1919
var content = $"""
2020
@using System
2121
@using System.Collections.Generic
2222
@using System.Linq
23-
@inject PdfSmith.BusinessLayer.Services.TimeZoneTimeProvider timeZoneTimeProvider
23+
@inject PdfSmith.BusinessLayer.Services.RequestTimeProvider requestTimeProvider
2424
{sanitizedTemplate}
2525
""";
2626

src/PdfSmith.BusinessLayer/Templating/ScribanTemplateEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace PdfSmith.BusinessLayer.Templating;
1010

11-
public partial class ScribanTemplateEngine(TimeZoneTimeProvider timeZoneTimeProvider) : ITemplateEngine
11+
public partial class ScribanTemplateEngine(RequestTimeProvider requestTimeProvider) : ITemplateEngine
1212
{
1313
private const string DateTimeZonePlaceholder = "datetime_withzone";
1414

@@ -27,7 +27,7 @@ public async Task<string> RenderAsync(string text, object? model, CultureInfo cu
2727
context.PushCulture(culture);
2828

2929
var dateWithTimeZoneScript = new ScriptObject();
30-
dateWithTimeZoneScript.Import(DateTimeZonePlaceholder, new Func<DateTime>(() => timeZoneTimeProvider.GetLocalNow().DateTime));
30+
dateWithTimeZoneScript.Import(DateTimeZonePlaceholder, new Func<DateTime>(() => requestTimeProvider.GetLocalNow().DateTime));
3131
context.PushGlobal(dateWithTimeZoneScript);
3232

3333
var result = await template.RenderAsync(context);

src/PdfSmith/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
builder.Services.AddHttpContextAccessor();
3838

3939
builder.Services.AddSingleton(TimeProvider.System);
40-
builder.Services.AddSingleton<TimeZoneTimeProvider>();
40+
builder.Services.AddSingleton<RequestTimeProvider>();
4141

4242
builder.Services.AddSingleton<ITimeZoneService, TimeZoneService>();
4343

0 commit comments

Comments
 (0)