Skip to content

WorkflowState CreatedAt/LastUpdatedAt throws in non-UTC timezones (DateTime.MinValue → DateTimeOffset) #1689

@daanschutte

Description

@daanschutte

Expected Behavior

WorkflowState unit tests should be timezone-invariant.

Actual Behavior

On the latest master branch, running tests with dotnet test has failures with certain timezones set, e.g. Dapr.Workflow.Test/WorkflowStateTests:Properties_ShouldReturnDefaults_WhenMetadataIsNull.

The failure occurs in WorkflowState.cs:56 during ⁨⁨⁨⁨⁨⁨⁨

public DateTimeOffset CreatedAt => _metadata?.CreatedAt ?? default;⁩⁩⁩⁩⁩⁩⁩

because _metadata?.CreatedAt ?? default is evaluated as a DateTime (default(DateTime) when _metadata is null), which is then implicitly converted to DateTimeOffset. The same applies to WorkflowState.cs:61 ⁨⁨⁨⁨⁨⁨⁨for LastUpdatedAt.

In non-UTC timezones this conversion applies a local offset to DateTime.MinValue and can underflow, throwing ArgumentOutOfRangeException. When TZ=UTC the offset is 0, so no underflow occurs.

System.ArgumentOutOfRangeException: The UTC time represented when the offset is applied must be between year 0 and 10,000. (Parameter 'offset')

System.ArgumentOutOfRangeException
The UTC time represented when the offset is applied must be between year 0 and 10,000. (Parameter 'offset')
   at System.DateTimeOffset.<ValidateDate>g__ThrowOutOfRange|128_0()
   at System.DateTimeOffset..ctor(DateTime dateTime)
   at System.DateTimeOffset.op_Implicit(DateTime dateTime)
   at Dapr.Workflow.WorkflowState.get_CreatedAt() in /Users/daan/Developer/contrib/dotnet/dapr-dotnet-sdk/src/Dapr.Workflow/WorkflowState.cs:line 57
   at Dapr.Workflow.Test.WorkflowStateTests.Properties_ShouldReturnDefaults_WhenMetadataIsNull() in /Users/daan/Developer/contrib/dotnet/dapr-dotnet-sdk/test/Dapr.Workflow.Test/WorkflowStateTests.cs:line 29
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Explicitly setting the timezone to UTC and running the tests passes⁨:

TZ=UTC dotnet test test/Dapr.Workflow.Test/Dapr.Workflow.Test.csproj  --filter FullyQualifiedName~WorkflowStateTests⁩⁩⁩⁩⁩⁩⁩

Steps to Reproduce the Problem

Change the timezone to e.g. Asia/Qatar, Asia/Tokyo, Europe/Amsterdam and run tests, e.g.

TZ=Europe/Amsterdam dotnet test test/Dapr.Workflow.Test/Dapr.Workflow.Test.csproj \
  --filter FullyQualifiedName~Properties_ShouldReturnDefaults_WhenMetadataIsNull

Tested on MacOS 26.2

Release Note

RELEASE NOTE: FIX ArgumentOutOfRangeException in implicit DateTime to DateTimeOffset conversion in WorkflowState in certain timezones.

Metadata

Metadata

Assignees

Labels

kind/bugSomething isn't working

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions