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)
{
// If we aren't a Markdown file, then there is nothing
// we can do about that.
// If we aren't a Markdown file, then there is nothing we can do about that.
if (!this.IsMarkdownTest(entity, path))
{
return entity;

View file

@ -18,14 +18,14 @@ namespace Nitride.Temporal.Cli;
/// </summary>
public class DatePipelineCommandOption : IPipelineCommandOption
{
private readonly Timekeeper clock;
private readonly Timekeeper timekeeper;
private readonly ILogger logger;
public DatePipelineCommandOption(ILogger logger, Timekeeper clock)
public DatePipelineCommandOption(ILogger logger, Timekeeper timekeeper)
{
this.logger = logger.ForContext<Instant>();
this.clock = clock;
this.timekeeper = timekeeper;
this.Option = new Option<DateTime>("--date")
{
Description = "Sets the date to something other than now",
@ -47,14 +47,14 @@ public class DatePipelineCommandOption : IPipelineCommandOption
{
// We have a date, so we need to create a fake clock that has this
// 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.
Instant now = this.clock.Clock.GetCurrentInstant();
ZonedDateTime dateTime = now.InZone(this.clock.DateTimeZone);
Instant now = this.timekeeper.Clock.GetCurrentInstant();
ZonedDateTime dateTime = now.InZone(this.timekeeper.DateTimeZone);
string formatted = dateTime.ToString("yyyy-MM-dd HH:mm:ss x", CultureInfo.CurrentCulture);
this.logger.Information("Setting date/time to {When:l}", formatted);

View file

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

View file

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

View file

@ -113,26 +113,4 @@ public class Timekeeper
{
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;
}
}