Maarten Balliauw 7/22/2026

TimeProvider and the End of Untestable DateTime.Now

Read Original

This article explains how .NET 8's built-in TimeProvider abstract class solves the long-standing problem of testing code that relies on DateTime.Now. It describes the common pattern of wrapping system clock calls with custom interfaces like IDateTimeProvider or IClock, and how each team implemented it differently. With TimeProvider, developers get a standardized solution in the base class library, featuring methods like GetUtcNow(), GetLocalNow(), and timestamp-based elapsed time measurement. The article also mentions how Duende IdentityServer v8 replaced its own IClock wrapper with TimeProvider, making it a practical case study for migrating legacy code to this new approach.

TimeProvider and the End of Untestable DateTime.Now

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week

No top articles yet