question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StartTime does not work if DateTime.Kind is not Utc

See original GitHub issue

We are continuously addressing and improving the SDK, if possible, make sure the problem persist in the latest SDK version.

Describe the bug This could be a feature but I thought I should report it in case it’s not. When specifying the WithStartTime, if I pass a DateTime with Kind Local, the ChangeFeedProcessor is created and lease is saved in the container but it doesn’t pick up any message even though there are items that are newer than the specified date. It only starts working if I set the value with a DateTime with Utc kind.

To Reproduce The code that’s not working

var startDateTimeString = "2022-11-08T14:28:33.000Z";
DateTime.TryParse(startDateTimeString, out DateTime startTime); // startTime.Kind is Local
var documentEndpoint = "https://someaccount.documents.azure.com:443/";
var databaseId = "ToDoList";
var targetContainerName = "Items";
var leaseContainerName = "leases";
var client = new CosmosClient(documentEndpoint, new DefaultAzureCredential());
var container = client.GetContainer(databaseId, targetContainerName);
var leaseContainer = _client.GetContainer(databaseId, leaseContainerName);
var processorName = $"manualChangeFeedProcessor{DateTime.UtcNow:O}";
var changeFeedProcessor = container
    .GetChangeFeedProcessorBuilder<Document>(processorName: processorName, HandleChangesAsync)
    .WithInstanceName($"consoleHost{DateTimeOffset.UtcNow.DateTime:O}")
    .WithLeaseContainer(leaseContainer)
    .WithStartTime(startTime)
    .WithErrorNotification(HandleError)
    .WithPollInterval(TimeSpan.FromMilliseconds(500))
    .Build();

await changeFeedProcessor.StartAsync();

The code that’s working: (I only need to call .ToUniversalTime() on startTime

var startDateTimeString = "2022-11-08T14:28:33.000Z";
DateTime.TryParse(startDateTimeString, out DateTime startTime); // startTime.Kind is Local
var documentEndpoint = "https://someaccount.documents.azure.com:443/";
var databaseId = "ToDoList";
var targetContainerName = "Items";
var leaseContainerName = "leases";
var client = new CosmosClient(documentEndpoint, new DefaultAzureCredential());
var container = client.GetContainer(databaseId, targetContainerName);
var leaseContainer = _client.GetContainer(databaseId, leaseContainerName);
var processorName = $"manualChangeFeedProcessor{DateTime.UtcNow:O}";
var changeFeedProcessor = container
    .GetChangeFeedProcessorBuilder<Document>(processorName: processorName, HandleChangesAsync)
    .WithInstanceName($"consoleHost{DateTimeOffset.UtcNow.DateTime:O}")
    .WithLeaseContainer(leaseContainer)
    .WithStartTime(startTime.ToUniversalTime())
    .WithErrorNotification(HandleError)
    .WithPollInterval(TimeSpan.FromMilliseconds(500))
    .Build();

await changeFeedProcessor.StartAsync();

Expected behavior It should either use given DateTime or throw error because I have given a DateTime that’s not Utc kind

Actual behavior It accepts given datetime but doesn’t process the changefeed.

Environment summary SDK Version: 3.31.2 OS Version: Windows

Additional context Add any other context about the problem here (for example, complete stack traces or logs).

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
roend83commented, Dec 12, 2022

@ealsur I found a way to work around this by manually calling GetChangeFeedProcessorBuilder in my function app startup. This allows me to manually create the change feed lease with a DateTime instance that has the correct UTC style. After that’s finished, the function app fires up and starts processing the change feed normally.

1reaction
deniz-newdaycommented, Nov 10, 2022

you’re right, DateTime.TryParse does parse it to Local by default for some reason. I agree with your concern that throwing exception to existing users would be a problem. I think .ToUniversalTime() would just work nicely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert DateTime of type DateTimeKind. ...
This is how ToUniversalTime() works; see MSDN. The problem is that these DateTimes are in fact already in UTC time. They are pulled...
Read more >
DateTime.SpecifyKind(DateTime, DateTimeKind) Method
A DateTime object consists of a Kind field that indicates whether the time value is based on local time, Coordinated Universal Time (UTC),...
Read more >
4 Common Datetime Mistakes in C# And How to Avoid Them
If Kind is set to UTC, then the same value is returned. On the other hand, if it's set to Local, the corresponding...
Read more >
Powershell and DateTimeKind - PI Square
I believe there is a bug with Get-PIArchive where an archive's StartTime and EndTime is incorrectly set with Kind=Unspecified when it should be...
Read more >
Specifying the Kind on DateTime
In your setter, you should only use SpecifyKind if the value.DateTimeKind is Unspecified. In other words, if the value passed in is Utc...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found