fix(temporal): set the date time zone properly

This commit is contained in:
Dylan R. E. Moonfire 2022-06-07 08:55:13 -05:00
parent 3a009b05cf
commit 85fa0fe97e
5 changed files with 27 additions and 39 deletions

View file

@ -39,8 +39,7 @@ public partial class IdentifyMarkdown : IOperation
private Entity MarkBinaryEntities(Entity entity, UPath path, IBinaryContent binary) private Entity MarkBinaryEntities(Entity entity, UPath path, IBinaryContent binary)
{ {
// If we aren't a Markdown file, then there is nothing // If we aren't a Markdown file, then there is nothing we can do about that.
// we can do about that.
if (!this.IsMarkdownTest(entity, path)) if (!this.IsMarkdownTest(entity, path))
{ {
return entity; return entity;

View file

@ -18,14 +18,14 @@ namespace Nitride.Temporal.Cli;
/// </summary> /// </summary>
public class DatePipelineCommandOption : IPipelineCommandOption public class DatePipelineCommandOption : IPipelineCommandOption
{ {
private readonly Timekeeper clock; private readonly Timekeeper timekeeper;
private readonly ILogger logger; private readonly ILogger logger;
public DatePipelineCommandOption(ILogger logger, Timekeeper clock) public DatePipelineCommandOption(ILogger logger, Timekeeper timekeeper)
{ {
this.logger = logger.ForContext<Instant>(); this.logger = logger.ForContext<Instant>();
this.clock = clock; this.timekeeper = timekeeper;
this.Option = new Option<DateTime>("--date") this.Option = new Option<DateTime>("--date")
{ {
Description = "Sets the date to something other than now", Description = "Sets the date to something other than now",
@ -42,19 +42,19 @@ public class DatePipelineCommandOption : IPipelineCommandOption
// If we got a date, then use NodaTime's fake clock to set it so // If we got a date, then use NodaTime's fake clock to set it so
// everything will use that. // everything will use that.
var value = (DateTime?)context.ParseResult.GetValueForOption(this.Option); var value = (DateTime?)context.ParseResult.GetValueForOption(this.Option);
if (value.HasValue && value.Value != DateTime.MinValue) if (value.HasValue && value.Value != DateTime.MinValue)
{ {
// We have a date, so we need to create a fake clock that has this // We have a date, so we need to create a fake clock that has this
// date for the entire run. // date for the entire run.
Instant instant = this.clock.CreateInstant(value.Value); Instant instant = this.timekeeper.CreateInstant(value.Value);
this.clock.Clock = new FakeClock(instant); this.timekeeper.Clock = new FakeClock(instant);
} }
// Report the date we are processing. // Report the date we are processing.
Instant now = this.clock.Clock.GetCurrentInstant(); Instant now = this.timekeeper.Clock.GetCurrentInstant();
ZonedDateTime dateTime = now.InZone(this.clock.DateTimeZone); ZonedDateTime dateTime = now.InZone(this.timekeeper.DateTimeZone);
string formatted = dateTime.ToString("yyyy-MM-dd HH:mm:ss x", CultureInfo.CurrentCulture); string formatted = dateTime.ToString("yyyy-MM-dd HH:mm:ss x", CultureInfo.CurrentCulture);
this.logger.Information("Setting date/time to {When:l}", formatted); this.logger.Information("Setting date/time to {When:l}", formatted);

View file

@ -53,13 +53,16 @@ public static class NitrideTemporalBuilderExtensions
} }
}); });
builder.ConfigureSite( if (config.DateTimeZone != null)
(_, scope) => {
{ builder.ConfigureSite(
Timekeeper timekeeper = scope.Resolve<Timekeeper>(); (_, scope) =>
{
Timekeeper timekeeper = scope.Resolve<Timekeeper>();
timekeeper.DateTimeZone = timekeeper.DateTimeZone; timekeeper.DateTimeZone = config.DateTimeZone;
}); });
}
return builder; return builder;
} }

View file

@ -1,5 +1,7 @@
using System; using System;
using NodaTime;
namespace Nitride.Temporal; namespace Nitride.Temporal;
/// <summary> /// <summary>
@ -19,7 +21,7 @@ public partial class NitrideTemporalConfiguration
/// Gets or sets the time zone to use for date time operations. Examples would be /// Gets or sets the time zone to use for date time operations. Examples would be
/// "America/Chicago". /// "America/Chicago".
/// </summary> /// </summary>
public string? DateTimeZone { get; set; } public DateTimeZone? DateTimeZone { get; set; }
public string? Expiration { get; set; } public string? Expiration { get; set; }
@ -28,6 +30,12 @@ public partial class NitrideTemporalConfiguration
return this.WithAddDateOptionToCommandLine(true); return this.WithAddDateOptionToCommandLine(true);
} }
public NitrideTemporalConfiguration WithDateTimeZone(string zoneName)
{
this.DateTimeZone = DateTimeZoneProviders.Tzdb[zoneName];
return this;
}
/// <summary> /// <summary>
/// Adds the "--expire" option into the pipeline commands where with the value set /// Adds the "--expire" option into the pipeline commands where with the value set
/// to /// to

View file

@ -113,26 +113,4 @@ public class Timekeeper
{ {
return instant.InZone(this.DateTimeZone).ToDateTimeOffset().DateTime; return instant.InZone(this.DateTimeZone).ToDateTimeOffset().DateTime;
} }
/// <summary>
/// Sets the clock used for time calculations.
/// </summary>
/// <param name="clock">The new clock to use.</param>
/// <returns>The instance for chaining methods.</returns>
public Timekeeper WithClock(IClock clock)
{
this.Clock = clock;
return this;
}
/// <summary>
/// Sets the date time zone and returns itself for chaining.
/// </summary>
/// <param name="timeZoneName">The name of the time zone.</param>
/// <returns>The instance for chaining methods.</returns>
public Timekeeper WithDateTimeZone(string timeZoneName)
{
this.DateTimeZone = DateTimeZoneProviders.Tzdb[timeZoneName];
return this;
}
} }